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>
#include <algorithm>
using namespace std;
bool is_prime[1500000];

int main(){
  int n, m = 1500000;
  fill(is_prime,is_prime+m,true);
  vector<int> P;
  for(int i = 2; i*i <= m; ++i){
    if(!is_prime[i]) continue;
    for(int j = i; i*j <= m; ++j){
      is_prime[i*j] = false;
    }
  }
  for(int i = 2; i < m; ++i)
    if(is_prime[i])
      P.push_back(i);
  while(cin >> n, n){
    if(is_prime[n]){
      cout << 0 << endl;
      continue;
    }
    int idx = upper_bound(P.begin(), P.end(), n) - P.begin();
    cout << P[idx] - P[idx-1] << endl;
  }
}

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

const int MX = 1500000;
bool sieve[MX];

int main() {
    memset(sieve, false, sizeof sieve);
    sieve[0] = sieve[1] = true;
    for (int i=2; i*i<MX; ++i) {
        if ( ! sieve[i]) {
            for (int j=i*i; j<MX; j+=i) {
                sieve[j] = true;
            }
        }
    }
    int n;
    while (cin >> n, n) {
        int l, h;
        for (int i=n; ; ++i) {
            if ( ! sieve[i]) {
                h = i; break;
            }
        }
        for (int i=n; ; --i) {
            if ( ! sieve[i]) {
                l = i; break;
            }
        }
        cout << h-l << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;

const int LIM = 1300000;

char isprime[LIM];

int prime[LIM];
int lim = 0;

void eratosthenes() {
    fill(isprime, isprime + LIM, true);
    isprime[0] = isprime[1] = false;
    for(int x = 2; x < LIM; ++x) {
        if(isprime[x]) {
            prime[lim++] = x;
            if(x > sqrt(LIM)) continue;
            for(int y = x * x; y < LIM; y += x) {
                isprime[y] = false;
            }
        }
    }
}

int main(){
    eratosthenes();
    int N;
    while(true) {
        scanf("%d", &N);
        if(N == 0) break;
        int res = 0;
        if(!isprime[N] && N > 2) {
            int* left = lower_bound(prime, prime + lim, N) - 1;
            int* right = upper_bound(prime, prime + lim, N);
            res = *right - *left;
        }
        printf("%d\n", res);
    }
    return 0;
}

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

using namespace std;

const int N = 1299710;

vector<bool> p;

void init(){
    p.assign(N, true); p[0] = p[1] = false;
    for(int i=2; i<=N; ++i)if(p[i]){
        for(int j=i<<1; j<=N; j+=i)p[j] = false;
    }
}

int main(){
    init();
    for(int k; cin >> k, k;){
        int lo = k, hi = k;
        while(!p[lo])lo--;
        while(!p[hi])hi++;
        cout << hi - lo << '\n';
    }
    return 0;
}

C++: #include <iostream>
#define MAX_N 1299709 + 1
using namespace std;

bool t[MAX_N];
int k;

bool isPrime(int x) {
  if(x == 2) return true;
  if(x < 2 || x % 2 == 0) return false;
  int i = 3;
  while(i * i <= x) {
    if(x % i == 0) return false;
    i += 2;
  }
  return true;
}

void table() {
  for(int i = 1; i < MAX_N; ++i) {
    if (isPrime(i)) t[i] = 1;
  }
}

void solve() {
  int p = k, n = k;
  while(t[p] != 1) p--;
  while(t[n] != 1) n++;
  cout << n - p << endl;
}

int main() {
  table();
  while(cin >> k && k) {
    solve();
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

#define intmax 1299710
int isprime[intmax];
void makeprime(){
	REP(i,intmax) if(i%2!=0) isprime[i] = true;
	isprime[1] = false;
	isprime[2] = true;
	for(ll int i=3;i<sqrt(intmax);i+=2){
		for(ll int j=i+2;j<intmax;j+=2){
			if(j%i==0) isprime[j] = false;
		}
	}
}

int main(){
	makeprime();
	int n;
	while(cin >> n,n){
		ll int a,b;
		if(!isprime[n]){
			for(int i = n+1;i<intmax;i++){
				if(isprime[i]) {
					a = i;
					break;
				}
			}
			for(int i= n-1;i>-1;i--){
				if(isprime[i]){
					b = i;
					break;
				}
			}
			cout << a-b << endl;
		}else{
			cout << 0 << endl;
		}

	}
}

C++: #include <iostream>
#define N 1200000
using namespace std;
int start(int);
int goal(int);
int sosu[N];
int main(){
  int i,j,n,s,g;
  for(i=0;i<N;i++) sosu[i]=1;
  sosu[0]=0;
  sosu[1]=0;
  for(i=0;i<N/2;i++){
    if(sosu[i]==1){
      for(j=i*2;j<N;j+=i){
	sosu[j]=0;
      }
    }
  }
  while(1){
    cin >> n;
    if(n==0) break;
    if(sosu[n]!=1){
      s=start(n);
      g=goal(n);
      cout << g-s << endl;
    }
    else cout << 0 << endl;
  }
  return 0;
}
int start(int n){
  int i,result;
  if(n%2==0) n--;
  else n-=2;
  for(i=n;i>=2;i-=2){
    if(sosu[i]==1){
      result=i;
      break;
    }
  }
  return result;
}
int goal(int n){
  int i,result;
  if(n%2==0) n++;
  else n+=2;
  for(i=n;i<N;i+=2){
    if(sosu[i]==1){
      result=i;
      break;
    }
  }
  return result;
}