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>
using namespace std;
int primes[100003];
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==100003)break;
	if(is(ii)){
	primes[cnt]=ii;
	cnt++;
	}
	ii++;
	}
	int n;
	while(cin>>n){
	int a=0,b=0;
	bool f=0;
	for(int i=0;i<100003;i++){
	if(primes[i]<=n)a=primes[i];
	if(f==0&&primes[i]>=n)b=primes[i],f=1;
	}
	cout<<b-a<<endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>

bool prime[1300001];

int main(void){
	memset(prime,true,sizeof(prime));
	prime[0]=prime[1]=false;
	for(int i=2;i<=1300000;i++){
		if(prime[i]){
			for(int j=i*2;j<=1300000;j+=i)prime[j]=false;
		}
	}
	while(1){
		int k;
		scanf("%d",&k);
		if(k==0)break;
		if(prime[k]){
			printf("0\n");
			continue;
		}
		int l=k,r=k;
		while(!prime[l])l--;
		while(!prime[r])r++;
		printf("%d\n",r-l);
	}
	return 0;
}

C++: #include <cstdio>
#include <utility>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

#define M 1299709 

int main() 
{
	bool t[M+1]; //素数ならfalse
	t[0] = true;
	t[1] = true;
	
	for(int i=2;i*i<=M;i++)
	{
		if(!t[i])
		{
			for(int j = i*2 ; j<=M ; j += i)
			{
				t[j] = true;
			}
		}
	}
	
//	REP(i,M+1){if(!t[i]){cout << i << endl;}}	// 0,1,2,3,5,,,,
	
	ll x;
	while(cin >> x && x!=0)
	{
		ll min(0),max(0);
		if(!t[x]){cout << 0 << endl;}
		else{
			for(int j=0;;j++){if(!t[x+j+1]){max=x+j+1;break;}}
			for(int j=0;;j--){if(!t[x+j-1]){min=x+j-1;break;}}
			cout << max - min << endl;
		}
	}
	return 0 ;
	
}

C++: #include <iostream>
#define N 1300000
using namespace std;
int start(int);
int goal(int);
int sosu[N];
int main(){
  int i,j,n,s,g;
  for(i=0;i<N;i++) sosu[i]=1;
  sosu[0]=0;
  sosu[1]=0;
  for(i=0;i<N/2;i++){
    if(sosu[i]==1){
      for(j=i*2;j<N;j+=i){
	sosu[j]=0;
      }
    }
  }
  while(1){
    cin >> n;
    if(n==0) break;
    if(sosu[n]!=1){
      s=start(n);
      g=goal(n);
      cout << g-s << endl;
    }
    else cout << 0 << endl;
  }
  return 0;
}
int start(int n){
  int i,result;
  if(n%2==0) n--;
  else n-=2;
  for(i=n;i>=2;i-=2){
    if(sosu[i]==1){
      result=i;
      break;
    }
  }
  return result;
}
int goal(int n){
  int i,result;
  if(n%2==0) n++;
  else n+=2;
  for(i=n;i<N;i+=2){
    if(sosu[i]==1){
      result=i;
      break;
    }
  }
  return result;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int n,a[100001]={1,2,0,0},c,i,j;
	for(i=4;i<100000;i++){
		for(j=2,c=0;j*j<=i;j++)if(i%j==0)c++;
		if(c)a[i]+=a[i-1]+1;
	}
	for(i=99999;i>=0;i--){
		if(a[i]){for(j=0,c=a[i];j<c;j++)a[i--]=c+1;i++;}
	}
	while(cin>>n,n)cout<<a[n]<<endl;
}

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

#define loop(n, i) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;


int main()
{
    int max = 1299710;
    vector<int> mat(max, 1);
    vector<int> primes;
    for (int i=2; i<sqrt(max); i++) {
        if (mat[i]) {
            for (int j=i*2; j < max; j+=i) mat[j] = 0;
        }
    }
    for (int i=2; i<max; i++) {
        if (mat[i]) primes.push_back(i);
    }

    int k;
    while (cin >> k, k) {
        loop(primes.size(),i) {
            if (k == primes[i]) {
                cout << 0 << endl;
                break;
            } else if (k < primes[i]) {
                cout << primes[i]-primes[i-1];
                break;
            }
        }
    }
}

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

int isPrime(int x){
  int i;
  if(x < 2)return 0;
  else if(x==2)return 1;
  if(x%2 == 0)return 0;
  for(i=3 ; i*i <= x ; i+=2){
    if(x%i == 0)return 0;
  }
  return 1;
}

int main(){
  
  int k;
  int i,j;

  while(1){
    cin >>k;
    if(k==0)break;
    
    if(isPrime(k)){
      cout <<"0"<<endl;
    }else{
      for(i=k+1 ; isPrime(i) != 1 ; i++);
      for(j=k-1 ; isPrime(j) != 1 ; j--);
      cout <<i-j<<endl;
    }
  }

  return 0;
}

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

bool check(int x){
	if(x%2 == 0)return true;
	if(x%3 == 0)return true;
	if(x%5 == 0)return true;
	for(int i = 6;i <= x+1;i+=6){
	if(x%(i - 1) == 0)return false;
	if(x%(i + 1) == 0)return false;
	}
	return true;
}

int main(){
	int n,a,b;
	while(1){
		cin >> n;
		if(n == 0)break;
		if(!check(n) || n == 1 || n == 2 || n == 3 || n == 5){
			cout << 0 <<endl;
			continue;
		}
		if(n == 4 || n == 6){
			cout << 1 <<endl;
			continue;
		}
		for(int i = n;i <= 1299709;i++){
			if(!check(i)){
				a = i;
				break;
			}
		}
		for(int i = n;i >= 3;i--){
			if(!check(i)){
				b = i;
				break;
			}
		}
		cout << a - b - 1 <<endl;
	}
	return 0;
}

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){
    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;
}