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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
//const ll mod = 1000000007;
vector<ll> Primes;

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    for(ll i = 2; i <= 2e6; i++) {
        bool ok = true;
        for(ll j = 2; j * j <= i; j++) {
            if(i % j == 0) {
                ok = false;
                break;
            }
        }
        if(ok) Primes.push_back(i);
    }
    ll N;
    while(cin >> N) {
        if(N == 0) break;
        auto itr = lower_bound(Primes.begin(), Primes.end(), N);
        if(*itr == N) {
            cout << 0 << endl;
            continue;
        }
        ll U = *itr;
        itr--;
        cout << U - *itr << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

bool is_prime[2000000];
int prime[2000000];
int main() {
	memset(is_prime, 1, sizeof(is_prime));
	is_prime[0] = is_prime[1] = false;
	int p = 0;
	for (int i = 2; i < 2000000; i++) {
		if (is_prime[i]) {
			prime[p++] = i;
			for (int j = i * 2; j < 2000000; j += i)is_prime[j] = false;
		}
	}
	int n;
	while (scanf("%d", &n), n) {
		if (is_prime[n]) {
			puts("0");
			continue;
		}
		int*a = lower_bound(prime, prime + p, n);
		if (a == prime)puts("0");
		else printf("%d\n", *a - *(a - 1));
	}
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<int> get_prime(int n) {
    if(n <= 1) return vector<int>{};
    vector<bool> is_prime(n+1, true);
    vector<int> prime;
    for (int i = 2; i <= n; ++i) {
        if(!is_prime[i]) continue;
        prime.emplace_back(i);
        for (int j = i*2; j <= n; j += i) {
            is_prime[j] = false;
        }
    }
    return prime;
}

int main() {
    const auto primes = get_prime(1299710);
    set<int> s;
    for (auto &&i : primes) s.insert(i);
    int n;
    while(cin >> n, n){
        auto ub = s.upper_bound(n);
        auto lb = ub;
        lb--;
        cout << n << "\n";
        if(*lb == n){
            puts("0");
            continue;
        }else {
            printf("%d\n", *ub-*lb);
        }
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
bool s[1300000];
int main(){
  for(int i=2;i*i<1299709;i++)
    if(!s[i])for(int j=2;j*i<1299709;j++)s[i*j]=1;
  int n;
  while(cin>>n&&n){
    int an=0;
    for(int i=n;s[i];i++)an++;
    for(int i=n;s[i];i--)an++;
    cout<<an<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string.h>

using namespace std;
vector<int> prime;
bool isPrime[1300000];

void PrimeCalc()
{
	memset(isPrime, 1, sizeof(isPrime));
	isPrime[0] = isPrime[1] = false;
	for(int i = 2; i < 1300000; ++i)
	{
		if(isPrime[i])
		{
			prime.push_back(i);
			for(int j = 2 * i; j < 1300000; j += i)
			{
				isPrime[j] = false;
			}
		}
	}
}

void solve()
{
	PrimeCalc();
	int n;
	while(cin >> n, n)
	{
		if(isPrime[n])
		{
			cout << 0 << endl;
			continue;
		}
		for(int i = 0; i < prime.size() - 1; ++i)
		{
			if(prime[i] <= n && n <= prime[i + 1])
			{
				cout << prime[i + 1] - prime[i] << endl;
				break;
			}
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include "bits/stdc++.h"
#define pb push_back
#define mt make_tuple
#define all(a) (a).begin(),(a).end()
#define frep(i,a,n) for(int i=(a);i<(n);++i)
#define rep(i, n) for(int i=0;i<(n);++i)
#define rrep(i, n) for(int i=n-1;i>=0;--i)
#define debug(x) cout << #x << " : " << x << " ------------ " << endl;
using namespace std;
int main()
{
    int N = 1299710;
    bool tmp[N + 1];
    fill(tmp, tmp+N+1, true);
    vector<int> prime(100000);
    tmp[0] = tmp[1] = false;
    frep(i, 2, N) {
        if (tmp[i]) {
            for (int j = i + i; j < N; j += i) {
                tmp[j] = false;
            }
            prime.pb(i);
        }
    }
    
    int n;
    while (cin >> n, n) {
        auto it = upper_bound(all(prime), n);
        if (*(it-1) == n) 
            cout << 0 << endl;
        else 
            cout << (*it - *(it-1)) << endl;
    }
    
    return EXIT_SUCCESS;
}