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;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<n;i++)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

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

void solve(int n){
    int l,u;
    l = n-1;
    u = n+1;

    if(!isp(n)){
        cout << 0 << endl;
        return;
    }

    while(isp(u)) u++;
    while(isp(l)) l--;
    cout << u - l << endl;
}

int main(){
    int n;
    while(1){
        cin >> n;
        if(!n) break;
        solve(n);
    }
}

C++: #include<iostream>
 
using namespace std;
 
const int MAX= 100001;
bool prime[MAX];
void make_prime(){
  for(int i = 0 ; i < MAX ; i ++){
    prime[i] = true;
  }
     
  prime[0] = false;
  prime[1] = false;
     
  for(int i = 2 ; i <= MAX ; i ++){
    if(prime[i]){
      for(int j = i+i ; j <= MAX ; j += i){
	prime[j] = false;
      }
    }
  }
}
 
int main(void){
  make_prime();
  int n;
  while(cin>>n,n){
    if(prime[n]){
      cout<<0<<endl;
      continue;
    }
    int k,j;
    for(k = n; !prime[k]; k--);
    for(j = n; !prime[j]; j++);
    cout<<j-k<<endl;
    
  }
}

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

bool t[MAX_N + 1];

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 = 0; i < MAX_N; ++i) {
    if (isPrime(i)) t[i] = 1;
  }
}

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

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

C++: #include<iostream>
using namespace std;
int primes[400003];
bool is(int n){
	if(n==2)return true;
	for(int i=3;i*i<=n;i++)if(n%i==0)return false;
	return true;
}
int main(){
	int cnt=0,ii=2;
	while(true){
	if(cnt==400003)break;
	if(is(ii)){
	primes[cnt]=ii;
	cnt++;
	}
	ii++;
	}
	int n;
	while(cin>>n){
	int a=0,b=0;
	bool f=0,f2=0;
	for(int i=0;i<400003;i++){
	if(primes[i]<n)a=primes[i];
	if(f==0&&primes[i]>n)b=primes[i],f=1;
	if(primes[i]==n)f2=1;
	}
	if(n<=1||f2)cout<<0<<endl;
	else cout<<b-a<<endl;
	}
	return 0;
}

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

const int MAX_N=2e6;
vector<int> primes,is_prime(MAX_N,1);

void sieve(){
    is_prime[0]=is_prime[1]=0;
    for (int i=2;i<MAX_N;++i){
        if (is_prime[i]){
            primes.emplace_back(i);
            for (int j=2*i;j<MAX_N;j+=i) is_prime[j]=0;
        }
    }
}

int k,ans;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    sieve();
    while(cin >> k,k){
        if (is_prime[k]){
            cout << 0 << '\n';
            continue;
        }
        auto itr=upper_bound(primes.begin(),primes.end(),k);
        ans=*itr-*(itr-1);
        cout << ans << '\n';
    }
}

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

const int MAX_N = 1e+7;
bool is_p[MAX_N+1] = {false,false};

void sieve(){
	for(int i = 2 ; i <= MAX_N ; i++ )
		is_p[i] = true;
	for(int i = 2 ; i*i <= MAX_N ; i++ ){
		if( is_p[i] ){
			for(int j = i+i ; j <= MAX_N ; j += i ){
				is_p[j] = false;
			}
		}
	}
}

int main(){
	sieve();
	int k;
	while( cin >> k , k ){
		int a, b;
		for(int i=k ; 2 <= i ; i-- ){
			if( is_p[i] ){
				a = i;
				break;
			}
		}
		for(int i=k ; true ; i++ ){
			if( is_p[i] ){
				b = i;
				break;
			}
		}
		cout << b - a << endl;
	}
}