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;

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

	return true;
}

int main()
{
	int k;
	while (cin >> k && k != 0)
	{
		int min = k;
		int max = k;
		int length = 0;
		while (true)
		{
			bool primeMin = isPrime(min);
			bool primeMax = isPrime(max);

			if (primeMin && primeMax)break;
			if (!primeMin)
			{
				min--;
				length++;
			}

			if (!primeMax)
			{
				max++;
				length++;
			}
		}

		cout << length << endl;
	}
	return 0;
}

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

int main(){
  bool compnum[1299710];
  memset(compnum, false, sizeof(compnum));
  for (int i = 2; i*i <= 1299709; i++) {
    for (int j = i+i; j <= 1299709; j+=i) {
      compnum[j] = true;
    }
  }
  while (true) {
    int x;
    cin >> x;
    if (x == 0) break;
    int l, r;
    l = r = x;
    while (l > 2 && compnum[l]) l--;
    while (r < 1299709 && compnum[r]) r++;
    cout << r-l << endl;
  }
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

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


int main(){
    int n;
    while(cin>>n){
        if(n==0)break;
        if( isPrime(n) )cout<<"0"<<endl;
        else{
            int down,up;
            for(int i=n-1;i>=0;i--){
                if( isPrime(i) ){
                    down=i;
                    break;
                }
            }
            for(int i=n+1;;i++){
                if( isPrime(i) ){
                    up=i;
                    break;
                }
            }
            cout<<up-down<<endl;
        }
    }
}

C++: #include <cstdio>

const int MaxPrime = 1300000;

bool prime[MaxPrime];

void init_prime() {
  prime[0] = prime[1] = false;
  for (int i=2;i<MaxPrime;i++) prime[i] = true;
  for (int i=2;i<MaxPrime;i++) {
    if (!prime[i]) continue;
    for (int j=i+i;j<MaxPrime;j+=i) prime[j] = false;
  }
}

int main() {
  init_prime();
  int n;
  while (scanf("%d", &n), n!=0) {
    if (prime[n]) {
      puts("0");
      continue;
    }
    int b = n, e = n;
    while (!prime[b]) b--;
    while (!prime[e]) e++;
    printf("%d\n", e-b);
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <array>
#include <vector>

using namespace std;

vector<int> *makePrimes( const int limit ) {
  vector<bool> isPrime( limit + 1 );
  fill( isPrime.begin(), isPrime.end(), true );
  isPrime[ 0 ] = isPrime[ 1 ] = false;
  for ( int i = 2; i * i <= limit; i++ ) {
	if ( isPrime[ i ] ) {
	  for ( int j = i * i; j <= limit; j+= i ) {
		isPrime[ j ] = false;
	  }
	}
  }
  const int size = count( isPrime.begin(), isPrime.end(), true );
  auto ans = new vector<int>( size );
  int index = 0;
  for ( int i = 0; i <= limit; i++ ) {
	if ( isPrime[ i ] ) {
	  (*ans)[ index++ ] = i;
	}
  }
  return ans;
}

int binary_search( vector<int> *vs, int val ) {
  int left = 0;
  int right = vs->size() - 1;
  while( left + 1 < right ) {
	const int middle = ( left + right ) / 2;
	if ( (*vs)[ middle ] <= val ) {
	  left = middle;
	} else {
	  right = middle;
	}
  }
  return left;
}

int main() {
  auto primes = makePrimes( 2000000 );
  while( true ) {
	int n;
	cin >> n;
	if ( n == 0 ) break;

	if ( binary_search( primes->begin(), primes->end(), n ) ) {
	  cout << 0 << endl;
	} else {
	  int li = binary_search( primes, n );
	  cout << (*primes)[ li + 1 ] - (*primes)[ li ] << endl;
	}
  }
  delete primes;
}

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define int long long
typedef pair<int,int> P;

//-----------------------------------------------------------------------

const int SZ=1300000;
bool isPrime[SZ];

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

    for(int i=2;i<SZ;i++) isPrime[i]=true;
    for(int i=2;i<SZ;i++){
        if(!isPrime[i]) continue;
        for(int j=2*i;j<SZ;j+=i) isPrime[j]=false;
    }
    
    int n;
    while(cin>>n,n)
    {
        int p1=n,p2=n;
        while(!isPrime[p1]) p1++;
        while(!isPrime[p2]) p2--;
        cout<<p1-p2<<endl;
    }

}

C++: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> P;
const int pmax=1299709;
bool iscomp[pmax+1];
void init()
{
	P.reserve(100000);
	for(int i=2;i<pmax+1;i++){
		if(iscomp[i])continue;
		P.push_back(i);
		for(int j=2;i*j<pmax+1;j++)
			iscomp[i*j]=true;
	}
}
int main()
{
	init();
	while(true){
		int n;
		cin>>n;
		if(n==0)return 0;
		int pos=lower_bound(P.begin(),P.end(),n)-P.begin();
		if(P[pos]==n)cout<<0<<endl;
		else{
			cout<<P[pos+1]-P[pos]<<endl;
		}
	}
}