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;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(ll i=0;i<n;i++)
#define FOR(i,n1,n2) for(ll i=n1;i<n2;i++)
#define bFOR(i,n1,n2) for(ll i=n1;i>=n2;i--)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef pair<ll,ll> Pi;
typedef tuple<ll,ll,ll> Tu;
const int INF=(ll)(1LL<<30)-1;
const double INFd=100000000000.0;
const double PI=3.14151926535;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
const ll tMOD=(ll)998244353;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int mdx[8]={0,1,0,-1,1,1,-1,-1},mdy[8]={-1,0,1,0,1,-1,1,-1};


template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int pri[1300000]={};
int pril[1300000];
int prih[1300000];
vector<ll> prime;
int main(){
    prime.push_back(2);
    pri[2]=1;
    for(int i=3;i<1300000;i++){
        int flag=1;
        for(int j=0;j<prime.size()&&prime[j]*prime[j]<=i;j++){
            if(i%prime[j]==0){
                flag=0;break;
            }
        }
        if(flag){
            prime.push_back(i);pri[i]=1;
        }
    }
    int t=2;
    for(int i=2;i<1300000;i++){
        if(pri[i])t=i;
        pril[i]=t;
    }
    for(int i=1299999;i>=2;i--){
        if(pri[i])t=i;
        prih[i]=t;
    }
    while(true){
        int n;
        cin>>n;
        if(n==0)break;
        cout<<prih[n]-pril[n]<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;
using namespace std;
int mod = 1000000007;

int main(void){
    vector<int> prime(2000000, 1);
    prime[0] = 0;
    prime[1] = 0;
    for(int i = 2; i < 1500; i++){
        for(int j = (i << 1); j < 2000000; j+=i){
            prime[j] = 0;
        }
    }

    int N;
    while(true){
        cin >> N;
        if(N == 0) break;
        int l = N, r = N;
        while(prime[l] == 0) l--;
        while(prime[r] == 0) r++;
        cout << r-l << endl;
    }
    return 0;
}

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

int table[Number]={0,0};

int main(){
  int n;
  for(int i=2;i<Number;i++)
    table[i]=1;
  for(int i=2;i<Number;i++){
    if(table[i]){
      for(int j=i+i;j<Number;j+=i){
	table[j]=0;
      }
    }
  }
  while(cin>>n,n){
    if(table[n]){
      cout<<0<<endl;
    }else{
      int lower=n;
      int higher=n;
      while(!table[--lower])
	;
      while(!table[++higher])
	;
      cout<<higher-lower<<endl;

    }

  }
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define MAX 1299710
int main (){
	bool pr[MAX];
	int a;
	vector <int> prl;
	for(int i=0;i<MAX;i++) pr[i]=true;
	pr[0]=false;
	pr[1]=false;
	
	for(int i=0;i*i<MAX;i++){
		if(pr[i]){
			for(int j=i*i;j<=MAX;j=j+i){
				pr[j]=false;
			}
		}
	}
	for(int i=0,j=0;i<100000;j++){
	    if(pr[j]==true){
	        prl.push_back(j);
	        i++;
	    }
	}
	while(cin>>a){
		if(a==0)break;
		if(pr[a]){
		    cout << 0 <<endl;
		}
		else{
		    auto ansitr=lower_bound(prl.begin(),prl.end(),a);
		    cout<< *ansitr - *(ansitr-1) <<endl;
		}
	}
}

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

int main() {
	vector<bool>hantei(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 c = 0; c < 100; c++) {
		if (!hantei[c])cout << c << endl;
	}
}

C++: #include<iostream>
using namespace std;
int primes[400003];
bool is(int n){
	for(int i=2;i*i<=n;i++)if(n%i==0)return false;
	return true;
}
int main(){
	int cnt=0,ii=2;
	while(true){
	if(cnt==400003)break;
	if(is(ii)){
	primes[cnt]=ii;
	cnt++;
	}
	ii++;
	}
	int n;
	while(cin>>n){
	int a=0,b=0;
	bool f=0,f2=0;
	for(int i=0;i<400003;i++){
	if(primes[i]<n)a=primes[i];
	if(f==0&&primes[i]>n)b=primes[i],f=1;
	if(primes[i]==n)f2=1;
	}
	if(n<=1||f2)cout<<0<<endl;
	else cout<<b-a<<endl;
	}
	return 0;
}