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>
using namespace std;

const int MAX_N = 2 * 1e6 + 2;

bool is_prime[MAX_N];
vector<int> primes;

void hurui() {
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i < MAX_N; i++) {
        is_prime[i] = true;
    }
    for (int i = 2; i < MAX_N; i++) {
        if (!is_prime[i]) continue;
        int now = i * 2;
        while (now < MAX_N) {
            is_prime[now] = false;
            now += i;
        }
    }
}

void solve(int k) {
    if (is_prime[k]) {
        cout << 0 << endl;
        return;
    }
    for (int i = 0; i < primes.size() - 1; i++) {
        if (primes[i] < k && primes[i + 1] > k) {
            cout << primes[i + 1] - primes[i] << endl;
            return;
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int K;
    hurui();
    for (int i = 2; i < MAX_N; i++) {
        if (is_prime[i]) {
            primes.push_back(i);
        }
    }
    while (true) {
        cin >> K;
        if (K == 0) break;
        solve(K);
    }
    return 0;
}

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

int pmem[1300000];
vector<int> prime;
void init()
{
  for(int i = 2; i < 1300000; i++)
  {
    if(pmem[i])
      continue;
    pmem[i] = 1;
    prime.push_back(i);
    for(int j = i + i; j < 1300000; j += i)
      pmem[j] = -1;
  }
}

int main()
{
loop:
  init();
  int n;
  cin >> n;
  if(!n)
    return 0;
  for(int i = 0; i < prime.size(); i++)
  {
    if(prime[i] == n)
    {
      cout << 0 << endl;
      goto loop;
    }
    if(prime[i] > n)
    {
      cout << prime[i] - prime[i - 1] << endl;
      goto loop;
    }
  }
}

C++: #include<iostream>
#include <algorithm>
using namespace std;
#define MAX_LIST 10000000



int main(){
	bool *prime=(bool *)malloc(sizeof(bool)*MAX_LIST);
	//bool prime[MAX_LIST];
	for(int i=0;i<MAX_LIST;i++){
		prime[i]=1;
	}
	prime[0]=prime[1]=0;
	for(int i=0;i*i<MAX_LIST;i++){
		if(prime[i]==1){ 
			for(int j=i*2;j<MAX_LIST;j=j+i){
				prime[j]=0;
			}
		}
	}
	int in;
	while(cin>>in,in){
		int ans=0;
		int i=0;
		while(prime[in+i]==0){
			ans++;
			i++;
		}
		i=0;
		while(prime[in-i]==0){
			ans++;
			i++;
		}
		cout<<ans<<endl;
	}
	
	return 0;
}

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

//define
#define ALL(a) a.begin(),a.end()
#define REP(i,n) for(int i=0;i<n;i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define debug(x) if(1) cout<<#x<<":"<<x<<endl;
#define DEBUG(x) if(1) cout<<#x<<":"<<x<<endl;
#define ll long long

//constant
const int MOD=1000000007;
const int INF=INT_MAX/3-1;
const double EPS=1e-9;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};

//typedef
typedef vector<int> vi;
typedef vector<vector<int>> vvi;


bool isnprime[2000000];

int main(){
    isnprime[0]=1;
    isnprime[1]=1;
    vi primes;
    for(int i=2;i<1000000;i++)
        if(!isnprime[i]){
            primes.push_back(i);
            for(int j=i*2;j<1000000;j+=i)
                isnprime[j]=1;
        }
    int k;
    while(cin>>k,k){
        auto it=lower_bound(ALL(primes),k);
        if(*it==k)
            cout<<0<<endl;
        else
            cout<<*it-*(it-1)<<endl;





    }
    return 0;
    }

C++: #include <cmath>
#include <cstdint>
#include <iostream>

using namespace std;

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

uint32_t above_prime(uint32_t n) {
  for (uint32_t i = n;; i++) {
    if (is_prime(i)) {
      return i;
    }
  }
}

uint32_t below_prime(uint32_t n) {
  for (uint32_t i = n;; i--) {
    if (is_prime(i) || i == 1) {
      return i;
    }
  }
}

int32_t main() {
  while (true) {
    uint32_t n;
    cin >> n;
    if (n == 0) break;

    uint32_t above = above_prime(n);
    uint32_t below = below_prime(n);

    cout << above - below << endl;
  }

  return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#include <cstring>
#include <cstdio>
#include <stack>
#define INF 1000000000
#define rep(i,n) for(int i =0;i<n;i++)
using namespace std;
typedef long long int ll;
vector<ll> p(1000001);

void era(){
    for (int i=2; i<1000001; i++) {
        if(p[i]==0)continue;
        int j=i;
        while (j<1000001) {
            j+=i;
            if(j>=1000001)break;
            p[j]=0;
        }
    }
}

int main(){
    rep(i, 1000001) p[i]=i;
    era();
    for(;;){
        int n;
        cin >>n;
        if(!n) break;
        int ans = 0;
        int i=n,j=n;
        while (p[i]==0) {
            i++;
        }
        while (p[j]==0) {
            j--;
        }
        ans = p[i]-p[j];
        cout << ans <<endl;
    }
}