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 KMAX 2299709

bool isprime[KMAX];

void cp(){
  for(int i=0; i < KMAX; i++){
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for(int i = 2; i*i <= KMAX; i++){
    if(isprime[i]){
      for(int j = i+i; j < KMAX; j += i){
        isprime[j] = false;
      }
    }
  }
}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);
  cp();
  int k;
  while(cin >> k, k){
    int a, b;
    for(int i = k; i >= 0; i--){
      if(isprime[i]){
        a = i;
        break;
      }
    }
    for(int i = k; i < KMAX; i++){
      if(isprime[i]){
        b = i;
        break;
      }
    }
    cout << b - a << endl;
  }
  return 0;
}

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

bool erat[2000000];
set<int> primes; 
int main(){
	for(int i = 0; i < 2000000; i++){
		erat[i] = true;
	}
	for(int i = 2; i < 2000000; i++){
		if(erat[i]){
			primes.insert(i);
			for(int j = i + i; j < 2000000; j += i){
				erat[j] = false;
			}
		}
	}
	int N;
	while(cin >> N, N){
		if(primes.count(N)){
			cout << 0 << endl;
		}else{
			auto it = primes.lower_bound(N);
			cout << (*it - *(--it)) << endl;
		}
	}
}

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

int main()
{
    int num = 1299711, prime_num, k;
    num = (num - 3) / 2;
    vector<bool> prime_list(num, true);
    vector<int> primes(1, 2);
    for (int i = 0; i < num; ++i) {
        if (prime_list[i]) {
            prime_num = i + i + 3;
            primes.push_back(prime_num);
            for (int j = i + prime_num; j < num; j += prime_num)
                prime_list[j] = false;
        }
    }
    while (cin >> k, k) {
        for (int i = 0; i < primes.size(); ++i) {
            if (primes[i] == k) {
                cout << 0 << endl;
                break;
            } else if (primes[i] > k) {
                cout << primes[i] - primes[i - 1] << endl;
                break;
            }
        }
    }
    return 0;
}

C++: #include <iostream>

using namespace std;

#define PRIMENUM 1299710

//その数は素数かどうか判定する変数
int isPrime[PRIMENUM];

void makePrimeNumber(){

	isPrime[2]=1;
	isPrime[3]=1;

	for(int i=4;i<PRIMENUM;i++){
		int flag=0;
		for(int j=2;j*j<=i;j++){
			if(i%j==0){
				flag=1;
				break;
			}
		}
		if(flag==0){
			isPrime[i]=1;
			//cout<<i<<endl;
		}	
	}

}

int main(){
	int n;

	makePrimeNumber();

	while(cin>>n){
		if(n==0)break;
		if(isPrime[n]==1){
			cout<<0<<endl;
			continue;
		}

		int start,end;

		for(int i=n-1;i>=0;i--){

			if(isPrime[i]==1){
				start = i;
				break;
			}
		}
		for(int i=n+1;i<=PRIMENUM;i++){
			if(isPrime[i]==1){
				end = i;
				break;
			}
		}
		cout<<end-start<<endl;
	}

	return 0;
}

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

#define MAX 1299709+1
int main(){
  int b[MAX]={0},i,j;
  b[0]=b[1]=1;
  for(i=4;i<MAX;i+=2)b[i]=1;
  for(i=3;i*i<MAX;i+=2)
    if(!b[i])for(j=i+i;j<MAX;j+=i)b[j]=1;

  int n,c;

  while(cin>>n,n){
    c=0;
    for(i=n;i>2;i--){
      if(!b[i])break;
      c++;
    }
    for(i=n;i<MAX;i++){
      if(!b[i])break;
      c++;
    }
    if(b[n])cout<<c<<endl;
    else puts("0");
  }
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
bool isPrime(int x){
    if(x < 2) return false;
    if(x == 2) return true;
    if(x%2 == 0) return false;
    for(int i=3; i*i<=x; i++){
        if(x%i == 0) return false;
    }
    return true;
}
int main(){
    int n;
    while(cin >> n){
        if(n == 0) break;
        if(isPrime(n)) {
            cout << 0 << endl;
            continue;
        }
        int cp = n,cp1 = n;
        int left = 0,right = 0;
        int flag_left=0,flag_right=0;
        while(1){
            if(left != 0 && right != 0) break;
            if(flag_left < 1) cp--;
            if(flag_right < 1) cp1++;
            if(isPrime(cp)) {
                left = cp;
                flag_left++;
            }
            if(isPrime(cp1)){
                right = cp1;
                flag_right++;
            }
        }
        cout << right-left << endl;
    }
    return 0;
}