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 <iostream>
#include <cstring>
using namespace std;

#define MAX_N 100000
#define MAX_PRIME 1299709

bool cache[MAX_PRIME+1];

void make_cache() {
    memset(cache, 1, sizeof(cache));
    cache[0] = cache[1] = false;
    for (int i = 2; i <= MAX_PRIME; i++) {
        if (cache[i]) {
            for (int j = i + i; j <= MAX_PRIME; j+=i) {
                cache[j] = false;
            }
        }
    }
}

int main() {
    make_cache();
    int n;
    while (cin >> n) {
        if (n == 0) break;
        int gap;
        if (cache[n]) {
            gap = 0;
        } else {
            int l, r;
            int i;
            for (i = 0; !cache[n-i]; i++);
            l = n-i;
            for (i = 0; !cache[n+i]; i++);
            r = n+i;
            gap = r - l;
        }
        cout << gap << endl;
    }
    return 0;
}

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

bool f[1300001] = {};
long long int len[1300001] = {};

int main() {

  long long int k = 0;

  for ( long long int i = 2; i < 1300000; i++ ) {

    if ( f[i] == false ) {

      len[k] = i - k;
      for ( long long int j = 2; j * i < 1300000; j++ ) {
	f[j*i] = true;
      }
      k = i;

    }else {

      len[i] = k;

    }

  }

  while( true ) {

    long long int n;
    cin >> n;
    if ( n == 0 ) break;

    if ( f[n] == false ) {
      cout << 0 << endl;
    }else {
      cout << len[len[n]] << endl;
    }

  }

  return 0;

}

C++: import java.util.*;
import java.lang.*;
import java.io.*;

public class Main{
	Scanner sc=new Scanner(System.in);

	void run(){
		boolean[] p=new boolean[2000000];
		Arrays.fill(p, true);
		p[0]=p[1]=false;
		for(int i=2; i<p.length; i++){
			if(p[i]){
				for(int j=i*2; j<p.length; j+=i){
					p[j]=false;
				}
			}
		}
		for(;;){
			int m=sc.nextInt();
			if(m==0){
				break;
			}
			int ans=0;
			for(int i=m; i>=0&&!p[i]; i--){
				ans++;
			}
			for(int i=m; i<p.length&&!p[i]; i++){
				ans++;
			}
			println(""+ans);
		}
	}

	void print(String s){
		System.out.print(s);
	}

	void println(String s){
		System.out.println(s);
	}

	public static void main(String[] args){
		new Main().run();
	}
}

C++: #include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
#include<string>
#include<cstdio>
#include<queue>
#include<functional>
#include<tuple>
#include<iomanip>
#include<stack>
#include<cmath>
#define ll long long
#define N 1299710
using namespace std;
int main() {
	bool prime[N];
	for (int i = 2; i < N; ++i) prime[i] = true;
	for (int i = 2; i*i < N; ++i)
		if (prime[i])
			for (int j = 2; i*j < N; ++j) prime[i*j] = false;
	vector<int>primeList;
	for (int i = 2; i < N; ++i)
		if (prime[i]) primeList.push_back(i);
	int n;
	while (cin >> n && n) {
		int pos=0;
		while (primeList[pos] < n) ++pos;
		if (primeList[pos] == n) cout << "0" << endl;
		else  cout << primeList[pos] - primeList[pos - 1] << endl;
	}
	return 0;
}

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

int main()
{
  bool pri[1000001];
  //  vector<int> price;
  fill(pri,pri+1000001,true);
  pri[0]=pri[1]=false;
  for(int i=2;i<1000001;i++)
    {
      if(pri[i]==0)
	continue;
      //  price.push_back(i);	
      for(int j=i+i;j<1000001;j+=i)
	{
	  pri[j]=false;
	}
    }
  int n;
  for(;cin>>n,n;)
    { 
      
      if(pri[n])
	cout<<"0"<<endl;
      else
	{
	  int max,min;
	  for(int i=n-1;;i--)
	    {
	      if(pri[i])
		{
		  min=i;
		  break;
		}
	    }
	  for(int i=n+1;;i++)
	    {
	      if(pri[i])
		{
		  max=i;
		  break;
		}
	    }
	  cout<<max-min<<endl;
	}    
    }
 
}

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

int main(){
  int pn[N]={},n,f,st,end,fst;
  pn[0]=1,pn[1]=1;
  for(int i=2;i<sqrt(N);i++)
    for(int j=2;j*i<N;j++)
      if(pn[i]==0) pn[i*j]=1;
  while(1){
  cin>>n;
  if(n==0) break;
  st=0,end=0,f=0,fst=0;
  while(1){
    if(f==1 || pn[n]==0) break;
    st=0,end=0;
    for(int i=fst;i<N;i++){
           if(i==n) f=1;
      if(pn[i]==0 && st==0) st=i;
      if(pn[i]==0 && st!=i) end=i;
        if(st>0 && end>0){
	fst=end;
	break;
      }
    }
  }
     cout<<end-st<<endl;      
  }
  return 0;
}

C++: #include<iostream>
 
using namespace std;
 
const int MAX= 100001;
bool prime[MAX];
void make_prime(){
  for(int i = 0 ; i < MAX ; i ++){
    prime[i] = true;
  }
     
  prime[0] = false;
  prime[1] = false;
     
  for(int i = 2 ; i <= MAX ; i ++){
    if(prime[i]){
      for(int j = i+i ; j <= MAX ; j += i){
	prime[j] = false;
      }
    }
  }
}
 
int main(void){
  make_prime();
  int n;
  while(cin>>n,n){
    for(int i = n ; 2 <= i ; i --){
      if(prime[n]){
	cout<<0<<endl;
	continue;
      }
      int i,j;
      for(i = n; !prime[i]; i--);
      for(j = n; !prime[j]; j++);
      cout<<j-i<<endl;
    }
  }
}

C++: #include<iostream>
#include <algorithm>
using namespace std;
#define MAX_LIST 1000000 



int main(){
	bool *prime=(bool *)malloc(sizeof(bool)*MAX_LIST);
	//bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){ 
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	int in;
	while(cin>>in,in){
		int ans=0;
		int i=0;
		while(prime[in+i]==0){
			ans++;
			i++;
		}
		i=0;
		while(prime[in-i]==0){
			ans++;
			i++;
		}
		cout<<ans<<endl;
	}
	
	return 0;
}