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 <bits/stdc++.h>

std::string next(std::string n) {
  std::string str1;
  std::string str2;
  long max = -1;
  for(int i = 1; i < n.size(); ++i) {
    str1 = str2 = "";
    for(int j = 0; j < i; ++j) {
      str1 += n[j];
    }
    for(int j = i; j < n.size(); ++j) {
      str2 += n[j];
    }
    max = std::max(max, stol(str1) * stol(str2));
  }
  return std::to_string(max);
}                   

int main() {
  int Q;
  std::cin >> Q;
  for(int i = 0; i < Q; ++i) {
    std::string N;
    std::cin >> N;
    int count = 0;
    for(;;) {
      if( N.size() == 1 ) {
        std::cout << count << std::endl;
        break;
      }
      std::string t = next(N);
      N = t;
      count += 1;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
const int pArray[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193};
set<int> primes;
bool isPrime(int i){
  if(i<=1)return false;
  for(int p:pArray){
    if(p>i)break;
    if(i%p==0)return false;
  }
  return true;
}

int main(){ _;
  for(int p:pArray)
    primes.insert(p);
  for(int i=1201;i<1300000;i+=2)
    if(isPrime(i))
      primes.insert(i);
  int n;
  while(cin>>n,n!=0){
    auto u = primes.upper_bound(n); u--;
    auto l = primes.lower_bound(n);
    cout<<(*l-*u)<<endl;
  }
}

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

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

//?????????: n??\???????´???°
vector<int> getPrimes(int n) {
	vector<char> is_prime(n + 1, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i*i <= n; i++)
		if (is_prime[i]) {
			int j = i + i;
			while (j <= n) {
				is_prime[j] = false;
				j += i;
			}
		}
	vector<int> primes;
	rep(i, 0, n + 1)
		if (is_prime[i])
			primes.emplace_back(i);
	return primes;
}

signed main() {
	vector<int> primes = getPrimes(1299709);
	for (int n; cin >> n&&n;) {
		int l = lower_bound(all(primes), n) - primes.begin();
		int u = upper_bound(all(primes), n) - primes.begin();
		if (primes[l] != primes[u])cout << 0 << endl;
		else cout << primes[l] - primes[l - 1] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;

const ll MAX = 1299709;

int main(void) {
  vector<bool> isp(MAX + 1, true);
  isp[0] = isp[1] = false;
  REP(i, 0, MAX + 1) if(isp[i]) for(ll j = i + i; j <= MAX; j += i) isp[j] = false;
  vector<ll> prime;
  REP(i, 0, MAX + 1) if(isp[i]) prime.push_back(i);

  ll N;
  while(cin >> N, N) {
    if(binary_search(prime.begin(), prime.end(), N)) {
      cout << 0 << endl;
    } else {
      auto t = lower_bound(prime.begin(), prime.end(), N);
      cout << (*t - *(t - 1)) << endl;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

const int MAX_N = 1300000;

int prime[MAX_N];
bool is_prime[MAX_N + 1];

void eratos(int n) {
    int p = 0;
    for (int i = 0; i <= n; ++i) is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i <= n; ++i) {
        if (is_prime[i]) {
            prime[p++] = i;
            for (int j = 2*i; j <= n; j += i) is_prime[j] = false;
        }
    }
}

int main() {
    eratos(MAX_N);

    int n;
    while (cin >> n, n) {

        int r = n, l = n;
        for (int i=n; !is_prime[i]; --i) l=i-1;
        for (int i=n; !is_prime[i]; ++i) r=i+1;

        cout << r-l << endl;

    }
}

C++: #include<iostream>
#include<vector>
#include<string>
#define SIZE 1299710
using namespace std;

int main() {
	bool *hantei;
	hantei = new bool[SIZE] {};
	hantei[0] = hantei[1] = true;
	for (int a = 2; a < SIZE; a++) {
		if (!hantei[a]) {
			for (int b = a * 2; b < SIZE; b += a)hantei[b] = true;
		}
	}
	int c;
	while (cin >> c, c) {
		if (c == 1 || !hantei[c]) { cout << 0 << endl; continue; }
		int d = 1;
		for (int e = c + 1;; e++) {
			if (!hantei[e])break;
			d++;
		}
		for (int f = c - 1;; f--) {
			if (!hantei[f])break;
			d++;
		}
		cout << d+1<< endl;
	}
}