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 <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

void makePrimeNumber(int N, vector<bool>& prime) // 0ツ暗按湘」Nツ暗按可コツづ個青ョツ青板つゥツづァツ素ツ青板づーツ探ツつキ
{
    prime.assign(N+1, true);
    prime [0] = prime[1] = false;
    for(int i=2; i*i<=N; i++){
        if(prime[i]){
            for(int j=i; i*j<=N; j++){
                prime[i*j] = false;
            }
        }
    }
}

int main()
{
    vector<bool> prime;
    makePrimeNumber(1299709, prime);

    for(;;){
        int k;
        cin >> k;
        if(k == 0)
            return 0;

        if(prime[k]){
            cout << 0 << endl;
            continue;
        }

        int i = k;
        int j = k;
        while(!prime[i])
            -- i;
        while(!prime[j])
            ++ j;
        cout << (j-i) << endl;
    }
}

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

const int MAX_N = 1299709;

int main() {
    vector<char> prime(MAX_N + 1, true);
    prime[0] = prime[1] = false;
    for(int i = 2; i * i <= MAX_N; i++)
        if(prime[i])
            for(int j = 2 * i; j <= MAX_N; j += i)
                prime[j] = false;
    vector<int> A;
    for(int i = 2; i <= MAX_N; i++) if(prime[i]) A.push_back(i);
    for(int N; cin >> N, N; ) {
        if(binary_search(A.begin(), A.end(), N)) {
            cout << 0 << endl;
        }
        else {
            vector<int>::iterator lb = lower_bound(A.begin(), A.end(), N);
            vector<int>::iterator ub = upper_bound(A.begin(), A.end(), N);
            cout << *ub - *(lb - 1) << endl;
        }
    }
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<endl
#define sset(s) cout<<#s<<":";each(i,s)cout <<" "<<i;cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 1300000;

int prime[MAX_N];
bool is_prime[MAX_N];

int sieve(int n)
{
	int p = 0;
	for(int i=0;i<=n;i++){
		is_prime[i] = true;
	}
	is_prime[0] = is_prime[1] = false;
	for(int i=2;i<=n;i++){
		if(is_prime[i]){
			prime[p++] = i;
			for(int j=2*i;j<=n;j+=i){
				is_prime[j] = false;
			}
		}
	}
	return p;
}


int main()
{
    sieve(1299709);
    while(1){
        int n;
        cin >> n;
        if(n == 0){
            break;
        }
        if(is_prime[n]){
            cout << "0\n";
            continue;
        }
        int i = 0;
        while(prime[i] < n){
            i++;
        }
        cout << prime[i] - prime[i-1] << "\n";
    }
}

C++: #include <iostream>
#include <cstdlib>
#define MAX 1299710

using namespace std;
int era[ MAX ]; // 0-initialized.
int prime[ MAX ];

int main(){
  for( int i = 2; i < MAX; i++ ){
    if( era[ i ] ){ continue; }
    for( int j = i * 2; j < MAX; j = j + i ){
      era[ j ] = 1;
    }
  }
  int k = 0;
  for( int i = 2; i < MAX; i++ ){
    if( !era[ i ] ){
      prime[ k++ ] = i;
    }
  }
  while( 1 ){
    int n;
    cin >> n;
    if( n == 0 ){ return EXIT_SUCCESS; }
    if( n == 2 ){
      cout << 0 << endl; 
      continue;
    }
    for( int i = 1; i < k; i++ ){
      if( prime[ i ] > n ){
        cout << prime[ i ] - prime[ i - 1 ] << endl;
        break;
      }else if( prime[ i ] == n ){
        cout << 0 << endl;
        break;
      }
    }
  }
}

C++: #include <iostream>
#define MAX_N 1299709 + 1
using namespace std;

bool t[MAX_N];

bool isPrime(int x) {
  if(x == 2) return true;
  if(x < 2 || x % 2 == 0) return false;
  int i = 3;
  while(i * i <= x) {
    if(x % i == 0) return false;
    i += 2;
  }
  return true;
}

void table() {
  for(int i = 1; i < MAX_N; ++i) {
    if (isPrime(i)) t[i] = 1;
  }
}

void solve(int k) {
  int p = k, n = k;
  while(t[p] != 1) p--;
  while(t[n] != 1) n++;
  cout << n - p << endl;
}

int main() {
  table();
  int k;
  while(cin >> k && k) {
    solve(k);
  }
  return 0;
}

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

using namespace std;

int main()
{
	vector<int> isp(1299710,1);
	isp[0]=isp[1]=0;
	for(int i=2;i*i<isp.size();i++)
		for(int j=i*i;j<isp.size();j+=i)
			isp[j]=0;
	
	for(int k;cin>>k,k;){
		int i,j;
		for(i=k;!isp[i];i--);
		for(j=k;!isp[j];j++);
		cout<<j-i<<endl;
	}
	
	return 0;
}

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

using namespace std;

inline int nextInt() { int d; cin >> d; return d; }

bool isPrime(int n) {
  if (n == 2) return true;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

int main() {
  while (true) {
    int p = nextInt();
    if (p == 0) {
      break;
    }
    int u, d;
    for (u = p; ; u++) {
      if (isPrime(u)) {
        break;
      }
    }
    for (d = p; ; d--) {
      if (isPrime(d)) {
        break;
      }
    }
    cout << u - d << endl;
  }

  return 0;
}