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>
using namespace std;
int main(){
	int n,a[100001]={1,2,0,0},c,i,j;
	for(i=4;i<100000;i++){
		for(j=2,c=0;j*j<=i;j++)if(i%j==0)c++;
		if(c)a[i]+=a[i-1]+1;
	}
	for(i=99999;i>=0;i--){
		if(a[i]){for(j=0,c=a[i];j<c;j++)a[i--]=c+1;i++;}
	}
	while(cin>>n,n)cout<<a[n]<<endl;
return 0;
}

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

const long N=1500000;

int main(){
	bool prime[N];
	
	for(long i=2; i<N; ++i) prime[i]=true;
	
	for(long i=2; i<N; ++i){
		if(prime[i])
			for(long j=2; i*j<N; ++j) prime[i*j]=false;
	}	
	
	while(1){
		long n;
		scanf(" %ld", &n);
		if(n==0) break;
		
		long a, b;
		for(long i=n; i<N; ++i){
			if(prime[i]){
				a=i;	
				break;
			}
		}
		for(long i=n; i>0; --i){
			if(prime[i]){
				b=i;	
				break;
			}
		}
		//printf("%ld-%ld=",a, b);
		printf("%ld\n", a-b);
	}
	return 0;
}

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( 1 ){
        cin >> k;
        if( k == 0 ) return 0;
        if( prime[k] ){
            cout << 0 << endl;
            continue;
        }
        int s, e;
        for( int i = k - 1; ; i-- ){
            if( prime[i] ){
                s = i;
                break;
            }
        }
        for( int i = k + 1; ; i++ ){
            if( prime[i] ){
                e = i;
                break;
            }
        }
        cout << e - s << endl;
    }
}

C++: #include<iostream>
#include <algorithm>
using namespace std;
#define MAX_LIST 100000000



int main(){
	bool *prime=(bool *)malloc(sizeof(bool)*MAX_LIST);
	//bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){ 
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	int in;
	while(cin>>in,in){
		int ans=0;
		int i=0;
		while(prime[in+i]==0){
			ans++;
			i++;
		}
		i=0;
		while(prime[in-i]==0){
			ans++;
			i++;
		}
		cout<<ans<<endl;
	}
	
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
    bool f[1000001];
    fill(f,f+1000001,true);
    f[0]=f[1]=false;
    vector<int>prime(1,1);
    for(int i=2;i<=1000000;i++){
        if(!f[i])continue;
        prime.push_back(i);
        for(int j=i+i;j<=1000000;j+=i)f[j]=false;
    }

    int n;
    while(scanf("%d",&n),n){
        int pos=lower_bound(prime.begin(),prime.end(),n)-prime.begin();
        if(prime[pos]==n){
            puts("0");
        }
        else{
            printf("%d\n",prime[pos]-prime[pos-1]);
        }
    }

    return 0;
}

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

vector<int> prime_table(int n) {
    vector<int> is_prime(n + 1);
    if (n) iota(is_prime.begin() + 2, is_prime.end(), 2);
    for (int i = 2; i * i <= n; i++) if (is_prime[i]) {
        for (int j = i + i; j <= n; j += i) is_prime[j] = 0;
    }
    return is_prime;
}

int main() {
    auto is_prime = prime_table(2e6);
    int n;
    while (cin >> n, n) {
        int prv = n, nxt = n;
        while (!is_prime[prv]) prv--;
        while (!is_prime[nxt]) nxt++;
        cout << nxt - prv << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <math.h>
using namespace std;
int main(){
	int n;
	while(cin>>n){
		if(n==0) break;
		int paf=0,pbe=0,p;
		int af=0;
		while(paf==0){
			p=n+af;
			if(p%2==0&&p!=2) goto enda;
			else if(p%3==0&&p!=3) goto enda;
			else{
				for(int i=1;i<sqrt(p)/6+3;i++){
					if(p%(6*i+1)==0&&p!=6*i+1) goto enda;
					else if(p%(6*i-1)==0&&p!=6*i-1) goto enda;
				}
			}
			paf=p;
			enda:;
			af++;
		}
		int be=0;
		while(pbe==0){
			p=n-be;
			if(p%2==0&&p!=2) goto endb;
			else if(p%3==0&&p!=3) goto endb;
			else{
				for(int i=1;i<sqrt(p)/6+3;i++){
					if(p%(6*i+1)==0&&p!=6*i+1) goto endb;
					else if(p%(6*i-1)==0&&p!=6*i-1) goto endb;
				}
			}
			pbe=p;
			endb:;
			be++;
		}
		cout<<paf-pbe<<endl;
	}
    return 0;
}

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

#define MAXP 1299709
bool prime[MAXP+1];
void init(){
	prime[2]=true;
	for(int i=3;i<=MAXP;i++)prime[i]=true;
	for(int i=3;i<=MAXP;i++){
		if(!prime[i])continue;
		for(int j=2*i;j<=MAXP;j+=i)prime[j]=false;
	}
}

int main(){
	int n;
	init();
	while(cin>>n&&n){
		int low=n,high=n;
		while(!prime[low])low--;
		while(!prime[high])high++;
		cout<<high-low<<endl;
	}
	return 0;
}

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

int p[100010], size;

bool isPrime(int x){
	for(int i=0;i<size && p[i]<=sqrt(x);i++){
		if(x % p[i] == 0) return false;
	}
	return true;
}

int main(void){
	int n;

	p[0] = 2;
	size = 1;
	for(int i=3;i<1299730;i+=2) if(isPrime(i)) p[size++] = i;

	while(cin>>n && n){
		int i;
		for(i=0;p[i]<n;i++);

		if(p[i] == n) cout<<"0\n";
		else cout<<p[i] - p[i-1]<<endl;
	}

	return 0;
}