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>
using namespace std;
int prime[1000];
int main(){
	int count=0;
	int i=2;
	while(true){
		bool t=true;
		for(int j=0;j<count;j++){
			if(i%prime[j]==0)
				t=false;
		}
		if(t==true){
			prime[count]=i;
			count++;
		}
		i++;
		if(i==1000)
			break;
	}
	int n;
	while(true){
	cin>>n;
	if(n==0)break;
	int bef,aft;
	bef=n;
	while(true){
		bool t=true;
		for(int i=0;i<count;i++){
			if(bef%prime[i]==0&&bef!=prime[i])
				t=false;
		}
		if(t==true)
			break;
		bef--;
	}
	aft=n;
	while(true){
		bool t=true;
		for(int i=0;i<count;i++){
			if(aft%prime[i]==0&&aft!=prime[i])
				t=false;
		}
		if(t==true)
			break;
		aft++;
	}
	cout<<aft-bef<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main() {
  int M = 1299710, n, l, r;
  vector<bool> P(M, true);
  for (int i = 2; i*i < M; ++i) {
    if (P[i]) {
      for (int j = i; i*j < M; ++j) {
	P[i*j] = false;
      }
    }
  }
  while (cin >> n, n) {
    r = n;
    l = n;
    while (!P[r]) ++r;
    while (!P[l]) --l;
    cout << r - l << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int SIZE=2000000;
bool is_prime[SIZE];
int main() {
    memset(is_prime,-1,sizeof(is_prime));
    for(int i=2;i*i<SIZE;i++) {
        if(!is_prime[i]) continue;
        for(int j=i*2;j<SIZE;j+=i) is_prime[j]=0;
    }
    vector<int> prime;
    for(int i=2;i<SIZE;i++) if(is_prime[i]) prime.push_back(i);
    int n;
    while(scanf("%d",&n),n) {
        int pos=lower_bound(prime.begin(),prime.end(),n)-prime.begin();
        if(prime[pos]==n) {printf("0\n");continue;}
        printf("%d\n",prime[pos]-prime[pos-1]);
    }
}

C++: #include <iostream>
using namespace std;
//#define max 1000000;

int prime[1000000];

void mk_prime(){
    for( int i = 0; i < 1000000; i++){
        prime[i] = 1;
    }
    prime[0] = 0;
    prime[1] = 0;
    for( int i = 2; i < 1000000; i++ ){
        if( prime[i] == 1 ){
            for( int j = 2 * i; j < 1000000; j += i ){
                prime[j] = 0;
            }
        }
    }
}

int main(){
    mk_prime();
    int k;

    while( cin >> k && k ){
        if( prime[k] ){
            cout << 0 << endl;
            continue;
        }
        int s, e;
        for( int i = k - 1; 1 ; i-- ){
            if( prime[i] ){
                s = i;
                break;
            }
        }
        for( int i = k + 1; 1 ; i++ ){
            if( prime[i] ){
                e = i;
                break;
            }
        }
        cout << e - s << endl;
    }
}

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

const int MAX_N = 1500000;

vector<int> prime;
bool is_prime[MAX_N+1];

void siege(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]) {
            prime.push_back(i);
            for (int j = 2*i; j <= n; j += i) is_prime[j] = false;
        }
    }
}

int main(void) {

    siege(MAX_N);
    int N;
    while (cin >> N, N) {
        if (is_prime[N]) {
            cout << 0 << endl;
        } else {
            int idx = lower_bound(prime.begin(), prime.end(), N) - prime.begin();
            cout << prime[idx]-prime[idx-1] << endl;
        }
    }

    return 0;
}

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

vector<bool> prime(1300000, true);

int main(){
    prime[0]=prime[1]=false;
    for(int i=2; i*i<1300000; i++){
        if(prime[i]){
            for(int j=2; i*j<1300000; j++){
                prime[i*j] = false;
            }
        }
    }
    
    while(1){
        int n;
        cin >> n;
        if(n==0) break;

        int min=n,max=n;
        while(!prime[min]) min--;
        while(!prime[max]) max++;
        cout << max-min << endl;
    }
    return 0;
}

C++: #include<iostream>

using namespace std;

bool is_prime(int n){
	if(n<=1)return false;
	for(int i=2;i*i<=n;i++){
		if(n%i==0)return false;
	}
	return true;
}

int main(){
	int n;
	while(cin>>n && n){
		if(is_prime(n)){
			cout<<0<<endl;
			continue;
		}
		else{
			int ans=1;
			for(int i=n+1;!is_prime(i);i++)ans++;
			cout<<ans<<" ";
			for(int i=n-1;!is_prime(i)&&i>=0;i--)ans++;
			cout<<ans+1<<endl;
		}
	}
}

C++: #include<cstdio>
#include<cstring>

char prime[1300000];

int main(void) {
    memset(prime, 1, sizeof(prime));

    prime[0] = prime[1] = 0;
    for(int i=0; i<1300000; i++) {
        if(!prime[i]) continue;
        for(int j=2; i*j<1300000; j++) {
            prime[i*j] = 0;
        }
    }

    while(true) {
        int n,lb,ub;
        scanf("%d", &n);
        if(!n) break;

        lb = ub = n;
        while(!prime[ub++]);
        while(!prime[lb--]);
        printf("%d\n",ub-lb-2);
    }
    return 0;
}

C++: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

bool prime[2000000];
int gap[1300000];

int main(){
	rep(i,2000000)prime[i]=true;
	prime[0]=prime[1]=false;
	loop(i,2,10000)for(int j=i*2;j<2000000;j+=i)prime[j]=false;
	rep(i,1300000)gap[i]=-1;
	int pre=2;
	loop(i,3,1300000){
		if(prime[i]){
			gap[i]=i-pre;
			pre=i;
		}
	}
	for(int i=1300000-1;i>=2;i--){
		if(gap[i]==-1)gap[i]=pre;
		else{
			pre=gap[i];
			gap[i]=0;
		}
	}
	gap[2]=gap[1]=gap[0]=0;
	int k;
	while(cin>>k,k)cout<<gap[k]<<endl;
	return 0;
}