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<vector>
#include<cmath>

using namespace std;

#define MAX 1300001

vector<int> vprimes;
bool *isPrime = new bool[MAX];

/* ツ素ツ青板青カツ青ャ O(n^2) */
void MakePrime(){
  isPrime[0]=false;
  isPrime[1]=false;
  isPrime[2]=true;
  for(int i = 2; i < MAX; ++i){
    isPrime[i] = true;
  }
  for(long long int i = 2; i < MAX; ++i){
    if( isPrime[i] ){
      for(long long int j = 2; i * j < MAX; ++j) {
	isPrime[i*j] = false;
      }
    }
  }
  for(int i = 0; i < MAX; ++i){
    if( isPrime[i] ){
      vprimes.push_back( i );
    }
  }
}

int main(){
  MakePrime();

  while(true){
    int k;
    cin >> k;
    if(k==0)break;

    int l=0;
    int lower=0,upper=0;
    int m,prem;
    bool inv=false;
    int r=vprimes.size();
    while(true){
      m=(l+r)/2;
      if(prem==m){
	if(vprimes[m]>k){
	  lower=m-1;
	  upper=m;
	}
	if(vprimes[m]<k){
	  lower=m;
	  upper=m+1;
	}
	break;
      }
      if(vprimes[m]>k)r=m;
      if(vprimes[m]<k)l=m;
      if(vprimes[m]==k){
	inv=true;
	break;
      }
      prem=m;
    }
    if(inv)lower=upper=1;
    cout << vprimes[upper]-vprimes[lower] << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

bool prime[2000000] = {};
void Eratosthenes(int n)
{
  for (int i = 2; i <= (int)sqrt(n) + 1; ++i)
  {
    if (prime[i] == 0)
    {
      for (int j = i * 2; j <= n; j += i)
      {
        prime[j] = 1;
      }
    }
  }
}

int main()
{
  Eratosthenes(1299709);
  int k;
  vector<int> ans;
  while (cin >> k, k)
  {
    int cnt = 0;
    int pk = k, nk = k;
    while (prime[pk] && pk < 1299709)
    {
      cnt++;
      pk++;
    }
    while (prime[nk] && nk > 1)
    {
      cnt++;
      nk--;
    }
    ans.push_back(cnt);
  }
  for (auto &e : ans)
    cout << e << endl;

  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int prime[1299709];
int table[100000];

int main(){
	prime[0]=1;
	for(int i=1;i<1299709;i++){
		if(prime[i]==0){
			prime[i]=1;
			for(int j=(i+1)*2-1;j<=1299709;j+=i+1)prime[j]=2;
		}
	}
	int now=0;
	for(int i=0;i<1299709;i++){
		if(prime[i]==1){
			table[now++]=i+1;
		}
	}
	int a;

	while(1){
		scanf("%d",&a);
		if(a==0)return 0;
		int index=(int)(lower_bound(table,table+100000,a)-table);
		if(table[index]==a)printf("0\n");
		else printf("%d\n",table[index]-table[index-1]);
	}
}

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

#define M 1299709

bool tbl[M + 1];

int main(){
	vector<int> p;
	p.reserve(100005);

	for(int i = 2; i * i <= M; ++i){
		if( !tbl[i] ){
			for(int j = i * i; j <= M; j += i){
				tbl[j] = true;
			}
		}
	}

	for(int i = 2; i <= M; ++i){
		if( !tbl[i] ){	p.push_back(i);	}
	}
	
	int k;
	while( scanf("%d", &k), k != 0 ){
		printf("%d\n", *lower_bound(p.begin(), p.end(), k) - *(upper_bound(p.begin(), p.end(), k) - 1));
	}
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>

#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>

#include <numeric>
#include <algorithm>
#include <functional>

#include <cctype>

#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define rep(i,n) for(unsigned int i=0;i<(n);i++)
#define tr(it,container) for(typeof(container.begin()) it = container.begin(); \
                                                  it != container.end(); ++it)
#define mp(a,b) make_pair((a),(b))

typedef long long ll;
typedef complex<double> P;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};
const double EPS = 1e-9;
const int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
const int daysleap[] = {31,29,31,30,31,30,31,31,30,31,30,31};

const int MAXX = 1299800;
bool isPrime[MAXX] = {false};
vector<int> PS;
void sieve(){
    for(int i=2;i<MAXX;i++) isPrime[i] = true;
    for(int i=2;i*i < MAXX;i++){
        if(!isPrime[i]) continue;
        for(int j=i*i;j<MAXX;j+=i){
            isPrime[j] = false;
        }
    }
    rep(i,MAXX) if(isPrime[i]) PS.push_back(i);
}

int main(){
    sieve();
    while(true){
        int n;
        cin >> n;
        if(n==0)break;
        if(isPrime[n]){
            cout << 0 << endl;
        }else{
            int ind = lower_bound(all(PS),n) - PS.begin();
            cout << PS[ind] - PS[ind-1] << endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>

using namespace std;


bool is_prime[1299709 + 1];

int main(){
  
  
  for(int i = 0; i <= 1299709; ++i){
    is_prime[i] = true;
  }

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

  while(1){
    int k;
    cin >> k;
    if(k == 0) return 0;
    
    if(is_prime[k] == true){
      cout << "0\n";
    }else{
      int left = k, right = k;
      while(is_prime[left] == false){
        left--;
      }
      while(is_prime[right] == false){
        right++;
      }
      cout << right - left << "\n";
    }
  }
    
}