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 <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;
/* c++11
#include <tuple>
using std::tuple; 
using std::make_tuple;
using std::get; // tuple std::get<NUM>(t); (0 <= NUM <= 2)
 */

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int> > PIII;

VI Sieve_of_Eratosthenes(int number) {
    VI prime(number + 1, 0);    
    prime[1] = 1;
    for (int i = 2; i <= number; i++) {
        if (prime[i] == 0) {
            for (int j = i * 2; j <= number; j += i) {
                prime[j]  = 1; 
            }
        }
    }
    return prime;
}

int main(void) {
    int number = 1299709;
    VI prime = Sieve_of_Eratosthenes(number);
    int p;
    while (1) {
        cin >> p;
        if (p == 0) {
            break;
        }
        int left;
        int right;
        if (prime[p] == 0) {
            cout << 0 << endl;
            continue;
        }
        bool left_det = false;
        bool right_det = false;
        int width = 0;
        while (!(left_det && right_det)) {
            width++; 
            if (prime[p - width] == 0 && !left_det) {
                left = p - width;
                left_det = true;
            }
            if (prime[p + width] == 0 && !right_det) {
                right = p + width;
                right_det = true;
            }
        }
        cout << right - left << endl;
    }
    return 0;
}

C++: #include <iostream>
using namespace std;
//#define max 1000000;
typedef long long int l;
l prime[1000000];

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

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

    while( 1 ){
        cin >> k;
        if( k == 0 ) return 0;
        if( prime[k] == 1 ){
            cout << 0 << endl;
            continue;
        }
        l s, e;
        for( l i = k - 1; ; i-- ){
            if( prime[i] == 1 ){
                s = i;
                break;
            }
        }
        for( l i = k + 1; ; i++ ){
            if( prime[i] == 1 ){
                e = i;
                break;
            }
        }
        cout << e - s << 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] << endl;
                break;
            }
        }
    }
}

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

vector<int> primes;

void list_prime(int N){
	bool flag[N + 1] = {true,true};
	for(int i = 4;i <= N;i += 2) flag[i] = true;
	for(int i = 3;i * i <= N;i += 2){
		if(flag[i]) continue;
		for(int j = i * 2;j <= N;j += i) flag[j] = true;
	}
	for(int i = 0;i <= N;i++) if(!flag[i]) primes.push_back(i);
}

int main(){
	list_prime(1300000);
	int n;
	while(cin >> n,n){
		int low = lower_bound(primes.begin(),primes.end(),n) - primes.begin();
		if(primes[low] == n) cout << 0 << endl;
		else{
			cout << primes[low] - primes[low - 1] << endl;
		}
	}
	return 0;
}

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


int main(void){
    vector<bool> array(1299800, true);

    for(int i = 2; i < 1299750; i++){
        if(array[i] == true){
            for(int j = 2*i; j < 1299750; j += i){
                array[j] = false;
            }
        }
    }

    while(1){
        int n; cin >> n;
        if(n == 0) break;
        int up = n, down = n;
        
        while(1){
            if(array[up] == true) break;
            up++;
        }

        while(1){
            if(array[down] == true) break;
            down--;
        }

        cout << up - down << endl;
    }

    return 0;
}