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;
//#define max 1000000;

int prime[1000000];

void mk_prime(){
    for( int i = 0; i < 1000000; i++){
        prime[i] = 1;
    }
    prime[0] = 0;
    prime[1] = 0;
    for( int i = 2; i < 1000000; i++ ){
        if( prime[i] == 1 ){
            for( int j = 2 * i; j < 1000000; j += i ){
                prime[j] = 0;
            }
        }
    }
}

int main(){
    mk_prime();
    int k;

    while( 1 ){
        cin >> k;
        if( k == 0 ) return 0;
        if( prime[k] ){
            cout << 0 << endl;
            continue;
        }
        int s, e;
        for( int i = k - 1; ; i-- ){
            if( prime[i] ){
                s = i;
                break;
            }
        }
        for( int i = k + 1; ; i++ ){
            if( prime[i] ){
                e = i;
                break;
            }
        }
        cout << e - s << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
using namespace std;

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

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

	vector<int> ps;
	for(int i = 2; i <= 1500000; i++) {
		if(isPrime(i)) ps.push_back(i);
	}

	int n;
	while(cin >> n, n) {
		for(int i = 0; i < ps.size(); i++) {
			if(ps[i] <= n && n < ps[i + 1]) {
				if(ps[i] == n) {
					cout << 0 << endl;
				}
				else {
					cout << ps[i + 1] - ps[i] << endl;
				}
				break;
			}
		}
	}
}

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

int main() {
    const int MAX = 1300000;
    bool isPrime[MAX];
    fill_n(isPrime, MAX, true);
    for (int i = 4; i < MAX; i += 2) {
        isPrime[i] = false;
    }
    for (int i = 3; i * i <= MAX; i += 2) {
        if (isPrime[i]) {
            for (int j = i * 2; j < MAX; j += i) {
                isPrime[j] = false;
            }
        }
    }
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        int gap = 0;
        if (isPrime[n] == false) {
            int min = n, max = n;
            do {
                min--;
            } while (isPrime[min] == false);
            do {
                max++;
            } while (isPrime[max] == false);
            gap = max - min;
        }
        cout << gap << endl;
    }
}

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

int main(){
	char est[1300000];
	for(int i = 2; i < 1300000; i++){
		est[i] = '1';
	}
	for(int i = 2; i * i <= 1300000; i++){
		for(int k = 2; k * i < 1300000; k++){
			est[i*k] = '0';
		}
	}
	int n;
	while(cin >> n){
		if(n == 0) break;
		int i = n, j = n;
		while(est[i] == '0') i--;
		while(est[j] == '0') j++;
		cout << j - i << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);++i)

#define N 1300000
bool prime[N];

int solve(int n){
    if(prime[n]) return 0;
    int l=n,r=n;
    while(!prime[l])l--;
    while(!prime[r])r++;
    return r-l;
}

int main(void){
    rep(i,N) prime[i] = true;
    prime[0] = prime[1] = false;
    for(int i=2; i<N; i++){
        if(!prime[i]) continue;
        for(int j=i*2; j<N; j+=i){
            prime[j] = false;
        }
    }

    int n;
    while(cin>>n,n){
        cout<<solve(n)<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30
#define MAX_P 1300000

int main(){
  bool f[MAX_P] = {};
  f[0] = f[1] = 1;
  REP(i, 2, MAX_P / 2 + 1){
    if(!f[i]){
      for(int j = i+i; j < MAX_P; j += i) f[j] = 1;
    }
  }

  //  rep(i, 100) if(!f[i]) printf("%d ", i); puts("");
  int n;
  while(scanf("%d", &n) && n){
    int a, b;
    for(int i = n; i >= 0; i--) if(!f[i]){ a = i; break;;}
    for(int i = n; i < MAX_P; i++) if(!f[i]){ b = i; break;;}
    printf("%d\n", b-a);
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
bool a[1500000];
int main() {
	fill(a, a + 1500000, true);
	a[0] = false; 1[a] = false;
	for (int i = 2; i < 1500000; i++) {
		if (a[i]) {
			int j = 2;
			while (i*j < 1500000) {
				a[i*j] = false;
				j++;
			}
		}
	}
	int n;
	while (cin >> n&&n != 0) {
		int s = n, e = n;
		while (!a[s]) s--;
		while (!a[e]) e++;
		cout << e - s << endl;
	}
}

C++: #include<iostream>
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;
	}
}