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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

const int maxn=10000000;
int f[maxn],prime[maxn];
 
int GenP() {
	int i,tot,p,tmp;
	tot=0;
	for (i=2;i<maxn;++i) f[i]=i;
	for (p=2;p<maxn;++p)
	{
		if (f[p]==p) prime[tot++]=p;
		for (i=0;i<tot && prime[i]<=f[p];++i)
		{
			tmp=prime[i]*p;
			if (tmp<maxn) 
				f[tmp]=prime[i];
			else
				break;
		}
	}
}


void mainmain(){
	GenP();
	set<int> se;
	rep(i,1000003){
		se.insert(prime[i]);
	}
	int n;
	while(cin>>n,n){
		
		auto it1=se.lower_bound(n);
		auto it2=se.upper_bound(n);
		if(*it1!=*it2){
			cout<<0<<endl;
			continue;
		}
		it2--;
		if(*it1==*it2){
			cout<<0<<endl;
		}
		else{
			cout<<*it1-*it2<<endl;
		}
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

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

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

int main()
{
    int n;

    while( cin >> n )
    {
        if( n == 0 )
            break;
        if( judgePrimeNumber(n) )
            cout << 0 << endl;
        else
        {
            int a = n + 1, b = n - 1;
            while( judgePrimeNumber(a) == false )
                a++;
            while( judgePrimeNumber(b) == false )
                b--;
            cout << a - b << endl;
        }
    }
    return 0;
}

C++: #line 1 "test/algebra/eratosthenes.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1276"

#include <bits/stdc++.h>
using namespace std;
using lint = long long;

#line 1 "test/algebra/../../library/algebra/eratosthenes.cpp"
// Eratosthenes's sieve
// create list of prime numbers in O(N)
// check if the given number is prime in O(1)
struct Sieve {
    vector<bool> isPrime;
    Sieve(size_t max) : isPrime(max + 1, true) {
        isPrime[0] = false;
        isPrime[1] = false;

        for (size_t i = 2; i * i <= max; ++i)
            if (isPrime[i])
                for (size_t j = 2; i * j <= max; ++j)
                    isPrime[i * j] = false;
    }
    bool operator()(size_t n) { return isPrime[n]; }
};
#line 8 "test/algebra/eratosthenes.test.cpp"

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Sieve isPrime(2000000);
    int n;
    while (cin >> n) {
        if (n == 0)
            break;
        int l = n, r = n;
        while (!isPrime(l))
            l--;
        while (!isPrime(r))
            r++;
        cout << r - l << "\n";
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;




const int INF = 1000000;
const int mod = 1e9 + 7;


int l = 1299710;

vi is_prime(l,1);
vi p;

void set_prime(){
	is_prime[0] = is_prime[1] = 0;
	for(int i = 2; i < l; i++){
		if(is_prime[i]){
			p.pb(i);
			for(int j = i + i; j < l; j += i) is_prime[j] = 0;
		}
	}
}


int main(){
	set_prime();
	int n;
	while(cin>>n){
		if(n == 0) break;
		if(is_prime[n]){
			cout<<0<<endl;
			continue;
		}
		vi::iterator s, e;
		s=e=upper_bound(all(p),n);
		s--;
		cout<<*e-*s<<endl;
	}

}

C++: #include<stdio.h>
int P(int x)
{
	if(x==2)return 1;
	if(x%2==0)return 0;
	for(int i=3;i*i<=x;i+=2)if(x%i==0)return 0;
	return 1;
}
int main()
{
	int r,n,i;
	while(scanf("%d",&n),n)
	{
		if(P(n))puts("0");
		else
		{
			r=2;
			for(i=1;!P(n+i);++i)++r;
			for(i=1;!P(n-i);++i)++r;
			printf("%d\n",r);
		}
	}
	return 0;
}