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>
#define MAX_N 1299709 + 1
using namespace std;

bool t[MAX_N];
int k;

bool isPrime(int x) {
  if(x == 2) return true;
  if(x < 2 || x % 2 == 0) return false;
  int i = 3;
  while(i * i <= x) {
    if(x % i == 0) return false;
    i += 2;
  }
  return true;
}

void table() {
  for(int i = 1; i < MAX_N; ++i) {
    if (isPrime(i)) t[i] = 1;
  }
}

void solve() {
  int p = k, n = k;
  while(t[p] != 1) p--;
  while(t[n] != 1) n++;
  cout << n - p << endl;
}

int main() {
  table();
  while(cin >> k && k) {
    solve();
  }
  return 0;
}

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

using namespace std;

#define INF 999999999

bool pr[1300000];

void erato(){
	pr[0] = 0;
	pr[1] = 0;
	pr[2] = 1;
	for(int i = 3;i<1300000;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 <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#define MAX 1000001

bool p[MAX];

int main(){
  memset(p, 1, sizeof(p));
  p[0] = p[1] = 0;
  for(int i = 0 ; i < MAX ; i++){
    if(p[i]){
      for(int j = 2 * i ; j < MAX ; j += i){
	p[j] = 0;
      }
    }
  }

  int n;
  while(cin >> n, n){
    int next, prev;
    if(p[n] == 1) cout << 0 << endl;

    else{
      for(int i = n ; ; i++){
	if(p[i] == 1){
	  next = i;
	  break;
	}
      }
      for(int i = n ; ; i--){
	if(p[i] == 1){
	  prev = i;
	  break;
	}
      }
      cout << next - prev << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int PRIME_MAX = 1299710;
bool isPrime[PRIME_MAX];
int main () {
	fill(isPrime, isPrime + PRIME_MAX, true);
	isPrime[0] = isPrime[1] = false;
	for (int i = 2; i * i <= PRIME_MAX; i++) {
		if (isPrime[i]) {
			for (int j = i << 1; j < PRIME_MAX; j += i) {
				isPrime[j] = false;
			}
		}
	}
	int k;
	while (cin >> k, k) {
		int lo, hi;
		lo = hi = k;
		while (!isPrime[lo]) lo--;
		while (!isPrime[hi]) hi++;
		cout << hi - lo << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, lint> pll;
template<class T>bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod*mod;
constexpr int MAX = 100010;

vector<int> min_factor, prime;

void sieve(int n){
    min_factor.resize(n+1, 0);
    For(i, 2, n+1){
        if(min_factor[i] == 0){
            min_factor[i] = i;
            prime.push_back(i);
        }
        for(int x: prime){
            if(x*i > n || x > i) break;
            min_factor[x*i] = x;
        }
    }
}

int main(){
    sieve(1300000);
    int n;
    while(scanf("%d", &n) && n){
        auto t = lower_bound(prime.begin(), prime.end(), n);
        printf("%d\n", *t == n ? 0 : *t-*(t-1));
    }
}

C++: #include <bits/stdc++.h>
#define pb push_back

using namespace std;

typedef pair<int, int> P;
typedef long long int LL;

int main() {
    vector<bool> v(1300000, false);

    for (int i = 2; i < 1200; i++) {
        for (int j = 2; j * i <= 1300000; j++) {
            v[j * i] = true;
        }
    }

    int k;
    while (cin >> k, k) {
        int a = -1, b = -1;
        for (int i = 1; true; i++) {
            if (!v[k]) break;
            if (a != -1 && b != -1) break;

            if (k - i > 0 && !v[k - i] && a == -1) a = k - i;
            if (k + i < 1300000 && !v[k + i] && b == -1) b = k + i;
        }
        cout << b - a << endl;
    }

    return 0;
}

C++: 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#include <cstring>
#include <cstdio>
#include <stack>
#define INF 1000000000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;
vector<ll> p(1300001);

void era(){
    for (int i=2; i<1300001; i++) {
        if(p[i]==0)continue;
        int j=i;
        while (j<1300001) {
            j+=i;
            if(j>=1300001)break;
            p[j]=0;
        }
    }
}

int main(){
    rep(i, 1300001) p[i]=i;
    era();
    for(;;){
        ll n;
        cin >>n;
        if(!n) break;
        ll ans = 0;
        ll i=n,j=n;
        while (p[i]==0) {
            i++;
        }
        while (p[j]==0) {
            j--;
        }
        ans = p[i]-p[j];
        cout << ans <<endl;
    }
}