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 <iostream>
#include <vector>

#define LIM 2000000

using namespace std;

int main(){

	bool nprime[LIM]={0};
	
	for(int i=2; i<LIM; ++i){
		
		if(!nprime[i]){
			for(int j=i+i; j<LIM; j+=i){
				nprime[j]=true;
			}
		}
	}
	
	vector<int> p;
	
	for(int i=2; i<LIM; ++i){
		if(!nprime[i]){
			p.push_back(i);
		}
	}
	
	int k;
	
	while(cin >> k, k){
		
		vector<int>::iterator it = lower_bound(p.begin(), p.end(), k);
		
		if(*it==k)
			cout << 0 << endl;
		else
			cout << *it - *(it-1) << endl;
	}
	
	return 0;
}

C++: #include <iostream>
using namespace std;

const int MAX_N=1299709;
bool prime[MAX_N+1];

void sieve()
{
  prime[0]=prime[1]=false;
  for(int i=2;i<=MAX_N;i++)
    prime[i]=true;
  for(int i=2;i<=MAX_N;i++)
    if(prime[i])
      for(int j=i*2;j<=MAX_N;j+=i)
	prime[j]=false;
}

int is_prime(int n)
{
  return prime[n];
}

int main()
{
  int n,sum;

  sieve();
  for(;;){
    cin>>n;
    if(!n) break;

    sum=0;
    for(int i=n;;i++){
      if(is_prime(i)) break;
      sum++;
    }
    for(int i=n;;i--){
      if(is_prime(i)) break;
      sum++;
    }
    cout<<sum<<endl;
  }
}

C++: #include<bits/stdc++.h>
#define endl '\n';
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double INFD = 1e100;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<int> era(10'000'000,0);
    era[0] = era[1] = 1;
    for(int i=2; i<10'000'000; i++){
        if(!era[i]){
            for(int j=i+i; j<10'000'000; j+=i){
                era[j] = 1;
            }
        }
    }
    vector<int> sosu;
    for(int i=0; i<10'000'000; i++){
        if(!era[i]) sosu.push_back(i);
    }
    int N;
    cin >> N;
    while(N != 0){
        if(!era[N]){
            cout << 0 << endl;
        }else{
            int pos = lower_bound(sosu.begin(),sosu.end(),N) - sosu.begin();
            cout << sosu[pos] - sosu[pos-1] << endl;
        }
        cin >> N;
    }
    return 0;
}

C++: #include <iostream>
#define REP(i,n) for (int i=0;i<(n);i++)

using namespace std;

const int N = 1e7;
int p[N];
void Eratosthenes(){
	fill(p,p + N,1);
	p[0] = p[1] = 0;
	for(int i = 2; i*i <= N; i++)
		if(p[i] == 1)
			for(int j = i*i; j <= N; j+=i)
				p[j] = 0;
}



int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
	Eratosthenes();
	int a;
	while(cin >> a,a){
		int leftp = 0,rightp = 0;
		int i;
		for(i = 0;i <= a;i++){
			if(p[i] == 0)continue;
			leftp = i;
		}
		for(;;i++){
			if(p[i] == 1){
				rightp = i;
				break;
			}
		}
		if(leftp == a)cout << 0 << endl;
		else cout << rightp - leftp << endl;
	}
  return 0;
}

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

//repetition
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)

//container util
#define all(x) (x).begin(),(x).end()

//typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

//const value
//const ll MOD = 1e9 + 7;
//const int dx[] = {0,1,0,-1};//{0,0,1,1,1,-1,-1,-1};
//const int dy[] = {1,0,-1,0};//{1,-1,0,1,-1,0,1,-1};

//conversion
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
inline ll toLL(string s) {ll v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
VLL p;
bool prime[2000000];
void makePrime(){

  memset(prime,true,sizeof(prime));
  prime[0] = false;
  prime[1] = false;
  for(int i = 2; i * i <2000000; i++){
    if(prime[i] == false) continue;
    ll x = 2;
    while(x * i < 2000000){
      prime[i*x] = false;
      x++;
    }
  }
  rep(i,2000000){
    if(prime[i]) p.push_back(i);
  }
}
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  makePrime();
  while(true){
    cin >> n;
    if(n == 0) break;
    if(prime[n]){
      cout << 0 << endl;
      continue;
    }
    //cerr << "check" << *lower_bound(all(p),n) << endl;
    ll  ans =  *lower_bound(all(p),n) - *(lower_bound(all(p),n)-1);
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
using namespace std;
#define MAX 1500000

bool prime[MAX];

void is_prime(){
    fill(prime,prime+MAX,true);
    prime[0] = prime[1] = false;
    
    for(int i = 2 ; i*i < MAX ; i++)
        if(prime[i])
            for(int j = 2*i ; j < MAX ; j += i)
                prime[j] = false;
}

int main(){
    int n;
    is_prime();
    
    while(cin >> n,n){
        int a,i;
        
        if(prime[n]) cout << '0' << endl;
        else{
            for(i = n ; prime[i] != true ; i++);
            a = i;
            for(i = n ; prime[i] != true ; i--);
            cout << a-i << endl;
        }
    }
    
    return 0;
}