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>
#include <cmath>

#define N 1299710

using namespace std;

int main(){
	bool prime[N];
	for (int i = 0; i < N; i++) prime[i] = 1;
	prime[1] = 0;
	for (int i = 1; i < sqrt(N); i++) {
		if (!prime[i]) continue;
		else {
			for (int j = 2; i * j < N; j++) prime[i * j] = 0;	
		}
	}

	int k;
	while (cin >> k, k) {
		if (prime[k]) {
			cout << "0" << endl;	
		} else {
			int a, b;
			for (a = k; !prime[a]; a--);
			for (b = k; !prime[b]; b++);
			cout << b - a << endl;
		}	
	}
	return 0;
}

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

bool isPrime[1500000];

void setPrime() {
	isPrime[0] = isPrime[1] = false;
	for (int i = 2; i < 1500000; i++) { isPrime[i] = true; }
	for (int i = 2; i < 1500000; i++) {
		if (isPrime[i]) {
			for (int j = i * 2; j < 1500000; j += i) {
				isPrime[j] = false;
			}
		}
	}
}

int main() {
	setPrime();
	int n;
	while (cin >> n) {
		if (!n) break;
		if (isPrime[n]) { cout << 0 << endl; continue; }
		
		int l, r;
		for (l = n - 1; !isPrime[l]; l--);
		for (r = n + 1; !isPrime[r]; r++);
		cout << r - l << endl;
	}
	return 0;
}

C++: #include<iostream>
 
using namespace std;
 
const int MAX= 10000100;
bool prime[MAX];
void make_prime(){
  for(int i = 0 ; i < MAX ; i ++) prime[i]=true;
  prime[0]=prime[1]=false;
  for(int i = 0; i < MAX; i ++)
    if(prime[i]){
      for(int j = i * 2 ; j < MAX ; j += i)
	prime[j]=false;
    }
}
 
int main(void){
  make_prime();
  long long int n;
  while(cin>>n,n){
    if(prime[n]){
      cout<<0<<endl;
      continue;
    }
    long long int k,j;
    
    for(k = n; !prime[k]; k--);
    for(j = n; !prime[j]; j++);
    cout<<j-k<<endl;
    
  }
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

vector<int> erat(int n) {
    vector<int> primes(n);
    for (int i = 2; i < n; ++i) primes[i] = i;
    for (int i = 2; i*i < n; ++i)
        if (primes[i])
            for (int j = i*i; j < n; j+=i) primes[j] = 0;
    primes.erase(remove(primes.begin(), primes.end(), 0), primes.end());
    return primes;
}

signed main() {
    int n;
    vector<int> primes = erat(1300000);
    while(cin >> n, n) {
        int k = lower_bound(primes.begin(), primes.end(), n) - primes.begin();
        // printf("test: primes[%lld] = %lld\n", k, primes[k]);
        if(primes[k] == n) cout << 0 << endl;
        else cout << primes[k] - primes[k-1] << endl;
    }
    return 0;
}

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

using namespace std;
    vector<int> eratosu(12000710,1);

    int solve(int N){
        if(eratosu[N] == 1)return 0;
        int upIndex;
        for(upIndex = N; eratosu[upIndex] == 0; upIndex++);
        int dind = 0;
        for(dind = N; eratosu[dind] == 0; dind--);
        return upIndex - dind;
    }

int main(){

    eratosu[0] = 0;
    eratosu[1] = 0;

    for(int i = 2; i < eratosu.size(); i++){
        if(eratosu[i] == 1){
            for(int j = 2; j * i < eratosu.size(); j++){
                eratosu[i*j] = 0;
            }
        }
    }

    int N;
    vector<int> ans;
    while(1){
        cin>>N;
        if(!N)break;
        ans.push_back(solve(N));
    }

    for(int i : ans)cout<<i<<endl;
}

C++: #include <iostream>

using namespace std;

static constexpr auto Limit = 2000000;

int main()
{
    bool marked[Limit] = { true, true };
    bool is_prime[Limit] = {};

    for( int i=0; i<Limit; ++i ) {
        if ( marked[i] || is_prime[i] )
            continue;
        
        is_prime[i] = true;
        
        for( int j=i; j<Limit; j+=i ) {
            marked[j] = true;
        }
    }

    int n;
    while( cin >> n ) {
        if ( n == 0 )
            break;

        int gap = 0;
        if ( !is_prime[n] ) {
            while( !is_prime[--n] );

            ++gap;
            while( !is_prime[++n] )
                ++gap;
        }
        cout << gap << endl;
    }
}