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

using namespace std;

#define INF 1010001000
#define EPS 1e-10
#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back

typedef pair<int, int> i_i;
typedef long long int llong;
typedef pair<llong, llong> ll_ll;
typedef struct edge {int f, t;}Edge;

void sieve();

vector<bool> prime(1299710, true);
int n = 1299710;


int main()
{
    sieve();
    int a;
    while (cin >> a, a) {
        int bef, aft, ptr = a;
        while (1) {
            if (prime[ptr]) {
                bef = ptr;
                break;
            } else {
                ptr--;
            }
        }
        ptr = a;
        while (1) {
            if (prime[ptr]) {
                aft = ptr;
                break;
            } else {
                ptr++;
            }
        }
        cout << aft - bef << endl;
    }
    return 0;
}


void sieve()
{
    prime[0] = prime[1] = false;
    for (int i = 2; i * i < n; i++) {
        if (prime[i]) {
            for (int j = 2; j * i < n; j++) {
                prime[i*j] = false;
            }
        }
    }
}

C++: #include <bits/stdc++.h>

bool isprime[1600000];

void solve(int n) {
  int left = n, right = n;
  for(int i = n; i > 0; --i) {
    if( isprime[i] ) {
      left = i;
      break;
    }
  }
  for(int i = n; i < 1600000; ++i) {
    if( isprime[i] ) {
      right = i;
      break;
    }
  }
  std::cout << right - left << std::endl;
}

int main() {

  for(int i = 0; i < 1600000; ++i) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for(int i = 2; i < 4000; ++i) {
    if( isprime[i] ) {
      for(int j = i + i; j < 1600000; j+=i) {
        isprime[j] = false;
      }
    }
  }

  for(;;) {
    int n;
    std::cin >> n;
    if( n == 0 ) break;

    solve(n);
  }

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <sstream>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <complex>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <time.h>
#include <tuple>
#define all(c) ((c).begin(),(c).end())
#define rall(c) ((c).rbegin(),(c).rend())
//#define sort(v,n) sort(v,v+n)
#define ll long long
#define fi first
#define se second
#define inf 999999999
using namespace std;
const ll MOD = 1e9 + 7;
const double PI = acos(-1.0);
//---------------------------------------------------------------------------------------------//
int n;
bool a[1299710];

inline void prime() {
	int now = 2;
	while (now<=650000) {
		if (a[now] == 1) {
			now++;
			continue;
		}
		for (int i = now*2; i <= 1299709; i += now) {
			a[i] = 1;
		}
		now++;
	}
	a[0] = 1;
	a[1] = 1;
}

int main() {
	prime();
	while (cin >> n, n) {
		if (a[n] == 0) {
			cout << 0 << endl;
		}
		else {
			int c, d;
			for (int i = n - 1;; i--) {
				if (a[i] == 0) {
					c = i;
					break;
				}
			}
			for (int i = n + 1;; i++) {
				if (a[i] == 0) {
					d = i;
					break;
				}
			}
			cout << d - c << endl;
		}
	}
	return 0;
}

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

#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)

const int LIM = 1299709;

bool is_prime(int n) {
    if(n < 2) return false;
    if(n == 2) return true;
    if(n % 2 == 0) return false;
    for(int i=3; i*i<=n; i+=2) if(n % i == 0) return false;
    return true;
}

void solve(int k) {
    int a=0, b=0;
    for(int i=k+1; ; ++i) if(is_prime(i)) {
        b = i; break;
    }
    for(int i=k-1; i>=0; --i) if(is_prime(i)) {
        a = i; break;
    }
    if(a == 0 or b == 0) {
        puts("0"); return ;
    }
    for(int i=a+1; i<=b-1; i++) if(is_prime(i)) {
        puts("0"); return ;
    }
    cout << b - a << "\n";
}

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

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int N = 1299710;
  vector<int> prime(N);
  REP(i,N) prime[i] = i;
  prime[1] = 0;
  for (int i=2;i<N;++i)
    if (prime[i])
      for (int j=i*2; j<N; j+=i)
        prime[j] = 0;
  prime.erase(remove(ALL(prime), 0), prime.end());
  int n;
  while(cin>>n,n) {
    if (binary_search(ALL(prime), n))
      cout << 0 << endl;
    else
      cout << *upper_bound(ALL(prime), n) - *(upper_bound(ALL(prime), n)-1) << endl;
  }
}