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 rep(i, n) REP((i), 0, (n))
#define REP(i, a, n) for(int i=(a); i<(n); i++)

int N = 1300000;
bool is_prime[1300000];

void set_prime() {
    memset(is_prime, true, N);
    is_prime[0] = is_prime[1] = false;
    REP(i, 2, sqrt(N)) {
        if(is_prime[i]) {
            for(int j=0; i*(j+2)<N; j++) {
                is_prime[i*(j+2)] = false;
            }
        }
    }
}

int main() {
    set_prime();
    int k;
    while(1) {
        cin >> k;
        if(k == 0) break;
        if(is_prime[k]) {cout << 0 << endl; continue;}
        int l, r;
        for(l=k-1; !is_prime[l]; l--);
        for(r=k+1; !is_prime[r]; r++);
        cout << r - l << endl;
    }
}

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,p=2; 
  while(cin>>n&&n){
    int an=0;
    for(int i=n;s[i];i++)an++;
    for(int i=n;s[i];i--)an++;
    cout<<max(an-1,0);
  }
  return 0;
}

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

int main()
{
  static const int M = 1299709;
  vector<int> primes;
  vector<bool> sieve(M+1, false);
  sieve[1] = true;
  for (int i = 2; i <= M; i++) {
    if (!sieve[i]) {
      primes.push_back(i);
      for (int j = i+i; j <= M; j += i) {
        sieve[j] = true;
      }
    }
  }

  int k;
  while (cin >> k && k != 0) {
    vector<int>::const_iterator it = lower_bound(primes.begin(), primes.end(), k);
    if (*it == k) {
      cout << 0 << endl;
    } else {
      cout << (*it - *(it-1)) << endl;
    }
  }
  return 0;
}

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

const int MAX = 10001000;
bool IsPrime[MAX];
vector<int> Era(int n = MAX) {
    vector<int> res;
	IsPrime[0] = false; IsPrime[1] = false; 
	for (int i = 2; i < n; ++i) IsPrime[i] = true;
	for (int i = 2; i < n; ++i) {
		if (IsPrime[i]) {
            res.push_back(i);
			for (int j = i*2; j < n; j += i) IsPrime[j] = false;
		}
	}
    return res;
}

int main() {
  Era();
  long long N;
  while (cin >> N) {
	if (N == 0) break;
	int prev = N, next = N;
	while (!IsPrime[prev]) --prev;
	while (!IsPrime[next]) ++next;
	cout << next - prev << endl;
  }
}

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

int main(){
  vector<bool> prime(1299709,1);
  prime[1]=0;
  for(int i=2;i<prime.size();i++){
    if(prime[i]==1){
      for(int j=i*2;j<prime.size();j+=i)prime[j]=0;
    }
  }
  //for(int i=0;i<prime.size();i++)if(prime[i]==1)cout<<i<<" ";
  while(1){
    int n;
    cin>>n;
    if(n==0)break;
    int ans=0;
    for(int i=0;i<prime.size();i++){
      if(prime[n+i]==1)break;
      ans++;
    }
    for(int i=0;i<prime.size();i++){
      if(prime[n-i]==1)break;
      ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#define MAX_N 1299709 + 1
using namespace std;

bool t[MAX_N];
int k;

bool isPrime(int x) {
  if(x == 2) return true;
  if(x < 2 || x % 2 == 0) return false;
  int i = 3;
  while(i * i <= x) {
    if(x % i == 0) return false;
    i += 2;
  }
  return true;
}

void table() {
  for(int i = 1; i < MAX_N; ++i) {
    if (isPrime(i)) t[i] = 1;
  }
}

void solve() {
  int p = k, n = k;
  while(t[p] != 1) p--;
  while(t[n] != 1) n++;
  cout << n - p << endl;
}

int main() {
  table();
  while(cin >> k && k) {
    solve();
  }
  return 0;
}

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

vector<int> p;

void solve(int n){
	auto ub = upper_bound(p.begin(), p.end(), n);
	auto lb = lower_bound(p.begin(), p.end(), n);
	if(ub != lb) cout << 0 << "\n";
	else cout << *ub - *(ub-1) << "\n";
}

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	for(int i=2; i<=1299709; ++i){
		bool f = true;
		for(int j=0; j<p.size()&&p[j]*p[j]<=i; ++j)
			if(i % p[j] == 0){
				f = false;
				break;
			}
		if(f) p.push_back(i);
	}
	while(1){
		int n;
		cin >> n;
		if(n == 0) return 0;
		solve(n);
	}
}

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

//#define int int64_t
#define REP(i, s, n) for (signed i = (s); i < signed(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) begin(c), end(c)


#define maxup(ans, x) (ans = (ans < x ? x : ans))
#define minup(ans, x) (ans = (ans > x ? x : ans))

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

signed main() {
	int k;
	while (cin >> k && k) {
		int p;
		auto isprime = [](int x) {
			for (int i = 2; i * i <= x; i++) {
				if (x % i == 0) return false;
			}
			return true;
		};
		for (int i = k; i >= 2; i--) {
			if (isprime(i)) {
				p = i;
				break;
			}
		}
		int pn;
		REP(i, k, 1 << 29) {
			if (isprime(i)) {
				pn = i;
				break;
			}
		}
		cout << pn - p << endl;
	}
}

C++: 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
#include<iostream>
using namespace std;
int primes[200003];
bool is(int n){
    for(int i=2;i*i<=n;i++)if(n%i==0)return false;
    return true;
}
int main(){
    int cnt=0,ii=2;
    while(true){
    if(cnt==200003)break;
    if(is(ii)){
    primes[cnt]=ii;
    cnt++;
    }
    ii++;
    }
    int n;
    while(cin>>n){
if(n==0)break;
    int a=0,b=0;
    bool f=0;
    for(int i=0;i<200003;i++){
    if(primes[i]<=n)a=primes[i];
    if(f==0&&primes[i]>=n)b=primes[i],f=1;
    }
    if(n<=1)cout<<0<<endl;
    else cout<<b-a<<endl;
    }
    return 0;
}