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++: // Enjoy your stay.

#include <bits/stdc++.h>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

typedef istringstream iss;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;

int isp[1300000];

int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	
	fill(isp+2,isp+1300000,1);
	rep2(i,2,1300000)if(isp[i]){
		for(int j=i*2;j<1300000;j+=i)isp[j]=0;
	}
	int x;
	while(cin>>x && x){
		if(isp[x])cout<<0<<endl;
		else{
			int l=x,r=x;
			while(!isp[l])l--;
			while(!isp[r])r++;
			cout<<r-l<<endl;
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=1300005,INF=1<<29;

vector<int> prime;//i番目の素数
bool is_prime[MAX+1];

void sieve(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(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    sieve(1300002);
    
    while(1){
        int N;cin>>N;
        if(N==0) break;
        if(is_prime[N]){
            cout<<0<<endl;
            continue;
        }
        
        auto it=lower_bound(all(prime),N),it2=it;
        it2--;
        cout<<*it-*it2<<endl;
    }
}

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

constexpr int lim = 2e6;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  vector<bool> prime(lim + 1, true);
  prime[0] = prime[1] = false;
  for (int p = 2; p * p <= lim; ++p) {
    if (prime[p]) {
      for (int i = 2 * p; i <= lim; i += p) {
        prime[i] = false;
      }
    }
  }
  int n;
  while (cin >> n, n) {
    if (prime[n]) {
      cout << "0\n";
    } else {
      int l = n, r = n;
      while (not prime[l - 1]) {
        --l;
      }
      while (not prime[r + 1]) {
        ++r;
      }
      cout << r - l + 2 << '\n';
    }
  }
}

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

using namespace std;

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

const int MAX = 1299809;
bool is_prime[MAX];
int n;


void solve() {
    int ans=0;
    int m=n;
    while(!is_prime[m]) {
        m--;
        ans++;
    }
    while(!is_prime[n]) {
        n++;
        ans++;
    }
    cout<<ans<<endl;
}

int main() {
    fill(is_prime,is_prime+MAX,true);
    is_prime[0]=is_prime[1]=false;
    for(int i=2;i<MAX;i++) {
        if(is_prime[i]) for(int j=i+i;j<MAX;j+=i) is_prime[j]=false;
    }

    while(cin>>n) {
        if(!n) return 0;
        solve();
    }

    return 0;
}

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

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

int MOD=1e9+7;
ll INF=1e18;

int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};

bool is_prime[1300000];
int prime[1300000];
int ans[1300000];

void eratos(int n){
    for(int i=0; i<=n; i++) is_prime[i]=true;
    is_prime[0]=false, is_prime[1]=false;

    for(int i=2; i*i<=n; i++){
        if(is_prime[i]){
            for(int j=2; i*j<=n; j++){
                is_prime[i*j]=false;
            }
        }
    }

    int cnt=1;
    for(int i=1; i<=n; i++){
        if(is_prime[i]){
            prime[cnt]=i;
            cnt++;
        }
    }

    cnt=1;
    for(int i=2; i<=n; i++){
        if(i>prime[cnt+1]) cnt++;

        if(i<=prime[cnt+1]){
            ans[i]=prime[cnt+1]-prime[cnt];
        }
    }
}

int main(){
    eratos(1299800);
    while(1){
        int n; cin>>n;
        if(n==0) return 0;
        if(n==1 || is_prime[n]) cout << 0 << endl;
        else cout << ans[n] << endl;
    }
}

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

int main(void){
    int i,j,n,buf[1300000]={0},pr[100000]={0};
    for(i=2;i<1300000;i++){
        if((i==2 || i==3 || i==5)||(i%2 && i%3 && i%5)){buf[i]=i;}
    }
    for(i=0;i<sqrt(1300000);i++){
        if(!(buf[i])){continue;}
        for(j=i+1;j<1300000;j++){
            if(!(buf[j]%buf[i])){buf[j]=0;}
        }
    }
    j=0;
    for(i=0;i<1300000;i++){
        if(buf[i]){pr[j]=buf[i];j++;}
    }
    
    while(1){
        scanf("%d",&n);
        if(!n){break;}
        auto tg=lower_bound(pr,pr+100000,n);
        if(n==*tg){printf("0\n");}
        else{printf("%d\n",*tg-*(tg-1));}
    }        
    return 0;
}