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 int long long
#define pb push_back 
#define pf push_front 
#define mp make_pair
#define fr first
#define sc second
#define Rep(i,n) for(int i=0;i<(n);i++)
#define All(v) v.begin(),v.end()
typedef pair<int, int> Pii; typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;

main()
{
  bool is_prime[1300000];
  vector<int> prime;
  
  Rep(i, 1300000) is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for(int i=2; i<1300000; i++) {
    if( is_prime[i] ) {
      prime.pb( i );
      for(int j=2*i; j<1300000; j+=i) is_prime[j] = false;
    }
  }

  //for(int i=0; i<prime.size(); i++) cout << prime[i] << endl;
  
  int k;
  while( cin >> k, k ) {
    if( is_prime[k] ) cout << 0 << endl;
    else cout << *upper_bound( All(prime), k ) - *(lower_bound( All(prime), k ) -1) << endl; 
  }
  
}

C++: // AOJ 1276

#include <iostream>

using namespace std;

bool prime[2000000];
void makeprime()
{
	fill(prime, prime + 2000000, true);
	prime[0] = prime[1] = false;
	for (int i = 4; i < 2000000; i+=2) prime[i] = false;
	for (int i = 3; i*i <= 2000000; i += 2){
		if (prime[i]){
			for (int j = i*2; j < 2000000; j += i){
				prime[j] = false;
			}
		}
	}

	return;
}

int main(void)
{
	makeprime();

	int k;
	while (cin >> k, k){
		int a = 0, b = 0;
		for (int i = k; i < 2000000; i++){
			if (prime[i]){
				a = i;
				break;
			}
		}
		for (int i = k; 0 <= i; i--){
			if (prime[i]){
				b = i;
				break;
			}
		}
		cout << a - b << endl;

	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define BIG 1000000010
#define EPS 1e-9
#define fst first
#define scd second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long


#define N 1300000

int prime[N];

void eratosthenes(){
  rep(i,N) prime[i]=1;
  prime[0]=prime[1]=0;
  rep(i,sqrt(N)){
    if(prime[i]){
      for(int j=i+i;j<N;j+=i) prime[j]=0;
    }
  }
}

signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  eratosthenes();
  while(1){
    int n;
    int numu,numd;
    cin>>n;
    if(n==0) break;
    else{
      repi(i,n,1300000){
	if(prime[i]==1){numu=i;break;}
      }
       repn(i,n){
	if(prime[i]==1){numd=i;break;}
      }
    }
    cout<<numu-numd<<endl;
  }
  
    
      

  return 0;
}

C++: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> P;
const int pmax=1299709;
bool iscomp[pmax+1];
void init()
{
	P.reserve(100000);
	for(int i=2;i<pmax+1;i++){
		if(iscomp[i])continue;
		P.push_back(i);
		for(int j=2;i*j<pmax+1;j++)
			iscomp[i*j]=true;
	}
}
int main()
{
	init();
	while(true){
		int n;
		cin>>n;
		if(n==0)return;
		int pos=lower_bound(P.begin(),P.end(),n)-P.begin();
		if(P[pos]==n)cout<<0<<endl;
		else{
			cout<<P[pos+1]-P[pos]<<endl;
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define MP make_pair
#define PB push_back
typedef long long ll;
const int N_MAX=1300000;
bool prime[N_MAX];
int main(){
	int n;
	rep(i,N_MAX)prime[i]=true;
	prime[0]=prime[1]=false;
	rep(i,N_MAX)if(prime[i])for(int j=i*2;j<N_MAX;j+=i)prime[j]=false;
	while(cin>>n,n){
		int i,j;
		for(i=n;!prime[i];i--);
		for(j=n;!prime[j];j++);
		cout<<j-i<<endl;
	}
	return 0;
}

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

int main(){
  int data[1400000],i,j;
  for(i = 2; i*i < 1400000; i++){
    if(data[i]==0){
      for(j = i*2; j < 1400000; j+=i){
	data[j]=1;
      }
    }
  }
  data[0]=1;
  data[1]=2;
  int n,down,up;
  while(1){
    cin >> n;
    if(n==0)break;
  
    for(i = n; i >=2; i--){
      if(data[i]==0){
	down = i;
	break;
      }
    }
    
    for(i = n; i < 1400000; i++){
      if(data[i]==0){
	up = i;
	break;
      }
    }
    
    if(data[n]==0)cout << "0" << endl;
    else cout << up - down << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 100000

int primes[N+10];
void li(){
	int p=0,i,j;bool b;
	primes[p++]=2;
	for(i=3;;i+=2){
		b=true;
		for(j=0;j<p&&primes[j]*primes[j]<=i;j++){
			if(i%primes[j]==0) {b=false;break;}
		}
		if(b) primes[p++]=i;
		if(p==N) break; 
	}
	//cout<<primes[N-1]<<endl;
}
int main(){
	li();
	int n;
	while(scanf("%d",&n)&&n){
		int z=lower_bound(primes,primes+N,n)-primes;
		if(primes[z]==n) puts("0");
		else{
			printf("%d\n",primes[z]-primes[z-1]);
		}
	}
	
	return 0;
}

C++: // 2017-02-21
#include <iostream>
using namespace std;
#define N 1299709

int main() {
	static bool a[N + 1] = {true, true};
	static int b[N + 1], c[N + 1];
	{
		int t = 2;
		for (int i = 2; i <= N; i++) {
			if (a[i]) {
				b[i] = t;
			} else {
				c[t] = i - t;
				t = i;
				for (int j = 2 * i; j <= N; j += i) {
					a[j] = true;
				}
			}
		}
	}
	int n;
	while (cin >> n, n) {
		if (a[n]) {
			cout << c[b[n]] << endl;
		} else {
			cout << 0 << endl;
		}
	}
	return 0;
}