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>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define itrep(i,x) for(auto i=(x).begin(); i!=(x).end();i++)
#define Sort(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
//typedef std::pair<int,int> P;
int D=1;
int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};

using namespace std;

bool isPrime(int x){
    if(x==1)return false;
    if(x==2)return true;
    if(x%2==0)return false;
    rep(i,3,sqrt(x)+1){
        if(x%i==0)return false;
    }
    return true;
}

int main(){
    vector<int> prime;
    prime.pb(2);
    rep(i,3,1299710){
        if(isPrime(i)){
            prime.pb(i);
        }
    }
    
    int x;
    while(cin>>x&&x){
        if(isPrime(x)){
            cout<<0<<endl;
            continue;
        }
        auto it=lower_bound(all(prime),x);
        cout<<*it-*(it-1)<<endl;
    }
    
    return 0;
}

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

vector<bool> Eratos(1299710,true);

int main()
{
	int N,min,max;
	for (int i = 2; i < 1142;) {
		for (int j = 1; j <= 1299710; j += i) {
			if (j % i == 0) {
				Eratos[j] = false;
			}
		}
		for (i++;!Eratos[i];i++);
	}
	while (cin >> N, N) {
		for (min = N; !Eratos[min]; min--);
		for (max = N; !Eratos[max]; max++);
		cout << max - min << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int isPrime[2000001];
int main(){

  fill(isPrime,isPrime + 2000001,1); 
  isPrime[0]=isPrime[1]=0;
  for(int i = 2 ; i*i < 2000001 ; i++ ){
    if(isPrime[i])for(int j = i*2 ; j < 2000001 ; j+=i )isPrime[j] = 0;
  }
  int n;
  while(cin >> n,n){
    int cnt = 0;
    int p,q;
    for(int i = n ; ; i++){
      if(isPrime[i] == 1){
	p = i;
	break;
      }
    }
    int f = 1;
    for(int i = n ; ; i--){
      if(i < 0){
	f = 0;
	break;
      }
      if(isPrime[i] == 1){
	q = i;
	break;
      }
    }
    if(f == 0)cout << 0 << endl;
    else cout << p-q << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int primes[200003];
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==200003)break;
	if(is(ii)){
	primes[cnt]=ii;
	cnt++;
	}
	ii++;
	}
	int n;
	while(cin>>n){
	int a=0,b=0;
	bool f=0;
	for(int i=0;i<200003;i++){
	if(primes[i]<=n)a=primes[i];
	if(f==0&&primes[i]>=n)b=primes[i],f=1;
	}
	if(n<=1)cout<<0<<endl;
	else cout<<b-a<<endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

#define N 1299710

int main(int argc, char const *argv[])
{
	int n;
	int p1,p2;

	bool f[N];

	for(int i1=0;i1<N;i1++) f[i1]=false;

	f[0]=true;
	f[1]=true;
	for(int i1=0;i1<N;i1++){
		if(f[i1]==false){
			for(int i2=2;i2*i1<N;i2++){
				f[i2*i1]=true;
			}
		}
	}

	while(1){
		cin>>n;
		if(n==0) break;

		p1=n;
		while(f[p1]==true) p1--;
		p2=n;
		while(f[p2]==true) p2++;

		cout<<p2-p1<<endl;

	}
	return 0;
}

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

#define MAX 1299809

int main(){

	  vector<int> pri;
	  bool is_p[MAX]={};
	  for(int i=2; i<MAX; i++) is_p[i] = true;
	  for(int i=2; i<MAX; i++){
			if( is_p[i] ) {
				  pri.push_back(i);
				  for(int j=2; i*j<MAX; j++)
						is_p[i*j] = false;
			}
	  }
	  int n;
	  while(cin >> n,n){
			
			int index = lower_bound(pri.begin(), pri.end(), n) - pri.begin();
			if( n == pri[index])
				  cout << 0 << endl;
			else
				  cout << pri[index] - pri[index-1]  << endl;
			
	  }
			
}

C++: #include <iostream>

#define MAX 1299710

bool is_prime[MAX];

int main(int argc,char* argv[]){
  
  for(int i = 2; i < MAX; i++){
    is_prime[i] = true;
  }
  is_prime[0] = is_prime[1] = false;

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

  while(true){
    int n;

    std::cin >> n;

    if(n == 0){
      break;
    }

    int low = 0, high = 0;

    for(low = n; low >= 0; low--){
      if(is_prime[low]){
	break;
      }
    }

    for(high = n; high < MAX; high++){
      if(is_prime[high]){
	break;
      }
    }
    
    std::cout << (high - low) << std::endl;
  }

  return 0;
  
}

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

#define N 11451419

int ans[N+1];
bool is_composite[N+1];
vector<int> primes;

int main() {
  is_composite[1] = true;
  for (int i=2; i<=N; i++) {
    if (!is_composite[i]) {
      primes.push_back(i);
      for (int j=i+i; j<=N; j+=i) {
        is_composite[j] = true;
      }
    }
  }

  int n = primes.size();
  for (int i=0; i<n-1; i++) {
    int a = primes[i];
    int b = primes[i+1];
    for (int j=a+1; j<b; j++) ans[j] = b-a;
  }

  while (1) {
    int k;
    scanf("%d", &k);
    if (k == 0) return 0;
    printf("%d\n", ans[k]);
  }
}

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

bool isPrime[1299710];

void prime(){
  isPrime[0]=isPrime[1]=true;
  for(int i=2;i<sqrt(MAX);i++){
    for(int j=2*i;j<MAX;j+=i)isPrime[j]=true;
  }
}

int main(){
  prime();
  int n;
  while(cin>>n,n){
    int forward,back;
    forward=back=n;
    while(isPrime[forward]==true)forward++;
    while(isPrime[back]==true)back--;
    cout << forward-back << endl;
  }
  return 0;
}