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;
int X, Y, A, i, j;
int main() {
	while (true) {
		cin >> A; if (A == 0) { break; }
		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;
	}
}

C++: #include <map>
#include <set>
#include <list>
#include <cstdio>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define fr first
#define sc second
#define mp make_pair

typedef long long int64;
typedef pair< int, int > iP;
typedef pair< iP, int > iiP;

const int INF = 2 << 28;
const double EPS = 1e-10;

#define MAX 1300000

vector< int > p;
vector< bool > isP(MAX, true);

void makePrime() {
    isP[0] = isP[1] = false;
    for(int i = 2; i * i < MAX; i++)
        for(int j = i + i; j < MAX; j += i)
            isP[j] = false;

    for(int i = 2; i < MAX; i++) {
        if(isP[i]) p.push_back(i);
    }
}

int main() {

    makePrime();

    int k;
    while(cin >> k, k) {
        if(isP[k]) cout << 0 << endl;
        else cout << *upper_bound(p.begin(), p.end(), k) - *(lower_bound(p.begin(), p.end(), k) - 1) << endl;
    }
}

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

bool ipn(int n){//cout<<n<<endl;
  if(n == 2 || n == 3) return true;
  if(n <= 4 || n%2 == 0 || n%3 == 0) return false;
  for(int i = 6; (i-1)*(i-1) <= n; i+=6)
    if(n%(i-1) == 0 || n%(i+1) == 0) return false;
  return true;
}

int main(){
  int n, mi, ma;
  while(cin>>n, n){
    for(int i = n;;i++)
      if(ipn(i)){ma=i;break;}
    for(int i = n;;i--)
      if(ipn(i)){mi=i;break;}
    cout<<ma-n + n - mi<<endl;
  }

  return 0;
}

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

using namespace std;

#define INF 999999999

bool pr[1000000];

void erato(){
	pr[0] = 0;
	pr[1] = 0;
	pr[2] = 1;
	for(int i = 3;i<1000000;i++){
		pr[i]=1;
		for(int j=2;j*j<=i;j++){
			if(i%j==0){
				pr[i] = 0;
				break;
			}
		}
	}
}

int main(){
	int i, j, n;
	int ans;
	erato();
	while(1){
		cin >> n;
		if(!n)break;
		if(pr[n]) cout << "0" << endl;
		else{
			int less = n-1;
			while(!pr[less]){
				less--;
			}
			int more = n+1;
			while(!pr[more]){
				more++;
			}
			cout << more - less << endl;
		}
	}
	return 0;
}

C++: #include <stdio.h>
bool primes[1299710];
int main() {
    int i,j,k;for (i=0;i<1299710;primes[i++]=1);for (i=2;i*i<1299709;i++) if (primes[i]) for (j=i*i;j<1299709;primes[j]=0,j+=i);
    while (scanf("%d",&i)!=EOF && i) {k=0;for (j=i;j<1299709 && !primes[j];k++,j++);for (j=i;j>1 && !primes[j];k++,j--);printf("%d\n",k);}
    return 0;}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

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

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll inf =1LL << 62;

#define MAX 1299709+10
bool primes[MAX];

int main(void){
	rep(i,MAX+1)
		primes[i]=true;
	primes[0]=primes[1]=false;
	for(int i=2;i*i<=MAX;i++){
		if(primes[i])
			for(int j=2*i;j<=MAX;j+=i)
				primes[j]=false;

	}
	int n;
	while(cin >> n,n){
		int l=n;
		int r=n;
		while(!primes[l])
			l--;
		while(!primes[r])
			r++;
		cout << r-l << endl;
	}
	return 0;
}

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

const int n = 1299709;
bool is_prime[n + 1];
vector<int> prime_list;

void init() {
    for (size_t i = 2; i <= n; i++) {
        is_prime[i] = true;
    }
    for (int i = 2; i <= n; i++) {
        if (is_prime[i]) {
            prime_list.push_back(i);
            for (int j = 2 * i; j <= n; j += i) {
                is_prime[j] = false;
            }
        }
    }
}

void solve(const int& k) {
    if (is_prime[k]) {
        cout << 0 << endl;
        return;
    }
    auto greater_prime_number_iterator = upper_bound(prime_list.begin(), prime_list.end(), k);
    cout << *greater_prime_number_iterator - *(greater_prime_number_iterator - 1) << endl;
}

int main() {
    init();
    int k;
    while (true) {
        cin >> k;
        if (k == 0) {
            break;
        }
        solve(k);
    }
}