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> // only for GCC

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

//container util
//------------------------------------------
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

//repetition
//------------------------------------------
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

bool p[1299710];

void erato(){
    fill(p, p+1299710, true);
    p[0] = p[1] = false;
    rep(i, 1299710){
        if(!p[i]) continue;
        for(int j=i+i; j < 1299710; j+=i){
            p[j] = false;
        }
    }
}

int main(){
    long long n;
    erato();
    while(cin >> n, n){
        int ans = 0;
        for(long long i = n; !p[i]; i--) ans++;
        if(!p[n]) for(long long i = n+1; !p[i]; i++) ans++;
        if(ans != 0) cout << ans+1 << endl;
        else cout << 0 << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<string>
#define SIZE 1299710
using namespace std;

int main() {
	bool *hantei;
	hantei = new bool[SIZE] {};
	hantei[0] = hantei[1] = true;
	for (int a = 2; a < SIZE; a++) {
		if (!hantei[a]) {
			for (int b = a * 2; b < SIZE; b += a)hantei[b] = true;
		}
	}
	for (int g = 0; g < 100; g++) {
		if (!hantei[g])cout << g << endl;
	}
	int c;
	while (cin >> c, c) {
		if (c == 1 || !hantei[c]) { cout << 0 << endl; break; }
		int d = 1;
		for (int e = c + 1;; e++) {
			if (!hantei[e])break;
			d++;
		}
		for (int f = c - 1;; f--) {
			if (!hantei[f])break;
			d++;
		}
		cout << d+1<< endl;
	}
}

C++: #include<iostream>
int p(int x){for(int i=2;i*i<=x;i++)if(x%i==0)return 0;return 1;}
main() {
  int n;
  while(std::cin>>n,n) {
    int a=0;
    for(int s=-1;s<2;s+=2)for(int i=0;!p(n+i*s);i++,a++);
    std::cout<<a<<'\n';
  }
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <complex>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <stdio.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define MT(a,b,c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define EPS 1.0e-9

char bits[1500000];
LL primes[750000], n_primes; 

void sieve(){
	int i, j;
	memset(bits, 1, 1500000);
	bits[0] = bits[1] = 0;
	for (i = 0; i < sqrt(1500000.0)+1; i++)
		if (bits[i])
			for (j = i * i; j < 1500000; j += i)
				bits[j] = 0;
	for (i = j = 0; i < 100000; i++)	
		if (bits[i]) primes[j++] = i;
	n_primes = j;
}
int main(){
	sieve();
	int n;
	while(cin>>n, n){
		if(bits[n]==1)cout << 0 << endl;
		else{
			int ret = 0;
			int tmp = n;
			while(!bits[tmp]){
				ret++;
				tmp--;
			}
			tmp = n;
			while(!bits[tmp]){
				ret++;
				tmp++;
			}
			cout << ret << endl;
		}
	}
	return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j,k,l;
  int a[100000];
  a[0]=2;
  k=1;
  for(i=3;k<100000;i+=2){
    l=(int)sqrt(i)+1;
    for(j=3;j<l;j+=2){
      if(i%j==0)
	break;
    }
    if(j>=l){
      a[k]=i;
      k++;
    }
  }
  int n;
  while(cin>>n&&n){
    for(i=0;a[i]<n;i++);
    if(a[i]==n)
      cout<<0<<endl;
    else
      cout<<a[i]-a[i-1]<<endl;
  }
  return 0;
}