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 "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "iomanip"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

list<int> Prime(int num) {
	list<int>P;
	P.push_back(2);
	P.push_back(3);
	for (int i = 5; i <= num; i += 6) {
		bool flag = true;
		for (auto j : P) {
			if (j*j > i) {
				break;
			}
			if (i%j == 0) {
				flag = false;
				break;
			}
		}
		if (flag)P.push_back(i);
		flag = true;
		for (auto j : P) {
			if (j*j > i + 2) {
				break;
			}
			if ((i + 2) % j == 0) {
				flag = false;
				break;
			}
		}
		if (flag)P.push_back(i + 2);
	}
	return P;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	list<int>P = Prime(1299709);
	cin >> N;
	vector<int>num(1299710);
	int bef = 2;
	for (auto i : P) {
		num[i] = 0;
		for (int j = bef+1; j < i; j++) {
			num[j] = i - bef;
		}
		bef = i;
	}
	while (N) {
		cout << num[N] << endl;
		cin >> N;
	}
	return 0;
}

C++: #include<iostream>
#include<cmath>
#define N 1299709
using namespace std;
int prime[N+1];

int i,j;

void init()
{
    for(i=0;i<=N;i+=2)
        prime[i]=0;
    prime[2]=1;
    prime[1]=1;
    for(i=3;i<=N;i+=2)
        prime[i]=1;
    for(i=3;i<=sqrt(N);i+=2)
    {
        if(prime[i])
            for(j=i+i;j<=N;j+=i) prime[j]=0;
    }
}

int n;

void result()
{
    if(prime[n]==1) cout<<"0"<<endl;
    else
    {
        //cout<<"taibangle"<<endl;
        int p=n,q=n;
        while(prime[p]==0)
        {
            p--;
        }
        while(prime[q]==0)
        {
            q++;
        }
        //cout<<p<<"  "<<q<<endl;
        cout<<q-p<<endl;
    }
}

int main()
{
    init();
    while(cin>>n,n)
    {
        result();
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <tuple>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long>vll;
typedef pair<int,int> pi;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

vector<int> sieve(int n){
  vector<int> ret;
  vector<bool> is_prime(n + 1, true);

  is_prime[0] = is_prime[1] = false;
  for(int i = 2; i < n + 1; i++){
    if(is_prime[i]){
      ret.push_back(i);
      for(int j = i; j < n + 1; j += i){
	is_prime[j] = false;
      }
    }
  }
  
  return ret;
}

int main(){
  int n;
  vector<int> prime = sieve(1299710);

  while(cin >> n, n){
    int idx = lower_bound(all(prime), n) - prime.begin();
    if(prime[idx] == n) cout << 0 << endl;
    else cout << prime[idx] - prime[idx - 1] << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<cmath>
#include<set>
#define MAX 1299710
using namespace std;

bool isPrime[MAX];
set<int> pNum;
void prime(){
  isPrime[0]=isPrime[1]=true;
  for(int i=2;i<MAX;i++){
    if(isPrime[i]==false)pNum.insert(i);
    for(int j=2*i;j<MAX;j+=i)isPrime[j]=true;
  }
}

int main(){
  prime();
  int n;
  while(cin>>n,n){
    int forward,back;
    set<int>::iterator ite;
    ite=pNum.lower_bound(n);
    forward=*ite;
    if(forward!=n && n!=2)ite--;
    back = *ite;
    cout << forward-back << endl;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int main(void)
{
	int n;
	int min,max;
	int check;

	int i;

	while(1){
		cin >> n;
		if (n==0) break;

		min=n;
		
		while(1){

			check=0;
			for(i=2;i*i<=min;i++){
				if(min%i==0){
					check=1;
					break;
				}
			}
			if(check==0) break;
			else min--;

		}
		max=n;
		while(1){

			check=0;
			for(i=2;i*i<=max;i++){
				if(max%i==0){
					check=1;
					break;
				}
			}
			if(check==0) break;
			else max++;
		}

		cout << max-min << endl;
	}

	return 0;
}

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

vector<int> sieve(int n) {
	vector<int> res(1, 2);
	vector<bool> prime(n, true);
	for(int i = 3; i < n; i += 2) {
		if(prime[i]) {
			res.push_back(i);
			for(int j = i * 3; j < n; j += i * 2)
				prime[j] = false;
		}
	}

	return res;
}

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

	const int MAX = 1299709;
	vector<int> primes = sieve(MAX);
	for(int k; cin >> k, k;) {
		int idx = lower_bound(primes.begin(), primes.end(), k) - primes.begin();
		if(primes[idx] == k)
			cout << 0 << endl;
		else
			cout << primes[idx] - primes[idx - 1] << endl;
	}

	return EXIT_SUCCESS;
}