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>
using namespace std;
int prime[10000];
int main(){
	int count=0;
	int i=2;
	while(true){
		bool t=true;
		for(int j=0;j<count;j++){
			if(i%prime[j]==0)
				t=false;
		}
		if(t==true){
			prime[count]=i;
			count++;
		}
		i++;
		if(i==10000)
			break;
	}
	int n;
	while(true){
	cin>>n;
	if(n==0)break;
	int bef,aft;
	bef=n;
	while(true){
		bool t=true;
		for(int i=0;i<count;i++){
			if(bef%prime[i]==0&&bef!=prime[i])
				t=false;
		}
		if(t==true)
			break;
		bef--;
	}
	aft=n;
	while(true){
		bool t=true;
		for(int i=0;i<count;i++){
			if(aft%prime[i]==0&&aft!=prime[i])
				t=false;
		}
		if(t==true)
			break;
		aft++;
	}
	cout<<aft-bef<<endl;
	}
	return 0;
}

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

#define MAX 1299709

int main(void){
  // エラトステネスの篩
  vector<bool>  eratos(MAX+2, true);
  eratos[0] = eratos[1] = false;
  for(size_t i = 2; i * i <= eratos.size(); i++){
    if(eratos[i]){
      for(size_t j = 2; i * j <= eratos.size(); j++){
        eratos[i * j] = false;
      }
    }
  }
  eratos[eratos.size() - 1] = true;

  // PrimGapを全て計算
  vector<int>  ans(MAX+2, -1);
  size_t s = 2, t = 2;
  while(t != eratos.size() - 1){
    s = t;
    for(++t; !eratos[t]; ++t);    
    ans[s] = 0;
    for(size_t i = s + 1; i < t; i++){
      ans[i] = t - s;
    }
  }

  // 出力
  while(true){
    int n;
    cin >> n;
    if(n == 0){ break; }
    cout << ans[n] << endl;
  }
  return 0;
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <cassert>
#include <iomanip>


using namespace std;

const int N = 1299709+100;
vector<int> sosuu(N);

void ini(){
	for(int i=2;i<N;i++) sosuu[i] = i;
	for(int i=2;i*i<N;i++){
		if(sosuu[i]){
			for(int j=i*i;j<=N;j+=i){
				sosuu[j] = 0;
			}
		}
	}
}

bool solve(int n){
	int a, b;
	for(int i=n;i>=1;i--){
		if(sosuu[i]){
			a = i;
			break;
		}
	}
	for(int j=n;j<=N;j++){
		if(sosuu[j]){
			b = j;
			break;
		}
	}
	
	cout<< b-a << endl;
	return true;
}

int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	
	ini();
	int n;
	while(cin>>n, n!=0){
		solve(n);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<int> get_prime(int n) {
    if(n <= 1) return vector<int>{};
    vector<bool> is_prime(n+1, true);
    vector<int> prime;
    for (int i = 2; i <= n; ++i) {
        if(!is_prime[i]) continue;
        prime.emplace_back(i);
        for (int j = i*2; j <= n; j += i) {
            is_prime[j] = false;
        }
    }
    return prime;
}

int main() {
    const auto primes = get_prime(1299715);
    set<int> s;
    for (auto &&i : primes) s.insert(i);
    int n;
    while(cin >> n, n){
        auto ub = s.upper_bound(n);
        auto lb = ub;
        lb--;
        cout << n << "\n";
        if(*lb == n){
            puts("0");
            continue;
        }else {
            printf("%d\n", *ub-*lb);
        }
    }
    return 0;
}

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

int main() {

	int n = 1234567;
	bool prime[n+1];
  memset(prime, true, sizeof(prime));
 
  for (int p=2; p*p<=n; p++)
  {
      if (prime[p] == true)
      {
          for (int i=p*2; i<=n; i += p)
              prime[i] = false;
      }
  }

  int N;
	while (cin >> N && N != 0) {
		int l = N, r = N;
		while(!prime[l] || !prime[r]) {
			if (!prime[l])
				l--;
			if (!prime[r])
				r++;
		}

		cout << r-l << '\n';
	}

	return 0;
}

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

bool isprime[1299710];

void eratos(int n){
  
  for(int i=0 ; i <= n ; i++){
    isprime[i]=true;    
  }
  isprime[0]=isprime[1]=false;
  for(int i=2 ; i <= sqrt(n) ; i++){
    if(isprime[i]){
      int  j=i+i;
      while(j <= n){
	isprime[j]=false;
	j=j+i;
      }
    }
  }
}

int main(){
  
  int k;
  int prime1,prime2;

  eratos(1299709);

  while(1){
    
    cin >>k;
    if(k==0)break;
    
    if(isprime[k]){
      cout <<"0"<<endl;
    }else{
      for(prime1=k+1 ; !isprime[prime1] ; prime1++);
      for(prime2=k-1 ; !isprime[prime2] ; prime2--);
      cout <<prime1-prime2<<endl;;
    }
 
  }
 
  return 0;
}

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

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

vector<int> pg(){
  vector<int> pl(1299710,1);
  pl[0]=pl[1]=0;
  for(int i=2;i*i<=1299709;++i)
    if(pl[i])
      for(int j=i*i;j<=1299709;j+=i)
        pl[j]=0;
  int old=2;
  vector<int> v(1299710,0);
  for(int i=3;i<=1299709;++i){
    if(pl[i]){
      int l=i-old;
      for(int j=old+1;j<i;++j){
        v[j]=l;
      }
      old=i;
    }
  }
  return v;
}

int main() {
  vector<int> v=pg();
  while(1){
    int n;
    cin>>n;
    if(!n)break;
    cout<<v[n]<<endl;
  }
  return 0;
}