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 <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000
#define MAX 1299709

// sieve of Eratosthenes (prime num)
bool p[MAX+1]; //prime number=true
void prime(int n){
  rep(i,n+1) p[i]=true;
  p[0]=p[1]=false;
  for(int i=2;i<=n;i++){
    if(p[i]){
      for(int j=i*2; j<=n; j+=i){
	p[j]=false;
      }
    }
  }
}

int solve(int k){
  if(p[k]) return 0;
  int s=k, l=k; //small, large
  while(!p[s]) s--;
  while(!p[l]) l++;
  return l-s;
}

int main(){
  prime(MAX);
  int k;
  while(scanf("%d", &k), k){
    cout << solve(k) << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;
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;
}
//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
  int anss[1300000];
int main() {
  int from = 2;
  for (int i = 2; i < 1300000; ++i) {
    if (isso(i)) {
      anss[i] = 0;
      for (int j = from; j < i; ++j) {
        anss[j] = i - from+1;
      }
      from = i + 1;
    }
    else {

    }
  }
  while (1) {
    int a; cin >> a;
    if (!a)break;
    else {
      cout << anss[a] << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define loop(i,a,b) for(int i=(a); i<int(b); i++)
#define rep(i,b) loop(i,0,b)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;

int const N=1299709+10;
vb isp;
vi ps;
void eratos(){
    isp.assign(N,true);
    ps.reserve(100000);
    isp[0]=isp[1]=false;
    ps.push_back(2);
    for(int i=4;i<N;i+=2){
        isp[i]=false;
    }
    for(int i=3;i<N;i++){
        if(!isp[i]) continue;
        ps.push_back(i);
        for(int j=i+i;j<N;j+=i){
            isp[j]=false;
        }
    }
}

int main(){
    eratos();
    int k;
    while(cin>>k&&k){
        auto lb=lower_bound(all(ps),k);
        auto i=lb-1;
        // cout << *lb << " " << *i << endl;
        if(k==*lb){
            cout << 0 << endl;
        } else {
            cout << *lb - *i  << endl;
        }
    }
}

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

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

using namespace std;
typedef long long ll;

bool prime[10000000];
void Eratosthenes(int n) {
    rep(i,n) prime[i] = true;
    prime[0] = false;
    prime[1] = false;

    REP(i,2,(int)sqrt(n)) {
        if(prime[i]) {
            for(int j=0;i*(j+2)<n;j++) {
                prime[i*(j+2)] = 0;
            }
        }
    }
}

int main() {
    Eratosthenes(10000000);

    int n;
    while(cin >> n && n) {
        int ans = 0;

        REP(i,n+1,1299710) {
            if(prime[i]) {
                ans = i;
                break;
            }
        }

        for(int i=n-1;i>=2;i--) {
            if(prime[i]) {
                ans -= i;
                break;
            }
        }

        if(prime[n]) cout << 0 << endl;
        else cout << ans << endl;

    }

    return 0;
}

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

bool check(int x){
	if(x == 2)return true;
	if(x < 2 || x%2 == 0)return false;
	for(int i = 3;i <= sqrt(x);i+=2){
		if(x%i == 0)return false;
	}
	return true;
}

int main(){
	int n,a,b;
	while(1){
		cin >> n;
		if(n == 0)break;
		if(check(n)){
			cout << 0 <<endl;
			continue;
		}
		for(int i = n + 1;i <= 1299709;i++){
			if(check(i)){
				a = i;
				break;
			}
		}
		for(int i = n - 1;i >= 3;i--){
			if(check(i)){
				b = i;
				break;
			}
		}
		cout << a - b <<endl;
	}
	return 0;
}

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

#define MAX_N 1299709+1

bool prime[MAX_N];

void init() {
    memset(prime, 1, sizeof(prime));
    prime[0] = prime[1] = false;
    for (int i = 2; i <= MAX_N; i++) 
        if (prime[i]) 
            for (int j = i+i; j <= MAX_N; j+=i) 
                prime[j] = false;
}

int main() {
    init();
    int n;
    while (cin >> n, n != 0) {
        int lb = n;
        int ub = n;
        while (!prime[lb]) 
            lb--;
        while (!prime[ub])
            ub++;
        cout << ub - lb << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int X, Y, A, i, j;
int main() {
	while (true) {
		cin >> A;
		for (i = A; i >= 2; i--) { for (j = 2; j <= sqrt(i); j++) { if (i%j == 0) { goto E; } }X = i; goto F; E:; }
	F:; for (i = A; i < 99999999; i++) { for (j = 2; j <= sqrt(i); j++) { if (i%j == 0) { goto G; } }Y = i; goto H; G:; }
	H:; cout << Y - X << endl;
	}
}