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 <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <map>
#include <functional>
#include <stdio.h>
#include <array>
#include <algorithm>
#include <string>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <queue>
#include<iomanip>
#include<bitset>
#include<stack>
#include<set>
#include<limits>
#include <complex>
#include<cstdlib>
using namespace std;

bool isso(long long int a) {
	if (a == 1||a==0)return false;
	for (long long int i = 2; i*i <= a; ++i) {
		if ((a%i)) {
		}
		else {
			return false;
		}
	}
	return true;
}
int main() {
	while (1) {
		int N; cin >> N;
		if (N) {
			int anum = 0;
			{
				while (1) {
					if (isso(N - anum)) {
						break;
					}
					anum++;
				}
			}
			int bnum = 0;
			{
				while (1) {
					if (isso(N + bnum)) {
						break;
					}
					bnum++;
				}
			}
			cout << anum + bnum << endl;
		}
		else {
			break;
		}
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

#define MAX 1500000
 
bool prime[MAX];
 
void make(){
    fill(prime,prime+MAX,true);
    prime[0] = prime[1] = false;
     
    for(int i = 2 ; i*i < MAX ; i++){
        if(prime[i]){
            for(int j = 2*i ; j < MAX ; j += i){
                prime[j] = false;
	    }
	}
    }
}
 
int main(){
    int n;
    make();
    while(cin >> n,n){
        int a,i;    
        if(prime[n]) cout << '0' << endl;
        else{
            for(i = n ; prime[i] != true ; i++);
            a = i;
            for(i = n ; prime[i] != true ; i--);
            cout << a-i << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;
vector<bool> createPrime ( int num )	// 素数の配列を生成 ( num以下 )
{
	vector<int> vp;
	vector<bool> vb(num+1, true);
	vb[0] = vb[1] = false;
	for (int i = 2; i <= num; ++i) {
		if (!vb[i]) { continue; }
		vp.push_back(i);
		for (int j = i+i; j <= num; j+=i) { vb[j] = false; }
	}
	return vb;
}

int main( ){
	vector< bool > prime = createPrime(1299709);
	while(true) {
		int n;
		cin >> n;
		if(!n) {
			break;
		}

		int ans;
		if(prime[n]) {
			cout << 0 << endl;
		} else {
			int a, b;
			for(int i = n; i >= 0; i--) {
				if(prime[i]) {
					a = i;
					break;
				}
			}
			for(int i = n; i <= 1299709; i++) {
				if(prime[i]) {
					b = i;
					break;
				}
			}
			cout << b - a << endl;
		}

		
	}
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

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

  const int MAXP = 1299709+1;
  vector<int> prime(MAXP, 0);
  for(int i=2;i<MAXP;++i)
	if(!prime[i])
	  for(int j=i+i;j<MAXP;j+=i)
		prime[j] = 1;

  int prv;
  for(int i=2;i<MAXP;++i){
	if(!prime[i]) prv = i;
	else{
	  ++prime[prv];
	  prime[i] = -1;
	}
  }
  //  REP(i,50) cout << prime[i] << ",";
  int N;
  while(cin >> N, N){
	if(prime[N] >= 0) cout << 0 << endl;
	else
	  for(int i=N-1;;--i)
		if(prime[i] > 0){
		  cout << prime[i]+1 << endl;
		  break;
		}
  }
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;

const int MAX_N = 1300000;
bool prime[MAX_N+1];

void sieve()
{
  fill(prime, prime+MAX_N+1, true);
  prime[0] = prime[1] = false;
  for(int i = 2; i <= MAX_N; i++)
    if(prime[i])
      for(int j = i*2; j <= MAX_N; j += i)
        prime[j] = false;
}

int main()
{
  sieve();
  while(1){
    int N;
    cin >> N;
    if(N == 0) break;
    if(prime[N]){
      cout << "0" << endl;
      continue;
    }
    int x = N - 1;
    int y = N + 1;
    while(!prime[x])
      x--;
    while(!prime[y])
      y++;
    cout << y - x << endl;
  }
}