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 <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE 

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define REV(v) rbegin(v), rend(v)
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

const int N = 2e6;
int gap[N], np[N];
vector<int> pr;

int main(){
	ios::sync_with_stdio(false);

	for (int i = 2; i < N; ++i){
		if (!np[i]){
			pr.push_back(i);
			for (int j = i * 2; j < N; j += i) np[j] = 1;
		}
	}

	rep(i, pr.size() - 1){
		for (int j = pr[i] + 1; j < pr[i + 1]; ++j){
			gap[j] = pr[i + 1] - pr[i];
		}
	}

	int n;
	while (cin >> n, n){
		cout << gap[n] << endl;
	}




	return 0;
}

C++: #include <iostream>
#define P 1300000
using namespace std;
bool prime[P];

int main() {
  prime[0] = prime[1] = 1;
  for(int i=2;i*i<=P;i++)
    if(prime[i] == 0)
      for(int j=2;j<=P/i;j++) prime[i*j] = 1;

  while(1) {
    int n;
    cin >> n;
    if(n == 0) break;
    int cnt = n,cnt2 = n;
    while(prime[cnt] != 0) cnt++;
    while(prime[cnt2] != 0) cnt2--;

    cout << cnt-cnt2 <<endl;
  }

  return 0;
}

C++: #include <bitset>
#include <iostream>

using namespace std;

int main()
{
    const auto primes = [] {
        bitset<1'300'000> b;
        auto t = b;
        t.set();
        t[0] = false;
        t[1] = false;
        int i = 0;
        while (true) {
            bool done = false;
            while (!t[i]) {
                ++i;
                if (i == t.size()) {
                    done = true;
                    break;
                }
            }
            if (done) {
                break;
            }

            b[i] = true;
            for (int j = i; j < t.size(); j += i) {
                t[j] = false;
            }
        }
        return b;
    }();

    while (true) {
        int q;
        cin >> q;
        if (q == 0) {
            return 0;
        }

        int start = 2;
        for (int i = 2; i < primes.size(); ++i) {
            if (primes[i]) {
                auto stop = i;
                if (i == q) {
                    cout << 0 << endl;
                    break;
                }
                else if (start < q && q < stop) {
                    cout << (stop - start) << endl;
                    break;
                }
                start = i;
            }
        }
    }
}

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

int main()
{
  bool pri[1300000];
  //  vector<int> price;
  fill(pri,pri+1300000,true);
  pri[0]=pri[1]=false;
  for(int i=2;i<1300000;i++)
    {
      if(pri[i]==0)
	continue;
      //  price.push_back(i);	
      for(int j=i+i;j<1300000;j+=i)
	{
	  pri[j]=false;
	}
    }
  int n;
  for(;cin>>n,n;)
    { 
      
      if(pri[n])
	cout<<"0"<<endl;
      else
	{
	  int max,min;
	  for(int i=n-1;;i--)
	    {
	      if(pri[i])
		{
		  min=i;
		  break;
		}
	    }
	  for(int i=n+1;;i++)
	    {
	      if(pri[i])
		{
		  max=i;
		  break;
		}
	    }
	  cout<<max-min<<endl;
	}    
    }
 
}

C++: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int psize;
int prime[2000000];
bool bprime[2000000];

int main() {
  MEMSET(bprime, true);
  bprime[0] = bprime[1] = false;
  REP(i, 2000000) {
    if (!bprime[i]) { continue; }
    prime[psize++] = i;
    for (int j = i + i; j < 2000000; j += i) {
      bprime[j] = false;
    }
  }
  int n;
  while (scanf("%d", &n), n) {
    if (bprime[n]) { puts("0"); }
    else {
      int upper = *upper_bound(prime, prime + psize, n);
      int lower = *(upper_bound(prime, prime + psize, n) - 1);
      cout << upper - lower << endl;
    }
  }
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

const int MAX_N = 1300000;
vector<int> prime;
bool is_prime[MAX_N];
void sieve(int n){
    rep(i, 0, n + 1) is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;
    rep(i, 2, n + 1){
        if(is_prime[i]){
            prime.push_back(i);
            for(int j = 2*i; j <= n; j += i) is_prime[j] = false;
        }
    }
}

signed main(){
    sieve(MAX_N);
    int n;
    while(cin >> n, n){
        if(is_prime[n]){
            cout << 0 << endl;
            continue;
        }
        int ind = upper_bound(all(prime), n) - prime.begin();
        cout << prime[ind] - prime[ind - 1] << endl;
    }
}