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 <cmath>

using std::cin;
using std::cout;
using std::endl;

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

int main(void) {
	int N = 1300000;

	std::vector<bool> prime(N);
	for (int i = 0; i < N+2; i++) {
		prime[i] = true;
	}
	prime[0] = false;
	prime[1] = false;
	for (int i = 2; i <= sqrt(N); i++) {
		if (is_prime(i)) {
			for (int j = 2; i*j < N+2; j++) {
				prime[i*j] = false;
			}
		}
	}

	int n;
	cin >> n;

	while (n != 0) {
		int pa = 0;
		int pb = 0;
		bool afind = false;
		bool bfind = false;
		for (int i = n; !(afind) && i < N+2; i++) {
			if (prime[i]) {
				pa = i;
				afind = true;
			}
		}
		for (int i = n; !(bfind) && i > 0; i--) {
			if (prime[i]) {
				pb = i;
				bfind = true;
			}
		}
		cout << (pa - pb) << endl;
		cin >> n;
	}

	return 0;
}

C++: #include <cstdio>
#include <cassert>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <array>

using namespace std;

#define all(c) begin(c), end(c)
#define dump(c) cerr << "> " << #c << " = " << (c) << endl
#define REP(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1001001001;
const double EPS = 1e-10;


template<typename T1,typename T2>
ostream& operator<<(ostream& os, const pair<T1,T2>& p){
  return os << '(' << p.first <<',' << p.second << ')';
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& a){
  os << '[';
  rep(i,a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

vector<int> get_primes(int N) {
  vector<int> table(N, 1);
  table[0] = table[1] = 0;
  for(int i = 2; i * i < N; i++) if(table[i])
      for(int j = i * i; j < N; j += i) table[j] = 0;

  vector<int> ret;
  rep(i, N) if(table[i]) ret.push_back(i);

  return ret;
}

int main() {
  auto primes = get_primes(2000000);

  for(int N; cin >> N, N != 0; ) {
    auto low = upper_bound(all(primes), N) - 1;
    if (*low == N) cout << 0 << endl;
    else cout << *(low + 1) - *low << endl;
  }
  
  
  return 0;
}

C++: #include<iostream>
#include<vector>
#include <algorithm>
const int MAX = 1299709;
using namespace std;

bool isPrime[MAX+5];
int seqPrime[MAX+5];
int ITR = 0;

void  eratos() {
	// 初期化
	for(int i=0;i<=MAX;i++) {
		isPrime[i] = true;
	}
	for(int i=2;i<=MAX;i++) {
		if(isPrime[i]) {
			seqPrime[ITR++] = i;
			for(int j=2*i;j<=MAX;j+=i) {
				isPrime[j] = false;
			}
		}
	}
}

int main()
{
	eratos();
	int k;
	while(1) {
		cin >> k;
		if(k==0) break;
		auto now = upper_bound(seqPrime, seqPrime+ITR, k);
		auto before = now-1;
		if(*before == k) cout << 0 << endl;
		else cout << (*now-*before) << endl;
	}
}

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

using namespace std;

int const MAX=1299709;
int prime[MAX+1]={0};//0::prime 1::NOTprime;

void hurui(){
  prime[1]=1;
  for(int i=1;i*i<=MAX;i++){
    for(int j=2;j<=MAX/i;j++){
      if(prime[i]==0)prime[i*j]=1;
    }
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
    
  hurui();
  int n;
  //for(int i=1;i<=30;i++) cout << prime[i] << endl;
  while(cin >> n,n){
    int res=0;
    if(prime[n]==0){
      cout << res << endl;
    }
    else{
      int mae=n,ato=n+1;
      while(prime[mae]==1){
        res++;
        mae--;
      }
      while(prime[ato]==1){
        res++;
        ato++;
      }
      cout << res+1 << endl;
    }
  }
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<cstdlib>
#include<sstream>
#include<queue>
#include<deque>
#include<cmath>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<iomanip>
#define INF 2147483647
#define lli long long int
#define MOD 1000000007
#define pb push_back
#define max_prime 1299709
using namespace std;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;

bool isPrime(int num){
	if (num == 1) return false;	
	if (num == 2) return true;	
	for (int i = 2; i <= sqrt(num)+1 ; i++){
		if (num % i == 0){
			return false;
		}
	}
	return true;
}

void eratosthenes(vi &v){
	v[0] = 1; v[1] = 1;
	for (int i = 2; i <= max_prime; i++){
		if (v[i] == 0){
			for (int j = 2; j <= max_prime/i; j++){
				v[i*j] = 1;
			}
		}
	}
}
int main() {
	vi primev(max_prime,0);
	eratosthenes(primev);
	while(1){
		int n; cin >> n;
		if (n == 0) break;

		if (isPrime(n)){
			cout << 0 << endl;
		}else{
			int lp;
			int sp;
			for (int i = 0; i < primev.size(); i++){
				if (primev[i] == 0 and n > i){
					sp = i;
				}else if(primev[i] == 0 and n < i){
					lp = i;
					break;
				}
				
			}
			cout << lp - sp << endl;
		}
	}
	
	return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1276

#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int num){
  if(num<=3) return true;

  for(int i=2;i<=(int)sqrt(num);i++){
    if(num%i==0) return false;
  }

  return true;
}

int main(){
  int k;
  while(cin>>k&&k){
    if(isPrime(k)){
      cout << 0 << endl;
      continue;
    }

    int min_prime, max_prime;
    for(int i=k-1;i>=0;i--){
      if(isPrime(i)){
        min_prime = i;
        break;
      }
    }

    for(int i=k+1;i<=1299709;i++){
      if(isPrime(i)){
        max_prime = i;
        break;
      }
    }

    cout << max_prime - min_prime << endl;
  }

  return 0;
}