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++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <utility>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#define each(e,c) for(auto&e:c)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<(DUMP(),__VA_ARGS__).str()<<" ["<<__LINE__<<"]"<<endl)
struct DUMP:ostringstream{template<class T>DUMP &operator,(const T&t){if(this->tellp())*this<<", ";*this<<t;return *this;}};
#else
#define dump(...)
#endif
template<class T> ostream& operator<<(ostream& os, vector<T> const& v){
    rep(i,v.size()) os << v[i] << (i+1==v.size()?"":" ");
    return os;
}

const int MP = 1299709+1000;

vi seive(){
    vi isp(MP);
    loop(i,2,MP) isp[i] = i;
    loop(i,2,MP) if(isp[i]) for(int j=i+i;j<MP;j+=i) isp[j] = 0;
    isp.erase(remove(all(isp), 0), isp.end());
    return isp;
}

int main(){
    auto ps = seive();
    int n;
    while(cin>>n && n){
        auto i = lower_bound(all(ps), n);
        if(*i==n){
            cout << 0 << endl;
        } else {
            auto j = i-1;
            cout << *i-*j << endl;
        }
    }
}

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

int main() {
bool a[1300000]={0};
a[1]=1;
for(int i=2;i<650001;i++){
	if(a[i]==1) continue;
	int j=2*i;
	while(j<1300000){
		a[j]=1;
		j=j+i;
	}
}
int k;
while(cin>>k && k){
int ans;
if(a[k]==0) ans=0; 
else{
	int l=k+1,m=k-1;
	while(a[l]==1) l++;
	while(a[m]==1)  m--;
	ans=l-m;
}
cout<<ans<<endl;

}



	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

vector<int> get_prime(int n) {
    if(n <= 1) return vector<int>{};
    vector<bool> is_prime(n+1, true);
    vector<int> prime;
    for (int i = 2; i <= n; ++i) {
        if(!is_prime[i]) continue;
        prime.emplace_back(i);
        for (int j = i*2; j <= n; j += i) {
            is_prime[j] = false;
        }
    }
    return prime;
}

int main() {
    const auto primes = get_prime(1300000);
    set<int> s;
    for (auto &&i : primes) s.insert(i);
    int n;
    while(cin >> n, n){
        if(s.count(n)){
            puts("0");
            continue;
        }
        auto ub = s.lower_bound(n);
        auto lb = ub;
        lb--;
        cout << n << "\n";
        printf("%d\n", *ub-*lb);
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

#define all(c) (c).begin(), (c).end()
const int N = 1299709;

template<typename T> inline T sqr(T x){return x * x;}

void sieve(vector<int>& v, int n){
    v.assign(n, 0); iota(all(v), 2);
    for(auto div=v.begin(); sqr(*div)<=v.back(); ++div){
        v.erase(remove_if(div+1, v.end(), [&](int x){return x % *div == 0;}), v.end());
    }
}

int main(){
    vector<int> primes;
    sieve(primes, N);
    for(int k; cin >> k, k;){
        auto itr = equal_range(all(primes), k);
        cout << (*itr.second == *itr.first? *itr.second - *(--itr.first): 0) << '\n';
    }
    return 0;
}

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

#define MAXN 1299710

bool p[MAXN];

int main() {

int i, j, N;

memset(p, true, sizeof(p));;
for (i=2; i<MAXN; i++) {
   if (p[i] == true) {
		for (j=2*i; j<MAXN; j+=i) {
			p[j] = false;
		}
	}
}

cin >> N;

do {

i = N; while (p[i] == false) i++;
j = N; while (p[j] == false) j--;

cout << i-j << endl;

cin >> N;

} while (N!=0);

return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define PRIME_MAX 1500000
vector<bool> prime(PRIME_MAX+1,true);
 
void init_prime(){
	prime[0] = prime[1] = false;
	for(int i=2;i*i<=PRIME_MAX;i++)
		if(prime[i]) for(int j=i*i;j<=PRIME_MAX;j+=i)prime[j] = false;
}

int main(){
	init_prime();
	
	int n;
	while(cin >> n ,n ){
		int h = 0,l = 0;
		for(int i=n;i<prime.size();i++)
			if(prime[i]){h = i;break;}	
		for(int i=n;i>=0;i--)
			if(prime[i]){l = i;break;}
		cout << h - l << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
typedef long long int integer;

const int NMAX = 1299800;
bool isprime[NMAX];

int main() {
  vector<integer> primes;
  set<int> prime_set;
  for (int i = 0; i < NMAX; i++) {
    isprime[i] = true;
  }
  isprime[0] = isprime[1] = false;
  for (integer n = 2; n < NMAX; n++) {
    if (!isprime[n]) continue;
    primes.push_back(n);
    prime_set.insert(n);
    integer m = 2 * n;
    while (m < NMAX) {
      isprime[m] = false;
      m += n;
    }
  }

  int n;
  while (cin >> n && n) {
    if (n <= 2 or prime_set.find(n) != prime_set.end()) {
      cout << 0 << endl;
    } else {
      auto upper = upper_bound(primes.begin(), primes.end(), n);
      auto lower = lower_bound(primes.begin(), primes.end(), n) - 1;
      cout << (*upper) - (*lower) << endl;
    }
  }
}