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 <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <iomanip>
#include <list>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
template<class T> inline T sqr(T x) { return x*x; }
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;

#define For(i,a,b)	for(int (i) = (a);i < (b);(i)++)
#define rep(i,n)	For(i,0,n)
#define clr(a)		memset((a), 0 ,sizeof(a))
#define mclr(a)		memset((a), -1 ,sizeof(a))
#define all(a)		(a).begin(),(a).end()
#define rall(a)		(a).rbegin(), (a).rend()
#define sz(a)		(sizeof(a))
#define Fill(a,v)	fill((int*)a,(int*)(a+(sz(a)/sz(*(a)))),v)

bool cheak(int x, int y, int xMax, int yMax){ return x >= 0 && y >= 0 && xMax > x && yMax > y; }
const int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
const int mod = 1000000007;
const int INF = 1e9;

vector<bool> era;

void initera(int n){
	era.resize(n, true);

	era[0] = false; era[1] = true;
	for (int i = 2; i < n; i++){
		if (era[i])for (int j = i * 2; j < n; j += i)
			era[j] = false;
	}
}

int main()
{
	initera(1299709+10);
	int n;
	while (cin >> n && n){
		int k = 0;
		int a, b;
		for (a = n; !era[a]; a--);
		for (b = n; !era[b]; b++);
		cout << b - a << endl;
	}
	return 0;
}

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

int main()
{
	vector<bool> numbers(1300000, true);
	vector<int> primes;

	numbers[0] = false;
	numbers[1] = false;

	for (int i = 2; i <= sqrt(1300000); i++) {
		for (int j = i + i; j < 1300000; j += i) {
			numbers[j] = false;
		}
	}

	for (int i = 2; i < 1300000; i++) {
		if (numbers[i] == true) {
			primes.push_back(i);
		}
	}

	while (true) {
		int n;
		cin >> n;

		if (n == 0) {
			break;
		}

		for (int i = 0; i < primes.size(); i++) {
			if (n == primes[i]) {
				cout << 0 << endl;
				break;
			}
			else if (n > primes[i] && n < primes[i + 1]) {
				cout << primes[i + 1] - primes[i] << endl;
				break;
			}
		}
	}

	return 0;
}

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

typedef long long ll;

vector<ll> prime;

int main(){
  prime.push_back(2);
  for(ll i=3; i<1300000; i+=2){
    bool flag=true;
    for(ll j=0; j<prime.size() && i>=prime[j]*prime[j]; j++){
      if(i%prime[j]==0){flag=false; break;}
    }
    if(flag)prime.push_back(i);
  }
  while(true){
    int n;
    cin>>n;
    if(n==0)break;
    ll to;
    for(to=0; n>prime[to]; to++);
    if(n==prime[to])cout<<"0"<<endl;
    else cout<<prime[to]-prime[to-1]<<endl;
  }
  return 0;
}

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

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
//#define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

bool sieve[1299709+1];

int main() {
    sieve[0] = sieve[1] = true;
    FOR(i, 2, 1299709+1) {
        if (sieve[i]) continue;
        for (int j = i+i; j < 1299709+1; j+= i) {
            sieve[j] = true;
        }
    }
    int k;
    while (cin >> k, k) {
        if (!sieve[k]) {
            cout << "0" << endl;
        } else {
            int a, b;
            FOR(i, k, 1299709+1) {
                if (!sieve[i]) {
                    b = i;
                    break;
                }
            }
            RREP(i, k) {
                if (!sieve[i]) {
                    a = i;
                    break;
                }
            }
            cout << b - a << endl;
        }
    }
}