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>

#define REP(i, n) for(int i = 0; i < (int) (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 1e9

using namespace std;

vector<int> prime;

void make_prime(){
  bool is_prime[2000000];
  REP(i, 2000000) is_prime[i] = true;
  REP(i, 2000000) {
    if(i == 0 or i == 1) is_prime[i] = false;
    if(!is_prime[i]) continue;
    REP(j, 2000000){
      if(j == 0 or j == 1) continue;
      if(i * j > 2000000) break;
      is_prime[i * j] = false;
    }
  }
  REP(i, 2000000) if(is_prime[i]) prime.push_back(i);
}

int main(){
  int k;
  make_prime();
  while(cin >> k and k){
    int res = 0;
    REP(i, prime.size()) {
      if(prime[i] < k) continue;
      if(prime[i] == k) { res = 0; break; }
      res = prime[i] - prime[i-1];
      break;
    }
    cout << res << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int PN[1000000];
bool isPrime[1300000];
int Index=0;
void Init(){
  for(int i=0;i<1300000;i++){
    isPrime[i] = false;
  }
  for(int i=3;i<1300000;i+=2)isPrime[i] = true;
  isPrime[2] = true;

  int lim = sqrt(1300000.0)+1;
  for(int i=3;i<=lim;i+=2){
    if(!isPrime[i])continue;
    for(int j=i+i;j<=1300000;j+=i){
      isPrime[j] = false;
    }
  }

  for(int i=2;i<1300000;i++){
    if(isPrime[i])PN[Index++] = i;
  }
}

int main(){
  int k;
  Init();
  while(cin >> k && k){
    if(isPrime[k]){
      cout << "0" << endl;
      continue;
    }
    int A = *(lower_bound(PN,PN+Index,k)-1);
    int B = *(upper_bound(PN,PN+Index,k));
    cout << B-A << endl;

  }
  return 0;
}

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

int pmem[1299709];
vector<int> prime;
void init()
{
  for(int i = 2; i < 1299709; i++)
  {
    if(pmem[i])
      continue;
    pmem[i] = 1;
    prime.push_back(i);
    for(int j = i + i; j < 1299709; j += i)
      pmem[j] = -1;
  }
}

int main()
{
loop:
  init();
  int n;
  cin >> n;
  if(!n)
    return 0;
  for(int i = 0; i < prime.size(); i++)
  {
    if(prime[i] == n)
    {
      cout << 0 << endl;
      goto loop;
    }
    if(prime[i] > n)
    {
      cout << prime[i] - prime[i - 1] << endl;
      goto loop;
    }
  }
}

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

int main() {
bool a[1300000]={0};
a[1]=1;
for(int i=2;i<650001;i++){
	if(a[i]==1) continue;
	int j=2*i;
	while(j<1300000){
		a[j]=1;
		j=j+i;
	}
}
int k;
while(cin>>k && k){
int ans;
if(a[k]==0) ans=0; 
else{
	int l=k+1,m=k-1;
	while(a[l]==0) l++;
	while(a[m]==0)  m--;
	ans=l-m;
}
cout<<ans<<endl;

}



	return 0;
}

C++: /*g++ main.cpp -o main.out*/
/*./main.out*/

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define LL long long
#define LD long double
#define Mod 1000000007
#define L_Mod 17100000013

const double eps = 1e-9;
const int INF = 0x3f3f3f3f;
const double PI = 3.1415926535;

#define NO_TLE std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define ree(a,n) memset(a,n,sizeof(a));

#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define red(i,l,u) for(int (i)=(int)(l);(i)>=(int)(u);--(i))

const int maxn=1299710;
bool u[maxn];
int ans[maxn];
void prepare()
{
    int i,j,k;
    for(i=2; i<maxn; i++)
        u[i]=1;
    for(i=2; i<maxn; i++)
        if(u[i])
            for(j=2; j*i<maxn; j++)
                u[i*j]=0;
    for(i=2; i<maxn; i++)
        if(!u[i])
        {
            j=i;
            while(j<maxn&&!u[j])
                j++;
            j--;
            for(k=i; k<=j; k++)
                ans[k]=j-i+2;
            i=j;
        }
        else
            ans[i]=0;
}
int main ()
{
    int i,j,k;
    prepare();
    while(scanf("%d",&k)!=EOF&&k)
    {
        printf("%d\n",ans[k]);
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int N;
bool isPrime[1299710];

void primes() {
	fill(isPrime, isPrime + 1299710, 1);
	for (int i = 2; i < 1299710; i++) {
		if (isPrime[i]) {
			for (int j = 2 * i; j < 1299710; j += i) isPrime[j] = false;
		}
	}
}

void solve() {
	int l = N, r = N;
	while (!isPrime[l]) { l--; }
	while (!isPrime[r]) { r++; }
	cout << r - l << endl;
}

int main() {
	primes();
	
	while (1) {
		cin >> N; if (N == 0) break;
		solve();
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>

#define loop(n, i) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;


int main()
{
    int max = 1299710;
    vector<int> mat(max, 1);
    vector<int> primes;
    for (int i=2; i<sqrt(max); i++) {
        if (mat[i]) {
            for (int j=i*2; j < max; j+=i) mat[j] = 0;
        }
    }
    for (int i=2; i<max; i++) {
        if (mat[i]) primes.push_back(i);
    }

    int k;
    while (cin >> k, k) {
        loop(primes.size(),i) {
            if (k == primes[i]) {
                cout << 0 << endl;
                break;
            } else if (k < primes[i]) {
                cout << primes[i]-primes[i-1] << endl;
                break;
            }
        }
    }
}