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;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

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

vector<int> primes;
bool isNotPrime[1300100];

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    primes.emplace_back(2);
    for(int i=3;i<=1300000;i+=2){
        if(!isNotPrime[i]){
            primes.emplace_back(i);

            for(int j=2*i;j<=1300000;j+=i){
                isNotPrime[j] = true;
            }
        }
    }

    while(true){
        int N;
        std::cin >> N;
        if(!N){break;}
    
        auto it = lower_bound(primes.begin(), primes.end(), N);

        if(*it == N){
            std::cout << 0 << std::endl;
        }else{
            std::cout << (*it - *(it - 1)) << std::endl;
        }
    }
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

using Tup = tuple<int64, int64, int64>;

int32 prime[1333456] = {};

void init(){
	FOR(i, 2, 1333456){
		if(prime[i] == 0){
			for(int32 j = 2;i*j < 1333456;j++){
				prime[i*j] = 1;
			}
		}
	}
	REP(i, 1333455){
		if(prime[i+1] != 0){
			prime[i+1] += prime[i];
		}
	}
	for(int32 i = 1333454;i >= 0;i--){
		if(prime[i] != 0){
			chmax(prime[i], prime[i+1]);
		}
	}
}

int main(void){
	int32 n;
	init();
	while(cin >> n && n){
		cout << (prime[n] ? prime[n]+1 : 0) << endl;
	}
}

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

int prime[100010];
bool p[1299710];

void eratos(){
    fill(p, p+1299710, true);
    p[0]=p[1]=false;
    for(int i=2; i*i<=1299709; i++){
        if(p[i]){
            for(int j=i; i*j<=1299709; j++){
                p[i*j]=false;
            }
        }
    }
    int now=0;
    for(int i=2; i<=1299709; i++){
        if(p[i]){
            prime[now++]=i;
        }
    }
}

int main(){
    int n;
    eratos();
    
    while(cin>>n, n){
        if(p[n]) {
            cout<<0<<"\n";
        }
        else{
            int idx=(int)(lower_bound(prime, prime+100001, n)-prime);
            cout << prime[idx]-prime[idx-1]<<"\n";
        }
    }
    
}

C++: #include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed
#define INF INT_MAX/2-1
#define pb push_back

using namespace std;

vector<int> primes;

bool isPrime(int p) {
  for (int i = 0; i < primes.size() && primes[i] <= sqrt(p); i++) {
    if (p % primes[i] == 0) {
      return false;
    }
  }
  return true;
}

void makePrimes(int n) {
  primes.pb(2);
  for (int i = 3; i <= n; i += 2) {
    if (isPrime(i)) {
      primes.pb(i);
    }
  }
}

int main() {
  makePrimes(1299709);
  int n;
  while (cin >> n, n) {
    for (int i = 0; i < 100000; i++) {
      if (primes[i] == n) {
        cout << "0" << el;
        break;
      } else if (primes[i] > n) {
        cout << primes[i] - primes[i-1] << el;
        break;
      }
    }
  }
}

C++: #include <iostream>

using namespace std;

const int max_p = 1300000;
bool prime[max_p + 1];

void initial_prime()
{
    for (int i = 0; i < max_p; i++)
        prime[i] = true;
    prime[0] = prime[1] = false;

    for (int i = 2; i * i < max_p; i++)
        if (prime[i])
            for (int j = 2 * i; j <= max_p; j += i)
                prime[j] = false;
}

int main()
{
    int k;
    
    initial_prime();

    while (cin >> k, k) {
        int l, r;
        int ans = 0;

        l = r = k;
        while (!prime[l--])
            ans++;
        while (!prime[r++])
            ans++;
        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
using namespace std;
//#define max 1000000;
typedef long 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;
            }
        }
        int ans = e - s;
        cout << ans << endl;
    }
}