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 <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#include <cstring>
#include <cstdio>
#include <stack>
#define INF 1000000000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;
vector<ll> p(1000001);

void era(){
    for (int i=2; i<1000001; i++) {
        if(p[i]==0)continue;
        int j=i;
        while (j<1000001) {
            j+=i;
            if(j>=1000001)break;
            p[j]=0;
        }
    }
}

int main(){
    rep(i, 1000001) p[i]=i;
    era();
    for(;;){
        int n;
        cin >>n;
        if(!n) break;
        ll ans = 0;
        int i=n,j=n;
        while (p[i]==0) {
            i++;
        }
        while (p[j]==0) {
            j--;
        }
        ans = p[i]-p[j];
        cout << ans <<endl;
    }
}

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[1000000];
int gap[1300000];

int main(){
	rep(i,1000000)prime[i]=true;
	prime[0]=prime[1]=false;
	loop(i,2,1000)for(int j=i*2;j<1000000;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;
}

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

vector<int> sieve(int n) {
	vector<int> res(1, 2);
	vector<bool> prime(n, true);
	for(int i = 3; i < n; i += 2) {
		if(prime[i]) {
			res.push_back(i);
			for(int j = i * 3; j < n; j += i * 2)
				prime[j] = false;
		}
	}

	return res;
}

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

	const int MAX = 1299709;
	vector<int> primes = sieve(MAX + 1);
	for(int k; cin >> k, k;) {
		int idx = lower_bound(primes.begin(), primes.end(), k) - primes.begin();
		if(primes[idx] == k)
			cout << 0 << endl;
		else
			cout << primes[idx] - primes[idx - 1] << endl;
	}

	return EXIT_SUCCESS;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,a[1299710]={1,2,0,0},c,i,j;
	for(i=4;i<1299710;i++){
		for(j=2,c=0;j*j<=i;j++)if(i%j==0)c++;
		if(c)a[i]+=a[i-1]+1;
	    else a[i]=0;
	}
	for(i=1299709;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;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,a[100001]={1,2,0,0},b[100000],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;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,a[1000001]={1,2,0,0},c,i,j;
	for(i=4;i<1000000;i++){
		for(j=2,c=0;j*j<=i;j++)if(i%j==0)c++;
		if(c)a[i]+=a[i-1]+1;
	    else a[i]=0;
	}
	for(i=999999;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;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<int> get_prime(int n) {
    if(n <= 1) return vector<int>{};
    vector<bool> is_prime(n+1, true);
    vector<int> prime;
    for (int i = 2; i <= n; ++i) {
        if(!is_prime[i]) continue;
        prime.emplace_back(i);
        for (int j = i*2; j <= n; j += i) {
            is_prime[j] = false;
        }
    }
    return prime;
}

int main() {
    const auto primes = get_prime(1300000);
    set<int> s;
    for (auto &&i : primes) s.insert(i);
    int n;
    while(cin >> n, n){
        auto ub = s.upper_bound(n);
        auto lb = ub;
        lb--;
        cout << n << "\n";
        if(*lb == n){
            puts("0");
            continue;
        }else {
            printf("%d\n", *ub-*lb);
        }
    }
    return 0;
}

C++: #include<iostream>
using namespace std;int X, Y, A, i, j;
main(){while (true){cin>>A;if(A==0){break;}
for(i=A;i>=2;i--){for(j=2;j*j<=i;j++){if(i%j==0){goto E;}}X=i;goto F;E:;}
F:;for(i=A;i<(1<<27);i++){for(j=2;j*j<=i;j++){if(i%j==0){goto G;}}Y=i;goto H;G:;}
H:; cout<<Y-X<<endl;}}

C++: #include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false

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

vector<int> prime;

int main() {
	
	vector<bool> use(1299710, false);
	REP(i, 2, 1299710){
		if(!use[i])prime.push_back(i);
		for(int j = 2; i * j <= 1299710; j++) use[i * j] = true;
	}
	
	int n;
	while(cin >> n, n){
		auto it1 = upper_bound(prime.begin(), prime.end(), n);
		auto it2 = lower_bound(prime.begin(), prime.end(), n);
		it1--;
		cout << *it2 - *it1 << endl;
	}
	
	
	return 0;
}