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 primes[200000];
bool is(int n){
	for(int i=2;i*i<=n;i++)if(n%i==0)return false;
	return true;
}
int main(){
	int cnt=0,ii=2;
	while(true){
	if(cnt==200000)break;
	if(is(ii)){
	primes[cnt]=ii;
	cnt++;
	}
	ii++;
	}
	int n;
	//for(int i=0;i<20;i++)cout<<primes[i]<<" ";cout<<endl;
	while(true){
	cin>>n;
	if(n==0)break;
	bool f=0,f2=0;
	int a,b;
	for(int i=0;i<200000;i++){
	if(n==primes[i]){
	f2=1;
	continue;
	}
	if(n>primes[i])a=primes[i];
	if(n<primes[i]&&f==0){
	b=primes[i];
	f=1;
	}
	}
	if(f2||n<=2)cout<<0<<endl;
	else 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(10000001);

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

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

C++: #include <iostream>
using namespace std;
//#define max 1000000;

int prime[1000000];

void mk_prime(){
    for( int i = 0; i < 1000000; i++){
        prime[i] = 1;
    }
    prime[0] = 0;
    prime[1] = 0;
    for( int i = 2; i < 1000000; i++ ){
        if( prime[i] == 1 ){
            for( int j = 2 * i; j < 1000000; j += i ){
                prime[j] = 0;
            }
        }
    }
}

int main(){
    mk_prime();
    int k;

    while( cin >> k && k ){
        if( prime[k] ){
            cout << 0 << endl;
            continue;
        }
        int s, e;
        for( int i = k - 1; ; i-- ){
            if( prime[i] ){
                s = i;
                break;
            }
        }
        for( int i = k + 1; ; i++ ){
            if( prime[i] ){
                e = i;
                break;
            }
        }
        cout << e - s << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#define MAX 1299709
using namespace std;
int main()
{
	int gaps[MAX+1];
	bool used[MAX+1];
	fill(gaps,gaps+MAX+1,0);
	fill(used,used+MAX+1,0);
	int pos=0;
	for(int i = 2; i <= MAX; ++i)
	{
		if(used[i]) continue;
		gaps[pos++]=i;
		for(int j = i*2; j <= MAX; j+=i)
		{
			used[j]=true;
		}
	}

	int k;
	while(cin>>k,k) 
	{
		for(int i = 1; i < pos; ++i)
		{
			if(gaps[i-1]==k||gaps[i]==k)
			{
				cout<<0<<endl;
				break;
			}
			else if(gaps[i-1]<k&&k<gaps[i])
			{
				cout<<gaps[i]-gaps[i-1]<<endl;
				break;
			}
		}
	}
  return 0;
}

C++: #include<iostream>
using namespace std;
int primes[400003];
bool is(int n){
	for(int i=2;i*i<=n;i++)if(n%i==0)return false;
	return true;
}
int main(){
	int cnt=0,ii=2;
	while(true){
	if(cnt==400003)break;
	if(is(ii)){
	primes[cnt]=ii;
	cnt++;
	}
	ii++;
	}
	int n;
	while(cin>>n){
	int a=0,b=0;
	bool f=0;
	for(int i=0;i<400003;i++){
	if(primes[i]<=n)a=primes[i];
	if(f==0&&primes[i]>=n)b=primes[i],f=1;
	}
	if(n<=1)cout<<0<<endl;
	else cout<<b-a<<endl;
	}
	return 0;
}

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

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

int main(){
  vector<bool> hurui(1300000,true);
  hurui[0]=hurui[1]=false;
  repp(i,2,1300000){
    if(hurui[i]){
      for(int j=i+i;j<1300000;j+=i) hurui[j]=false;
    }
  }
  map<int,int> mp;
  int pre = 2;
  rep(i,1300000){
    if(hurui[i]){
      mp[pre]=i-pre;
      pre=i;
    }
  }
  int n;
  while(1){
    cin >> n;
    if(n==0) return 0;
    auto itr = mp.upper_bound(n);
    itr--;
    if(itr->first==n){
      cout << 0 << endl;
    }else{
      cout << itr->second << endl;
    }
  }
  return 0;
}