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 <algorithm>
#include <cstring>
#include <vector>

using namespace std;

bool isprime[1300001];
vector<int> primes;

int main(){
    memset(isprime,true,sizeof(isprime));

    for(int i=2;i<=1300000;++i){
        if(isprime[i]){
            primes.push_back(i);
            for(int j=2;i*j<=1300000;j++){
                isprime[i*j] = false;
            }
        }
    }

    int n;
    while(cin >> n && n){
        if(find(primes.begin(), primes.end(), n) != primes.end()){
            cout << 0 << endl;
        }else{
            vector<int>::iterator it = upper_bound(primes.begin(), primes.end(), n);
            cout << *it - *(it-1) << endl;
        }
    }
}

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

int isPrime[MAX];

void eratos() {
  isPrime[0] = isPrime[1] = true;
  for (int i = 2; i * i <= MAX - 1; i++) {
    for (int j = i + i; j < MAX; j += i) {
      isPrime[j] = false;
    }
  }
}

void init() {
  for (int i = 0; i < MAX; i++) {
    isPrime[i] = true;
  }
}

int main() {
  init();
  eratos();

  while (true) {
    int n;
    cin >> n;
    if (n == 0) return 0;
    
    if (isPrime[n] == true) {
      cout << 0 << endl;
      continue;
    }
    int prev, next;
    for (int i = n - 1; ; i--) {
      if (isPrime[i] == true) {
	prev = i;
	break;
      }
    }
    for (int i = n + 1; ; i++) {
      if (isPrime[i] == true) {
	next = i;
	break;
      }
    }
    cout << next - prev << endl;
  }
}

C++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>

using namespace std;

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

int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};

static const double EPS = 1e-8;

bool isPrime[1299709+1];

int main(){

	memset(isPrime,-1,sizeof(isPrime));
	isPrime[0] = false;
	isPrime[1] = false;

	vector<int> prime;
	for(int i=2;i<=1299709;i++){
		if(isPrime[i]){
			prime.push_back(i);
			for(int j=i+i;j<=1299709;j+=i){
				isPrime[j] = false;
			}
		}
	}
	int n;

	while(~scanf("%d",&n)){
		if(n==0) break;
		if(isPrime[n]){
			printf("%d\n",0);
		}

		else{
			vector<int>::iterator it = lower_bound(prime.begin(),prime.end(),n);
			printf("%d\n",*it - *(it-1));
		}
	}
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )

#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

// 素数判定 O(√N)
bool isPrime( ULL n )
{
	if ( n < 2 )
	{
		return false;
	}

	for ( ULL i = 2; i * i <= n; ++i )
	{
		if ( !( n % i ) )
		{
			return false;
		}
	}

	return true;
}

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	for ( int k; cin >> k, k; )
	{
		int lb, ub;

		for ( lb = k; !isPrime( lb ); lb-- );
		for ( ub = k; !isPrime( ub ); ub++ );

		cout << ub - lb << endl;
	}

	return 0;
}

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

bool is_prime[1500100];
vector<int> primes;

void calc_prime(int n){
	for(int i = 0; i <= n; ++i)
		is_prime[i] = true;
	is_prime[0] = is_prime[1] = false;
	for(int i = 2; i <= n; ++i){
		if(is_prime[i]){
			primes.push_back(i);
			for(int j = i * 2; j <= n; j += i)
				is_prime[j] = false;
		}
	}
	return;
}

int main() {
	calc_prime(1300000);
	int n;
	while(cin >> n && n){
		auto it = lower_bound(primes.begin(), primes.end(), n);
		if(*it == n){
			cout << "0" << endl;
		}
		else{
			cout << *it - *(it - 1) << endl;
		}
	}
	return 0;
}

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

const int N = 1299800;

bool prime[N];

void makePrime(){
    for(int i = 2; i < N; i++) prime[i] = true;
    for(int i = 2; (i * i) < N; i++){
        if(!prime[i]) continue;
        for(int j = i + i; j < N; j += i){
            prime[j] = false;
        }
    }
}

int main(){
    makePrime();
    int n;
    while(cin >>n){
        if(n == 0) break;
        if(prime[n]){
            cout <<'0' <<endl;
            continue;
        }
        int top = n, tail = n;
        while(!prime[top]) top--;
        while(!prime[tail]) tail++;
        cout <<tail - top <<endl;
    }
    return 0;
}