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;
//#define max 1000000;
typedef long int l;
l prime[1000000];

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

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

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

C++: #include <bits/stdc++.h>
using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<(n);++i)
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RREP(i, n) for(int i=(int)(n)-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=(int)(b)-1;i>=(a);--i)
#define ALL(a) a.begin(),a.end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2>
bool chmax(T1 &a, const T2 &b) {
    if (a < b) { a = b; return true; }
    return false;
}
template <typename T1, typename T2>
bool chmin(T1 &a, const T2 &b) {
    if (a > b) { a = b; return true; }
    return false;
}
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
    os << "{";
    int a = 0;
    for (auto &tp : mp) {
        if (a) os << ", "; a = 1;
        os << tp;
    }
    return os << "}";
}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << p.first << ":" << p.second;
    return os;
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    const int MAX = 1299709;
    vi isprime(MAX+1, 1);
    isprime[0] = isprime[1] = 0;
    vi primes;
    FOR(i, 2, MAX+1) {
        if (!isprime[i]) continue;
        primes.push_back(i);
        for (int j = 2*i; j <= MAX; j += i) {
            isprime[j] = 0;
        }
    }

    for (;;) {
        int k; cin >> k;
        if (k == 0) break;
        if (isprime[k]) {
            cout << 0 << endl;
            continue;
        }
        int ub = upper_bound(ALL(primes), k) - primes.begin();
        int ans = primes[ub] - primes[ub-1];
        cout << ans << endl;
    }

    return 0;
}

C++: #include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e8;
template<typename T> void cmax(T &a, T b) { a = max(a, b); }
template<typename T> void cmin(T &a, T b) { a = min(a, b); }
const int MAX_N = 2000006;
bool isPrime[MAX_N];
void eratos() {
  for (int i = 0; i < MAX_N; i++) isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;
  for (int i = 0; i < MAX_N; i++) {
    if (isPrime[i]) {
      for (int j = i + i; j < MAX_N; j += i) {
        isPrime[j] = false;
      }
    }
  }
}
signed main() {
  eratos();
  vector<int> prime;
  rep(i, MAX_N) if (isPrime[i]) prime.push_back(i);
  int n;
  while (cin >> n, n) {
    if (isPrime[n]) {
      cout << 0 << endl;
    } else {
      auto left = lower_bound(all(prime), n);
      left--;
      auto right = upper_bound(all(prime), n);
      cout << *right - *left << endl;
    }
  }
  return 0;
}

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

using i64 = int64_t;

constexpr i64 MAX_N = 1299709 + 1;

i64 prime[MAX_N]; // i番目の素数
bool is_prime[MAX_N + 1];   // is_prime[i]がtrueならばiは素数

// n以下の素数の数を返す
i64 sieve(i64 n) {
    i64 p = 0;
    for (i64 i = 0; i <= n; i++)
        is_prime[i] = true;
    is_prime[0] = false;
    is_prime[1] = false;
    for (i64 i = 2; i <= n; i++) {
        if (is_prime[i]) {
            prime[p++] = i;
            for (i64 j = 2 * i; j <= n; j += i)
                is_prime[j] = false;
        }
    }
    return p;
}

int main(){
    i64 n;
    sieve(MAX_N-1);
    while(cin >> n, n != 0){
        if(is_prime[n]){
            cout << 0 << endl;
            continue;
        }
        for(int i=0;i<100000;++i){
            if(prime[i] > n){
                cout << prime[i] - prime[i-1] << endl;
                break;
            }
        }
    }

    return 0;
}