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++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
	vector<bool> a(2000000, true);
	a[0] = a[1] = false;
	for (int i = 0; i < 2000000; i++)
		if (a[i])
			for (int j = i * 2; j < 2000000; j += i)
				a[j] = false;
	vector<int> p;
	for (int i = 0; i < 2000000; i++)
		if(a[i]) p.push_back(i);
	for (;;) {
		int k; cin >> k;
		if (k == 0) break;
		int i = upper_bound(p.begin(), p.end(), k) - p.begin() - 1;
		int j = lower_bound(p.begin(), p.end(), k) - p.begin();
		cout << p[j] - p[i] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
deque<int> Era(int n) {
    deque<int> arr(n+1,1);
    deque<int> prime;
    arr[0]=0;
    arr[1]=1;
    for (int i=2;i*i<n;i++) {
        if (arr[i]==1) {
            for (int j=i*i;j<n;j+=i) {
                arr[j]=0;
            }
        }
    }
    for (int k=2;k<=n;k++) {
        if (arr[k]==1) {
            prime.emplace_back(k);
        }
    }
    return prime;
}
int main(void){
    std::deque<int> deq;
    deq=Era(1300000);
    int n;
    while (cin>>n) {
        if (n==0) {
            break;
        } else {
            if (find(deq.begin(),deq.end(),n)!=deq.end()) {
                cout<<0<<endl;
            } else {
                auto back=lower_bound(deq.begin(),deq.end(),n);
                auto front=back--;
                cout<<*front-*back<<endl;
            }
        }
    }
}

C++: #include <iostream>
#define MAX 1299711

using namespace std;

bool prime[MAX];

void isPrime(void){

  fill(prime, prime + MAX , true);
  prime[0] = prime[1] = false;
  for(int i = 2 ; i*i < MAX ; i++){
    if(prime[i]){
      for(int j = i*i ; j < MAX ; j += i) prime[j] = false;
    }
  }
}

int main(void){

  isPrime();

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

C++: #include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
#define rep(i,n) range(i,0,n)
#define range(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define LINF    ((ll)1ll<60)
#define INF     ((int)1<<30)
#define EPS     (1e-9)
#define MOD     (1000000007)
template<class S,class T>ostream& operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool b=1;for(auto s:t)os<<(exchange(b,0)||strlen(abi::__cxa_demangle(typeid(S).name(),0,0,0))>20?"":" ")<<s;return os<<endl;}
class Eratosthenes{
  public:
    int n, sqlim;
    vector<bool> prime;
    Eratosthenes(int N):n(N+1){
        sqlim=(int)ceil(sqrt(n));
        prime=vector<bool>(n,1); prime[0]=prime[1]=0;
        for(int i=2;i<=sqlim;i++) if(prime[i]) for(int j=i*i;j<=n;j+=i) prime[j]=0;}
    vector<int> primeArray(int s=0, int l=10000){vi ret; for(int i=s;ret.size()!=l;i++) if(prime[i]) ret.push_back(i); return ret;}
};


int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	vi v=Eratosthenes(1299710).primeArray(0,100000);
	set<int> prime(all(v));
	vi ans(1299710,0);
	int p=0,l=0;
	rep(i,1299710){
	    if(prime.find(i)!=prime.end()){
	        range(j,p,i) ans[j]=l+1;
	        p=i+1,l=0;
	    }else l++;
	}
	int n;
	while(cin>>n,n){
		cout<<ans[n]<<endl;
	}
	return 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;
}

C++: #include <stdio.h>
#include <math.h>
#define MAXNUM 1299709

int main(void){
	int flag[MAXNUM + 1], i, j, n;
	for(i = 0;i <= MAXNUM;i++)
		flag[i] = 1;
	for(i = 2;i <= sqrt((double)MAXNUM);i++){
		for(j = i * 2;j <= MAXNUM;j += i)
			flag[j] = 0;
	}
	flag[0] = flag[1] = 0;
	while(1){
		scanf("%d",&n);
		if(!n)
			break;
		for(i = n;!flag[i];i++);
		for(j = n;!flag[j];j--);
		printf("%d\n",i - j);
	}
	return 0;
}