Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	isPrime := Sieve(1299709)
	for {
		k := io.NextInt()
		if k == 0 {
			break
		}
		ans := 0
		if !isPrime[k] {
			p, n := k-1, k+1
			for !isPrime[p] || !isPrime[n] {
				if !isPrime[p] {
					p--
				}
				if !isPrime[n] {
					n++
				}
			}
			ans = n - p
		}
		io.Println(ans)
	}
}

func Sieve(x int) []bool {
	flags := make([]bool, x+1)
	for i := 2; i <= x; i++ {
		flags[i] = true
	}
	for i := 2; i <= int(math.Sqrt(float64(x))); i++ {
		if !flags[i] {
			continue
		}
		for j := i * i; j <= x; j += i {
			flags[j] = false
		}
	}
	return flags
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

const ll MAX = 1300000;
bool isPrime[MAX];
int d[MAX];
void calc(){
  FOR(i,0,MAX) d[i] = -1;
  d[0] = 0;
  d[1] = 0;
  for (ll i = 0; i < MAX; i++) isPrime[i] = true;
  int cnt = 0;
  for (ll i = 2; i < MAX + 1; i++){
    if(isPrime[i]){
      d[i-1] = cnt;
      d[i] = 0;
      cnt = 0;
      for (ll j = 2 * i; j < MAX; j += i) isPrime[j] = false;
    } else {
      cnt++;
    }
  }
  for(int i = MAX - 1; i >= 0; i--) {
    if(d[i] > 0 && d[i-1] == -1) d[i-1] = d[i];
  }
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  calc();
  int n;
  while(cin >> n, n) cout << (d[n] == 0 ? 0 : (d[n] + 1)) << '\n';
  return 0;
}

C++: #include <bitset>
#include <iostream>

using namespace std;

int main()
{
    const auto primes = [] {
        bitset<1'000'000> b;
        auto t = b;
        t.set();
        t[0] = false;
        t[1] = false;
        int i = 0;
        while (true) {
            bool done = false;
            while (!t[i]) {
                ++i;
                if (i == t.size()) {
                    done = true;
                    break;
                }
            }
            if (done) {
                break;
            }

            b[i] = true;
            for (int j = i; j < t.size(); j += i) {
                t[j] = false;
            }
        }
        return b;
    }();

    while (true) {
        int q;
        cin >> q;
        if (q == 0) {
            return 0;
        }

        int start = 2;
        for (int i = 2; i < primes.size(); ++i) {
            if (primes[i]) {
                auto stop = i;
                if (i == q) {
                    cout << 0 << endl;
                    break;
                }
                else if (start < q && q < stop) {
                    cout << (stop - start) << endl;
                    break;
                }
                start = i;
            }
        }
    }
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<int> get_prime(int n) {
    if(n <= 1) return vector<int>{};
    vector<bool> is_prime(n+1, true);
    vector<int> prime;
    for (int i = 2; i <= n; ++i) {
        if(!is_prime[i]) continue;
        prime.emplace_back(i);
        for (int j = i*2; j <= n; j += i) {
            is_prime[j] = false;
        }
    }
    return prime;
}

int main() {
    const auto primes = get_prime(1300000);
    set<int> s;
    for (auto &&i : primes) s.insert(i);
    int n;
    while(cin >> n, n){
        auto ub = s.lower_bound(n);
        auto lb = ub;
        lb--;
        cout << n << "\n";
        if(*ub == n){
            puts("0");
            continue;
        }else {
            printf("%d\n", *ub-*lb);
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const double PI = acos(-1);
const ll MOD = 1000000007;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

vector<bool> A(1299710,true);
vector<int> B(0);

int main() {
  A[0] = A[1] = false;
  for (int i = 2; i * i <= 1299709; i++) {
    if (A[i]) {
      int j = i + i;
      while (j <= 1299709) {
        A[j] = false;
        j += i;
      }
    }
  }
  rep(i,1299710) {
    if (A[i]) B.push_back(i); 
  }

  while (true) {
    int N; cin >> N;
    if (N == 0) break;
    if (A[N]) cout << 0 << endl;
    else {
      int v = lower_bound(all(B),N) - B.begin();
      cout << B[v] - B[v-1] << endl;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {

	int n = 1299709 + 1;
	bool prime[n+1];
  memset(prime, true, sizeof(prime));
 
  for (int p=2; p*p<=n; p++)
  {
      if (prime[p] == true)
      {
          for (int i=p*2; i<=n; i += p)
              prime[i] = false;
      }
  }

  int N;
	while (cin >> N && N != 0) {
		int ans = 0;
		if (N < 2 && N > 1299709) {
			ans = 0;
			cout << ans << '\n';
			continue;
		}

		int l = N, r = N;
		while(!prime[l] || !prime[r]) {
			if (!prime[l])
				l--;
			if (!prime[r])
				r++;
		}
		ans = r-l;
		cout << ans << '\n';
	}

	return 0;
}

C++: #include<iostream>

using namespace std;

bool is_prime(int n){
	if(n<=1)return false;
	for(int i=2;i*i<=n;i++){
		if(n%i==0)return false;
	}
	return true;
}

int main(){
	int n;
	while(cin>>n && n){
		if(is_prime(n)){
			cout<<0<<endl;
			continue;
		}
		else{
			int ans=1;
			for(int i=n+1;!is_prime(i);i++)ans++;
			for(int i=n-1;!is_prime(i);i--)ans++;
			cout<<ans+1<<endl;
		}
	}
}