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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;

#define TT template<typename T>
#define TTF template<typename T, typename F>
#define ET(T) typename T::value_type
TT inline T sq(T x){return x*x;}
TT inline T In(){T x;cin>>x;return x;}
TT inline void Out(T&x){cout<<x;}
TT inline void sort(T&v){sort(begin(v),end(v));}
TT inline void revs(T&v){reverse(begin(v),end(v));}
TT inline void uniq(T&v){sort(v);v.erase(unique(begin(v),end(v)),end(v));}
TT inline int ubnd(T&v,ET(T)&x){return upper_bound(begin(v),end(v),x)-begin(v);}
TT inline int lbnd(T&v,ET(T)&x){return lower_bound(begin(v),end(v),x)-begin(v);}
TTF inline void inpt(T&v,int n,F f){for(v.reserve(n);n--;v.emplace_back(f()));}
TTF inline void show(T&v,F f,string d=" ",string e="\n"){int i=0;for(auto&x:v)i++&&(cout<<d),f(x);cout<<e;}
#undef TT
#undef TTF
#undef ET

inline void fast_io(){ios::sync_with_stdio(0);cin.tie(0);}
inline int in(){int x;scanf("%d",&x);return x;}
inline ll pow_mod(ll a,ll k,ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
inline ll mod_inv(ll a,ll p){return pow_mod(a,p-2,p);}

const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000;
const int N = 10000001;

bool sieve[N] = {true, true};
int main()
{
  for (int i = 2; i * i < N; i++){
    if (sieve[i]) continue;
    for (int j = i * i; j < N; j += i) sieve[j] = true;
  }
  int n;
  while (n = in()){
    int l, r;
    l = r = 0;
    while (sieve[n - l]) l++;
    while (sieve[n + r]) r++;
    printf("%d\n", l + r);
  }

  return (0);
}

// priority_queue queue deque front stringstream max_element min_element insert count make_tuple

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 1299709+5;
bool prime[MAX_N];
vector<int> p;

void make_prime (void )
{
	fill (prime, prime + MAX_N, true ); 
	prime[0] = false;
	prime[1] = false;
	for (int i = 2; i < MAX_N; i++ ){
		if (prime[i] ){
			for (int j = 2*i; j < MAX_N; j += i ){
				prime[j] = false;
			} // end for
		} // end if
	} // end for
	p.clear();
	rep (i, MAX_N ){
		if (prime[i] ){
			p.push_back (i );
		} // end if
	} // end rep
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.PrimeGap", "r", stdin );
	make_prime();

	int n;
	while (scanf ("%d", &n ), n ){
		if (prime[n] ){
			cout << 0 << endl;
		}else{
			vector<int>::iterator it = upper_bound (ALL (p ), n );	// n より大きい素数の位置がでる。
			int m = upper_bound (ALL (p ), n ) - p.begin();
			cout << (*it) - p[m-1] << endl;
		} // end if
	} // end loop
		
	return 0;
}

C++: #include <vector>
#include <iostream>
#include <algorithm>
using namespece std;
vector<int> P;
const int pmax=1299709;
bool iscomp[pmax+1];
void init()
{
	P.reserve(100000);
	for(int i=2;i<pmax+1;i++){
		if(iscomp[i])continue;
		P.push_back(i);
		for(int j=2;i*j<pmax+1;j++)
			iscomp[i*j]=true;
	}
}
int main()
{
	init();
	while(true){
		int n;
		cin>>n;
		if(n==0)return;
		int pos=lower_bound(n)-R.begin();
		if(P[pos]==n)cout<<0<<endl;
		else{
			cout<<P[pos+1]-P[pos]<<endl;
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);++i)

#define N 1300000
bool prime[N];

int solve(int n){
    if(prime[n]) return 0;
    int l=n,r=n;
    while(!prime[l])l--;
    while(!prime[r])r++;
    return r-l;
}

int main(void){
    rep(i,N) prime[i] = true;
    prime[0] = prime[1] = false;
    for(int i=2; i<=sqrt(N); i++){
        if(!prime[i]) continue;
        for(int j=i*i; j<N; j+=i){
            prime[j] = false;
        }
    }

    int n;
    while(cin>>n,n){
        cout<<solve(n)<<endl;
    }
    return 0;
}

C++: #include<iostream>

using namespace std;

#define MAX 1299709

int main(){

	int hurui[MAX + 1] = { 0 };
	int n,cnt;

	hurui[0] = 1, hurui[1] = 1;
	for (int i = 0; i <= MAX; i++){
		if (hurui[i] == 0){
			for (int j = i * 2; j <= MAX; j += i){
				hurui[j] = 1;
			}
		}
	}

	while (1){
		cin >> n;
		if (n == 0) break;

		if (hurui[n] == 0){
			cout << "0" << endl;
			continue;
		}

		while (hurui[n] == 1) n--;

		cnt = 1;
		n++;
		while (hurui[n] == 1) n++, cnt++;

		cout << cnt << endl;
	}
	return 0;
}