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<stdio.h>
#include<iostream>
using namespace std;
bool n[1299710]={};
int main(){
for(int i=3;i<1299710;i+=2){n[i]=true;
	for(int j=2;j*j<=i;j++)
		if(i%j==0){n[i]=false;break;}}
n[2]=true;
	int N;
while(1){
	cin>>N;if(N==0)break;
	int x=0;int y=0;int c=0;
	if(n[N]==true||N==1)c=0;
	else{
	for(int i=N;i<1299710;i++)
		if(n[i]==true){x=i;break;}
	for(int i=N;i>1;i--)
		if(n[i]==true){y=i;break;}
	c=x-y;
	
	}
	cout<<c<<endl;
}
return 0;
}

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

#define MAX 10000001

bool p[MAX];

int main(){
  memset(p, 1, sizeof(p));
  p[0] = p[1] = 0;
  for(int i = 0 ; i < MAX ; i++){
    if(p[i]){
      for(int j = 2 * i ; j < MAX ; j += i){
	p[j] = 0;
      }
    }
  }

  int n;
  while(cin >> n, n){
    int next, prev;
    if(p[n] == 1) cout << 0 << endl;

    else{
      for(int i = n ; ; i++){
	if(p[i] == 1){
	  next = i;
	  break;
	}
      }
      for(int i = n ; ; i--){
	if(p[i] == 1){
	  prev = i;
	  break;
	}
      }
      cout << next - prev << endl;
    }
  }
  return 0;
}

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

int main(){
  int n;
  int p[N];
  
  for(int i=0;i<N;i+=2)p[i] = 0;
  for(int i=1;i<N;i+=2)p[i] = 1;
  p[1] = 0;
  p[2] = 1;

  int c = 3;
  while(c*c<N){
    for(int i=3*c;i<N;i+=2*c)p[i] = 0;
    while(!p[++c]);
  }

  while(1){
    cin >> n;
    if(!n)break;

    if(p[n])cout << 0 << endl;
    else{
      int l,r;
      l = r = n;
      while(!p[--l]);
      while(!p[++r]);
      cout << r - l << endl;
    }
  }
}

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

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

int main() {
	int k;
	while (cin >> k, k != 0) {
		if (isPrime(k)) puts("0");
		else {
			int ans = 1, x = k-1;
			while (!isPrime(x)) {
				++ans;
				--x;
			}
			
			x = k+1;
			while (!isPrime(x)) {
				++ans;
				++x;
			}
			
			cout << ans + 1 << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;
//#define MAX 1300000
const int MAX = 1300000;

vector<int> prime(MAX);

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

int main(){
    mk_prime();
    int k;

    while( cin >> k, k ){
        if( prime[k] == 1 ){
            cout << 0 << endl;
            continue;
        }
        int s, e;
        for( int i = k - 1; ; i-- ){
            if( prime[i] == 1 ){
                s = i;
                break;
            }
        }
        for( int i = k + 1; ; i++ ){
            if( prime[i] == 1 ){
                e = i;
                break;
            }
        }
        //int ans = e - s;
        cout << e - s << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int prime(int x) {
	if (x < 2)return 0;
	else if (x == 2) return 1;
	if (x % 2 == 0) return 0;
	for (int i = 3; i*i <= x; i += 2) {
		if (x%i == 0) return 0;
	}
	return 1;
}
int main(){
	int n;
	while (cin >> n, n) {
		if (prime(n))cout << 0 << endl;
		else { 
			int x = n, y = n;
			while (!prime(x)) {
				x--;
			}
			while (!prime(y)) {
				y++;
			}
			cout << y - x << endl;
		}
	}
}

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

int main ()
{
    vector<bool> num(1000001, true);
    num[0] = num[1] = false;
    vector<int> prim;
    for (int i = 2; i <= 1000000; i++) {
        for (int j = 2; num[i] && j * i <= 1000000; j++) {
            num[i * j] = false;
        }
        if (num[i]) prim.push_back(i);
    }
    int n;
    while (cin >> n, n) {
        auto i = lower_bound(prim.begin(), prim.end(), n) - prim.begin();

        int res = 0;
        if (n < prim[i]) res = prim[i] - prim[i-1];
        if (n > prim[i]) res = prim[i+1] - prim[i];

        cout << res << endl;
    }

    return 0;
}

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

  


vector<int> linear_sieve(const int UPBD) {
  vector<int> primes, last_prime(UPBD, 0);
  for (int p = 2; p < UPBD; ++p) {
    if (not last_prime[p]) primes.push_back(p), last_prime[p] = p;
    for (int j = 0; primes[j] * p < UPBD; ++j) {
      last_prime[primes[j] * p] = primes[j];
      if (p % primes[j] == 0) break;
    }
  }
  return last_prime;
}


 

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
  int n; 
  vector<int> sieve = linear_sieve(1.3e6);
  vector<int> lp(sieve.size()), np(sieve.size());
  for (int i = 2, p = 2; i < sieve.size(); ++i) {
    if (sieve[i] == i) p = i;
    lp[i] = p;
  }
  for (int i = sieve.size() - 1, p = -1; i > 0; --i) {
    if (sieve[i] == i) p = i;
    np[i] = p;
  }
  while (cin >> n and n) {
    cout << np[n] - lp[n] << '\n';
  }
}