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 <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <cmath>
#include <algorithm>
#include <map>
#include <list>

using namespace std;

#define FOR( i, k, n ) for ( int i = ( k ); i <= ( int )( n ); ++i )
#define REP( i, n ) FOR( i, 1, n )

typedef long long ll;
typedef pair< int, int > P;

#define MAX_N 1300000

bool is_prime[ MAX_N + 1 ];

int main()
{
	// エラトステネス
	for( int i = 3; i <= MAX_N; i += 2 ) is_prime[ i ] = true;
	is_prime[ 2 ] = true;
	for( int i = 3; i * i <= MAX_N; i += 2 ) {
		if( !is_prime[ i ] ) continue;
		for( int j = i * 2; j <= MAX_N; j += i ) is_prime[ j ] = false;
	}

	int n;
	while( cin >> n, n ) {
		if( is_prime[ n ] ) { cout << 0 << endl;  continue; }
		int left, right;
		for( left = n; left >= 1; --left ) if( is_prime[ left ] ) break;
		for( right = n; right <= MAX_N; ++right ) if( is_prime[ right ] ) break;
		cout << right - left << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
#ifdef DEBUG
#define line() cerr << "[" << __LINE__ << "] ";
#define dump(i) cerr << #i ": " << i << " ";
#define dumpl(i) cerr << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;

bool has(vector<string> &v, string s)
{

    return find(ALL(v), s) != v.end();
}

void show(vector<string> &v)
{
    rep(i, v.size())
    {
        cerr << v[i] << " ";
    }
    cerr << endl;
}

bool isprime(ll n)
{
    if (n == 2)
        return true;
    if (n == 1)
        return false;
    ll m = 1;
    while (m * m < n)
    {
        m++;
    }
    rep(i, 2, m + 1)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

void init(vector<int> &v)
{
    v[0] = 2;
    int n = 3;
    int i = 1;
    while (i <= 100010)
    {
        cerr << i << endl;
        bool f = true;
        rep(j, i)
        {
            if (n % v[j] == 0)
            {
                f = false;
                break;
            }
        }
        if (f)
        {
            v[i] = n;
            i++;
        }
        n++;
    }
}

int main()
{
    vector<int> v(100010, 0);
    while (true)
    {
        int n;
        cin >> n;
        if (n == 0)
        {
            break;
        }
        ll low = n, up = n;

        while (!isprime(low))
            low--;
        while (!isprime(up))
            up++;
        cout << up - low << endl;
    }
    return 0;
}

C++: #include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
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 vector<int>vi;
typedef vector<vi> vvi;
typedef vector<string>vs;
typedef pair<int,int> pii;
typedef long long ll;
#define ALL(A) (A).begin(),(A).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin();i!=(c).end();++i)
const double EPS=1e-10;
const double PI= acos(-1.0);

#define MAX_K 1299710

int main(){
	int k;
	vi prime(MAX_K, true);
	prime[1]=false;
	FOR(i, 2, MAX_K) {
		if(prime[i]) {
			for(int j=2*i; j < MAX_K; j += i) {
				prime[j] = false;
			}
		}
	}

	while(cin >> k, k) {
		int p_left = 0, p_right = 0;
		FOR(i, 1, k+1) {
			if(prime[i]) {
				p_left = i;
			}
		}
		FOR(i, k, MAX_K) {
			if(prime[i]) {
				p_right = i;
				break;
			}
		}
		cout << max(p_right - p_left, 0) << endl;
	}
}

C++: #include <iostream>
using namespace std;
bool isPrime(int x) { for(int i=2; i*i<=x; i++) if(x%i==0) return false; return true; }
int main() {
  int n;
  while(cin >> n, n) {
    int ans = 0;
    for(int i=0; !isPrime(n+i); i++, ans++);
    for(int i=0; !isPrime(n-i); i++, ans++);
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int a[1299710]={};
  a[2]=1;
  int fl;
  for(int i=3;i<1299710;i++){
    fl=0;
    for(int j=2;j*j<=i;j++){
      if(i%j==0){
	fl=1;
	break;
      }
    }
    if(fl==0){
      a[i]=1;
    }
  }
  int n,h,t;
  while(1){
    cin >> n;
    if(n==0)break;
    for(int i=n;i>=0;i--){
      if(a[i]==1){
	h=i;
	break;
      }
    }
    for(int i=n;i<=1299709;i++){
      if(a[i]==1){
	t=i;
	break;
      }
    }
    cout << t-h<< endl;
  }
  return 0;
}