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 <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>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double PI = 3.14159265358979;
const double E = 2.718281828459045;
const double root2 = sqrt(2);
typedef pair<int, int> P;
typedef unsigned int ui;
int main() {
	int isprime[1300000] = {};
	for (int i = 2; i < 1300000; i++) {
		if (!isprime[i]) {
			for (int j = 2 * i; j < 1300000; j += i) {
				isprime[j] = 1;
			}
		}
	}
	int n;
	while (cin >> n) {
		if (n == 0)break;
		if (!isprime[n])cout << 0 << endl;
		else {
			int cnt = 0;
			int le = n; int ri = n;
			while (isprime[le]) {
				le--; cnt++;
			}
			while (isprime[ri]) {
				ri++; cnt++;
			}
			cout << cnt << endl;
		}
	}
	return 0;
}

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

int prime[1299710];
int max_n = 1299711;
int main(){
  prime[0] = prime[1] = 1;
  for(int i = 0; i * i <= max_n; i++){
    if(!prime[i]){
      for(int j = i; i * j < max_n; j++){
        prime[i * j] = 1;
      }
    }
  }
  int n;
  while(cin >> n, n){
    int left, right;
    left = right = n;
    while(prime[left] && left >= 2) left--;
    while(prime[right]) right++;
    cout << right - left << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> pii;

int main()
{
  int k,n;
  int i,j;
  bool prime[2000000]={};

  for(i=2;i<2000000;i++)
    if(prime[i]==false)
      for(j=2;i*j<2000000;j++)
	prime[i*j]=true;
  while(1){
    cin>>k;
    if(k==0)break;
    if(prime[k]==false){
      cout<<"0"<<endl;
      continue;
    }
    int ans;
    for(i=k+1;i<2000000;i++)
      if(prime[i]==false){
	ans=i;
	break;
      }
    for(i=k-1;i>1;i--)
      if(prime[i]==false){
	ans-=i;
	break;
      }
    cout<<ans<<endl;
  }
}

C++: #include<iostream>
#include<cmath>
using namespace std;
int X, Y, A, i, j;
int main() {
	while (true) {
		cin >> A; if (A == 0) { break; }
		for (i = A; i >= 2; i--) { for (j = 2; j <= sqrt(i); j++) { if (i%j == 0) { goto E; } }X = i; goto F; E:; }
	F:; for (i = A; i < 99999999; i++) { for (j = 2; j <= sqrt(i); j++) { if (i%j == 0) { goto G; } }Y = i; goto H; G:; }
	H:; cout << Y - X << endl;
	}
}

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

vector<int> prime;
vector<int> ls;

void sieve(int N){
    prime.resize(N+1,1);
    prime[0] = prime[1] = 0;
    for(int i=2;i*i<=N;i++){
        for(int j=i*i;j<=N;j+=i){
            prime[j] = 0;
        }
    }
    for(int i=2;i<=N;i++){
        if(prime[i])ls.push_back(i);
    }
}

int main() {
    sieve(int(2e6));
    int n;
    while(cin >> n, n){
        int l=n, r=n;
        while(prime[l]==0)l--;
        while(prime[r]==0)r++;
        cout << r-l << endl;
    }
    return 0;
}

C++: #include <iostream>

using namespace std;
using ll = long long;

inline bool isprime(ll n)
{
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

int main()
{
    while (true) {
        ll n;
        cin >> n;
        if (n == 0) {
            break;
        }
        ll inf = n;
        for (;; inf--) {
            if (isprime(inf)) {
                break;
            }
        }
        ll sup = n;
        for (;; sup++) {
            if (isprime(sup)) {
                break;
            }
        }
        cout << sup - inf << endl;
    }

    return 0;
}