Problem:
Problem B:
Prime Gap
The sequence of
n
- 1 consecutive composite numbers (positive integers that are not prime and
not equal to 1) lying between two successive prime numbers
p
and
p
+
n
is called a
prime
gap of length
n
. For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6.
Your mission is to write a program to calculate, for a given positive integer
k
, the length of the prime gap that contains
k
. For convenience, the length is considered 0 in case no prime gap
contains
k
.
Input
The input is a sequence of lines each of which contains a single positive integer. Each positive
integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709.
The end of the input is indicated by a line containing a single zero.
Output
The output should be composed of lines each of which contains a single non-negative integer. It
is the length of the prime gap that contains the corresponding positive integer in the input if it
is a composite number, or 0 otherwise. No other characters should occur in the output.
Sample Input
10
11
27
2
492170
0
Output for the Sample Input
4
0
6
0
114

Submitted solutions:

code_snippet: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define endl '\n'
#define print(v) for(auto& x:v)cout<<x<<' ';cout<<endl

const int N = 2e6;
int minp[N];
bitset<N> vis;
vector<int> ps;

void euler(int n)
{
	for (int i = 2; i <= n; i++) {
		if (!vis[i]) ps.push_back(i), minp[i] = i;
		for (int j = 0; j < ps.size() && ps[j] * i <= n; j++) {
			minp[i * ps[j]] = ps[j];
			vis[i * ps[j]] = true;
			if (i % ps[j] == 0) break;
		}
	}
}

void solve()
{
	euler(N - 1);
	int n;
	while (cin >> n && n) {
		if (!vis[n]) {
			cout << 0 << endl;
			continue;
		}

		int left = n, right = n;

		while (left >= 1 && vis[left]) left--;
		while (right <= N - 1 && vis[right]) right++;

		cout << right - left << endl;
	}
}

signed main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	//int q; cin >> q;
	int q = 1;
	while (q--) solve();
}
accuracy: 1
time_ms: 10
memory_kb: 20828
final_rank: 15

code_snippet: //https://vjudge.net/problem/Aizu-1276

#include<bits/stdc++.h>
using namespace std;
#define LENGTH 1299715
long long is_prime[LENGTH];
long long prime[LENGTH];
int main() {
    int a, b;
    memset(is_prime,0,sizeof(int)*LENGTH);
    memset(prime,0,sizeof(int)*LENGTH);
    for (int i=2, t=0, p=0; i<=LENGTH;i++) {
        if (is_prime[i]==0)
        {
            prime[p++]=i;
        }
        for (int j=0;j<p&&i*prime[j]<=LENGTH;j++) {
            is_prime[i*prime[j]]=1;
            if (i%prime[j]==0)
                break;
        }
    }
    int num;
    while(cin>>num)
    {
        if(num==0) break;
        if(is_prime[num]==0){
            cout<<0<<endl;
            continue;
        }
        int l=num,r=num;
        for(;;)
        {
            l--;
            if(is_prime[l]==0) break;
        }
        for(;;)
        {
            r++;
            if(is_prime[r]==0) break;
        }
        cout<<r-l<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 21616
final_rank: 16

code_snippet: #include <vector>
#include <iostream>
using namespace std;

const int LIMIT = 1299709;

int main() {
	// step #1. sieve of eratosthenes
	vector<bool> p(LIMIT + 1, true);
	p[0] = p[1] = false;
	for (int i = 2; i <= LIMIT; i++) {
		if (p[i]) {
			for (int j = i * 2; j <= LIMIT; j += i) {
				p[j] = false;
			}
		}
	}

	// step #2. count lower/upper primes
	vector<int> low(LIMIT + 1, 0), up(LIMIT + 1, 0);
	for (int i = 2; i <= LIMIT; i++) {
		low[i] = (p[i] ? i : low[i - 1]);
	}
	for (int i = LIMIT; i >= 2; i--) {
		up[i] = (p[i] ? i : up[i + 1]);
	}

	// step #3. answer queries
	while (true) {
		int N;
		cin >> N;
		if (N == 0) {
			break;
		}
		int ans = up[N] - low[N];
		cout << ans << endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 13212
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
    int MAXS = 1299710;
    vector<bool> P(MAXS, true);
    P[0] = P[1] = false;
    rep(i,2,MAXS) {
        if (P[i]) {
            rep(j,2,MAXS) {
                if (i * j >= MAXS) break;
                P[i*j] = false;
            }
        }
    }
    vector<int> L(MAXS,0), R(MAXS,MAXS-1);
    rep(i,1,MAXS) {
        if (P[i]) L[i] = i;
        else L[i] = L[i-1];
    }
    rrep(i,0,MAXS-1) {
        if (P[i]) R[i] = i;
        else R[i] = R[i+1];
    }
    while(1) {
        int K;
        cin >> K;
        if (K == 0) return 0;
        cout << R[K] - L[K] << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 13028
final_rank: 11

code_snippet: #define _CRT_SECURE_NO_WARNINGS
#if USE_MP
#include <boost/multiprecision/cpp_int.hpp>
#endif
#include <cassert>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <array>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <functional>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <bitset>
#include <string.h>
#include <numeric>
#include <random>
#if defined _DEBUG
//#include "TestCase.h"
//#include "Util.h"
#endif

using namespace std;

using ll = long long;
using ull = unsigned long long;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;

using ai2 = array<int, 2>;
using ai3 = array<int, 3>;
using ai4 = array<int, 4>;
using al2 = array<ll, 2>;
using al3 = array<ll, 3>;
using al4 = array<ll, 4>;
using ad2 = array<double, 2>;
using ad3 = array<double, 3>;
using ad4 = array<double, 4>;

inline void Yes(bool upper = false) { cout << (upper ? "YES" : "Yes") << "\n"; }
inline void No(bool upper = false) { cout << (upper ? "NO" : "No") << "\n"; }

inline ll DivCeil(ll nume, ll deno)
{
	assert(deno != 0);
	if (deno < 0) { nume = -nume; deno = -deno; }
	if (nume < 0) return -(-nume / deno);
	else return (nume + deno - 1) / deno;
}
inline ll DivFloor(ll nume, ll deno)
{
	assert(deno != 0);
	if (deno < 0) { nume = -nume; deno = -deno; }
	if (nume < 0) return -((-nume + deno - 1) / deno);
	else return nume / deno;
}
inline ll DivRound(ll nume, ll deno)
{
	assert(deno != 0);
	if (deno < 0) { nume = -nume; deno = -deno; }
	if (nume < 0) return -((-nume + deno / 2) / deno);
	else return (nume + deno / 2) / deno;
}

template <typename T, int S>
auto MakeVecImpl(queue<int>& size, const T& ini)
{
	if constexpr (S == 1)
	{
		return vector<T>(size.front(), ini);
	}
	else
	{
		int fsize = size.front();
		size.pop();
		return vector(fsize, MakeVecImpl<T, S - 1>(size, ini));
	}
}

template <typename T, int S>
auto MakeVec(const array<int, S>& size, const T ini = T())
{
	queue<int> qsize;
	for (const auto v : size) qsize.push(v);
	return MakeVecImpl<T, S>(qsize, ini);
}
int d4[][2] =
{
	{0,-1},
	{0,1},
	{-1,0},
	{1,0},
};
int d8[][2] =
{
	{-1,-1},
	{0,-1},
	{1,-1},

	{-1,0},
	{1,0},

	{-1,1},
	{0,1},
	{1,1},
};
vector<array<int, 2>> Neighbor4(int y, int x)
{
	vector<array<int, 2>> ret;
	ret.push_back({ y, x - 1 });
	ret.push_back({ y, x + 1 });
	ret.push_back({ y - 1, x });
	ret.push_back({ y + 1, x });
	return ret;
}
vector<array<int, 2>> Neighbor6(int y, int x)
{
	vector<array<int, 2>> ret;
	const int ofs = (y & 1); // 1 - (y & 1); //

	ret.push_back({ y, x - 1 });
	ret.push_back({ y, x + 1 });

	ret.push_back({ y - 1, x - 1 + ofs });
	ret.push_back({ y - 1, x + ofs });

	ret.push_back({ y + 1, x - 1 + ofs });
	ret.push_back({ y + 1, x + ofs });

	return ret;
}
vector<array<int, 2>> Neighbor8(int y, int x)
{
	vector<array<int, 2>> ret;
	ret.push_back({ y - 1, x - 1 });
	ret.push_back({ y - 1, x });
	ret.push_back({ y - 1, x + 1 });

	ret.push_back({ y, x - 1 });
	ret.push_back({ y, x + 1 });

	ret.push_back({ y + 1, x - 1 });
	ret.push_back({ y + 1, x });
	ret.push_back({ y + 1, x + 1 });
	return ret;
}

vector<int> DecompDigit(ull val)
{
	if (val == 0)
		return { 0 };

	vector<int> ret;
	while (val)
	{
		ret.emplace_back(val % 10);
		val /= 10;
	}
	reverse(ret.begin(), ret.end());
	return ret;
}


#define PI 3.14159265358979l
long double R2D = 180 / PI;
long double D2R = PI / 180;

static const ll Mod = 1000000007;
static const ll Mod9 = 998244353;// 1000000007;
static const ll INF64 = 4500000000000000000;// 10000000000000000;
static const int INF32 = 2000000000;

random_device rd;
mt19937 mt(0);//(rd());
#if USE_MP
using mpint = boost::multiprecision::cpp_int;
#endif

const double EPS = 1e-10;

vector<bool> isP;
vector<int> GetPrime()
{
	vector<int> ret;
	int SIZE = 1e7;
	isP.resize(SIZE, true);
	isP[0] = isP[1] = false;
	for (int i = 2; i < SIZE; i++) {
		if (!isP[i]) continue;
		for (int j = i + i; j < SIZE; j += i) {
			isP[j] = false;
		}
		ret.emplace_back(i);
	}
	return ret;
}

struct Solver
{
	void Run()
	{
	}
};

int main()
{
	std::cin.tie(nullptr); //★インタラクティブ注意★
	std::ios_base::sync_with_stdio(false);
	int T = 1;
	//cin >> T;
	auto p = GetPrime();
	while (1)
	{
		int i;
		cin >> i;
		if (i == 0) break;

		if (isP[i])
			cout << "0\n";
		else
		{
			auto itr = lower_bound(p.begin(), p.end(), i);
			cout << *itr - *(itr - 1) << "\n";
		}
	}

	return 0;
}

/// 値渡しになっていないか?
/// 入力を全部読んでいるか? 途中でreturnしない
/// 32bitで収まるか? 10^5数えるとき
/// modは正しいか?
/// multisetでcountしていないか?
accuracy: 1
time_ms: 40
memory_kb: 8960
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX int(1299709)
#define ALL(v) v.begin(),v.end()

int main(void) {
	vector<int> prime;
	vector<bool> isPrime(MAX + 1, true);
	for (int i = 2; i <= MAX; ++i) {
		if (isPrime[i]) {
			prime.push_back(i);
		}
		int a = 2 * i;
		while (a <= MAX) {
			isPrime[a] = false;
			a += i;
		}
	}
	while (true) {
		int N;
		cin >> N;
		if (N == 0) break;

		int index = lower_bound(ALL(prime), N) - prime.begin();
		int ans = -1;
		if (prime[index] == N) {
			ans = 0;
		} else {
			ans = prime[index] - prime[index - 1];
		}
		cout << ans << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3664
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i,end) for(int i = 0; i <= (int)(end); i++)
#define reps(i,start,end) for(int i = start; i < (int)(end); i++)
#define repsn(i,start,end) for(int i = start; i <= (int)(end); i++)
#define ll long long
#define print(t) cout << t << endl 
#define all(a)  (a).begin(),(a).end()
// << std::fixed << std::setprecision(0)
const ll INF = 1LL << 60;
 
template<class T> void chmin(T& a, T b){
 if(a > b){
  a = b;
 }
}
 
template<class T> void chmax(T& a, T b){
 if(a < b){
  a = b;
 }
}
// for AOJ or ICPC or etc..
template<class Tp>
bool zero (const Tp &x) {
    return x == 0;
}

template<class Tp, class... Args>
bool zero (const Tp &x, const Args& ...args) {
    return zero(x) and zero(args...);
}
ll divisor(ll n){
  ll sum = 0;
  for(ll i = 1;i * i <= n;i++){
    if(n % i == 0){
      sum++;
      if(i * i != n)sum++;;
    }
  }

  return sum;
}
int main(){
  while(1){
    ll n;cin >> n;
    if(zero(n))break;
    if(divisor(n) <= 2){
      cout << 0 << endl;
      continue;
    }else{
      ll mi = n;ll ma = n;
      while(divisor(mi) != 2){
        mi--;
      }
      while(divisor(ma) != 2){
        ma++;
      }
      cout << ma - mi << endl;
    }
  }
}
accuracy: 1
time_ms: 100
memory_kb: 3344
final_rank: 6

code_snippet: #line 2 "library/KowerKoint/base.hpp"

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif

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

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for(ll i = a; i < (ll)(b); i++)
#define ALL(a) (a).begin(),(a).end()
#define END(...) { print(__VA_ARGS__); return; }

using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using ll = long long;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VS = vector<string>;
using VVS = vector<VS>;
using VVVS = vector<VVS>;
using VC = vector<char>;
using VVC = vector<VC>;
using VVVC = vector<VVC>;
using P = pair<int, int>;
using VP = vector<P>;
using VVP = vector<VP>;
using VVVP = vector<VVP>;
using LP = pair<ll, ll>;
using VLP = vector<LP>;
using VVLP = vector<VLP>;
using VVVLP = vector<VVLP>;

template <typename T>
using PQ = priority_queue<T>;
template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;

constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr int DX[] = {1, 0, -1, 0};
constexpr int DY[] = {0, 1, 0, -1};

void print() { cout << '\n'; }
template<typename T>
void print(const T &t) { cout << t << '\n'; }
template<typename Head, typename... Tail>
void print(const Head &head, const Tail &... tail) {
    cout << head << ' ';
    print(tail...);
}

#ifdef ONLINE_JUDGE
template<typename... Args>
void dbg(const Args &... args) {}
#else
void dbg() { cerr << '\n'; }
template<typename T>
void dbg(const T &t) { cerr << t << '\n'; }
template<typename Head, typename... Tail>
void dbg(const Head &head, const Tail &... tail) {
    cerr << head << ' ';
    dbg(tail...);
}
#endif

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
    os << p.first << " " << p.second;
    return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
    is >> p.first >> p.second;
    return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
    for(int i = 0; i < (int) v.size(); i++) {
        os << v[i] << (i + 1 != (int) v.size() ? " " : "");
    }
    return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
    for(T &in : v) is >> in;
    return is;
}

template<typename T>
vector<vector<T>> split(typename vector<T>::const_iterator begin, typename vector<T>::const_iterator end, T val) {
    vector<vector<T>> res;
    vector<T> cur;
    for(auto it = begin; it != end; it++) {
        if(*it == val) {
            res.push_back(cur);
            cur.clear();
        } else cur.push_back(val);
    }
    res.push_back(cur);
    return res;
}

vector<string> split(typename string::const_iterator begin, typename string::const_iterator end, char val) {
    vector<string> res;
    string cur = "";
    for(auto it = begin; it != end; it++) {
        if(*it == val) {
            res.push_back(cur);
            cur.clear();
        } else cur.push_back(val);
    }
    res.push_back(cur);
    return res;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template <typename T>
pair<VI, vector<T>> compress(const vector<T> &a) {
    int n = a.size();
    vector<T> x;
    REP(i, n) x.push_back(a[i]);
    sort(ALL(x)); x.erase(unique(ALL(x)), x.end());
    VI res(n);
    REP(i, n) res[i] = lower_bound(ALL(x), a[i]) - x.begin();
    return make_pair(res, x);
}

template <typename T>
pair<vector<T>, vector<T>> factorial(int n) {
    vector<T> res(n+1), rev(n+1);
    res[0] = 1;
    REP(i, n) res[i+1] = res[i] * (i+1);
    rev[n] = 1 / res[n];
    for(int i = n; i > 0; i--) {
        rev[i-1] = rev[i] * i;
    }
    return make_pair(res, rev);
}
#line 3 "library/KowerKoint/internal_operator.hpp"

namespace internal_operator {
    template <typename T>
    T default_add(T a, T b) { return a + b; }
    template <typename T>
    T default_sub(T a, T b) { return a - b; }
    template <typename T>
    T zero() { return T(0); }
    template <typename T>
    T default_div(T a, T b) { return a / b; }
    template <typename T>
    T default_mult(T a, T b) { return a * b; }
    template <typename T>
    T one() { return T(1); }
    template <typename T>
    T default_xor(T a, T b) { return a ^ b; }
    template <typename T>
    T default_and(T a, T b) { return a & b; }
    template <typename T>
    T default_or(T a, T b) { return a | b; }
    ll mod3() { return 998244353LL; }
    ll mod7() { return 1000000007LL; }
    ll mod9() { return 1000000009LL; }
}

#line 3 "library/KowerKoint/integer.hpp"

VL divisor(ll n) {
    assert(n > 0);
    VL fow, bck;
    for(ll i = 1; i * i <= n; i++) {
        if(n % i == 0) {
            fow.push_back(i);
            if(i * i != n) bck.push_back(n / i);
        }
    }
    reverse(ALL(bck));
    fow.insert(fow.end(), ALL(bck));
    return fow;
}

bool is_prime(ll n) {
    assert(n > 0);
    for(ll d = 2; d*d <= n; d++) {
        if(n % d == 0) return false;
    }
    return true;
}

VL least_prime_factors(ll n) {
    assert(n > 0);
    VL lpfs(n+1, -1), primes;
    FOR(d, 2, n+1) {
        if(lpfs[d] == -1) {
            lpfs[d] = d;
            primes.push_back(d);
        }
        for(ll p : primes) {
            if(p * d > n || p > lpfs[d]) break;
            lpfs[p*d] = p;
        }
    }
    return lpfs;
}

VL prime_list(ll n) {
    assert(n > 0);
    VL primes;
    vector<bool> sieved(n+1);
    FOR(d, 2, n+1) {
        if(!sieved[d]) {
            primes.push_back(d);
            for(ll i = d*d; i <= n; i += d) sieved[i] = 1;
        }
    }
    return primes;
}

map<ll, int> prime_factor(ll n) {
    assert(n > 0);
    map<ll, int> factor;
    for(ll d = 2; d*d <= n; d++) {
        while(n%d == 0) {
            n /= d;
            factor[d]++;
        }
    }
    if(n > 1) factor[n]++;
    return factor;
}

ll extgcd(ll a, ll b, ll& x, ll& y) {
    x = 1, y = 0;
    ll nx = 0, ny = 1;
    while(b) {
        ll q = a / b;
        tie(a, b) = LP(b, a % b);
        tie(x, nx) = LP(nx, x - nx*q);
        tie(y, ny) = LP(ny, y - ny*q);
    }
    return a;
}

ll inv_mod(ll n, ll m) {
    ll x, y;
    assert(extgcd(n, m, x, y) == 1);
    x %= m;
    if(x < 0) x += m;
    return x;
}

ll pow_mod(ll a, ll n, ll m) {
    if(n == 0) return 1LL;
    if(n < 0) return inv_mod(pow_mod(a, -n, m), m);
    ll res = 1;
    while(n) {
        if(n & 1) {
            res *= a;
            res %= m;
        }
        n >>= 1;
        a *= a;
        a %= m;
    }
    return res;
}

#line 5 "library/KowerKoint/modint.hpp"

template <ll (*mod)()>
struct Modint {
    ll val;
    
    Modint(): val(0) {}

    Modint(ll x): val(x) {
        val %= mod();
        if(val < 0) val += mod();
    }

    Modint& operator+=(const Modint& r) {
        val += r.val;
        if(val >= mod()) val -= mod();
        return *this;
    }
    friend Modint operator+(const Modint& l, const Modint& r) {
        return Modint(l) += r;
    }

    Modint& operator-=(const Modint& r) {
        val -= r.val;
        if(val < 0) val += mod();
        return *this;
    }
    friend Modint operator-(const Modint& l, const Modint& r) {
        return Modint(l) -= r;
    }

    Modint& operator*=(const Modint& r) {
        val *= r.val;
        val %= mod();
        return *this;
    }
    Modint operator*(const Modint& r) {
        return (Modint(*this) *= r);
    }
    friend Modint operator*(const Modint& l, const Modint& r) {
        return Modint(l) *= r;
    }

    Modint pow(ll n) const {
        return Modint(pow_mod(val, n, mod()));
    }

    Modint inv() const {
        return Modint(inv_mod(val, mod()));
    }

    Modint& operator/=(const Modint& r) {
        return (*this *= r.inv());
    }
    friend Modint operator/(const Modint& l, const Modint& r) {
        return Modint(l) /= r;
    }

    Modint& operator^=(const ll n) {
        val = pow_mod(val, n, mod());
        return *this;
    }
    Modint operator^(const ll n) {
        return this->pow(n);
    }

    Modint operator+() const { return *this; }
    Modint operator-() const { return Modint() - *this; }

    Modint& operator++() {
        val++;
        if(val == mod()) val = 0LL;
        return *this;
    }
    Modint& operator++(int) {
        Modint res(*this);
        ++*this;
        return res;
    }

    Modint& operator--() {
        if(val == 0LL) val = mod();
        val--;
        return *this;
    }
    Modint& operator--(int) {
        Modint res(*this);
        --*this;
        return res;
    }

    friend bool operator==(const Modint& l, const Modint& r) {
        return l.val == r.val;
    }
    friend bool operator!=(const Modint& l, const Modint& r) {
        return l.val != r.val;
    }

    static pair<vector<Modint>, vector<Modint>> factorial(int n) {
        vector<Modint> fact(n+1), rfact(n+1);
        fact[0] = 1;
        REP(i, n) fact[i+1] = fact[i] * (i+1);
        rfact[n] = 1 / fact[n];
        for(int i = n-1; i >= 0; i--) rfact[i] = rfact[i+1] * (i+1);
        return {fact, rfact};
    }

    friend istream& operator>>(istream& is, Modint& mi) {
        is >> mi.val;
        return is;
    }

    friend ostream& operator<<(ostream& os, const Modint& mi) {
        os << mi.val;
        return os;
    }
};

using MI3 = Modint<internal_operator::mod3>;
using V3 = vector<MI3>;
using VV3 = vector<V3>;
using VVV3 = vector<VV3>;
using MI7 = Modint<internal_operator::mod7>;
using V7 = vector<MI7>;
using VV7 = vector<V7>;
using VVV7 = vector<VV7>;
using MI9 = Modint<internal_operator::mod9>;
using V9 = vector<MI9>;
using VV9 = vector<V9>;
using VVV9 = vector<VV9>;
#line 3 "library/KowerKoint/counting.hpp"

template <typename T>
struct Counting {
    vector<T> fact, ifact;

    Counting() {}
    Counting(ll n) {
        expand(n);
    }

    void expand(ll n) {
        ll sz = (ll)fact.size();
        if(sz > n) return;
        fact.resize(n+1);
        ifact.resize(n+1);
        fact[0] = 1;
        FOR(i, max(1LL, sz), n+1) fact[i] = fact[i-1] * i;
        ifact[n] = 1 / fact[n];
        for(ll i = n-1; i >= sz; i--) ifact[i] = ifact[i+1] * (i+1);
    }

    T permutation(ll n, ll r) {
        assert(n >= r);
        assert(r >= 0);
        expand(n);
        return fact[n] * ifact[n-r];
    }

    T combination(ll n, ll r) {
        assert(n >= r);
        assert(r >= 0);
        expand(n);
        return fact[n] * ifact[r] * ifact[n-r];
    }

    T stirling(ll n, ll k) {
        assert(n >= k);
        assert(k >= 0);
        if(n == 0) return 1;
        T res = 0;
        int sign = k%2? -1 : 1;
        expand(k);
        REP(i, k+1) {
            res += sign * ifact[i] * ifact[k-i] * T(i).pow(n);
            sign *= -1;
        }
        return res;
    }

    vector<vector<T>> stirling_table(ll n, ll k) {
        assert(n >= 0 && k >= 0);
        vector<vector<T>> res(n+1, vector<T>(k+1));
        res[0][0] = 1;
        FOR(i, 1, n+1) FOR(j, 1, k+1) {
            res[i][j] = res[i-1][j-1] + j * res[i-1][j];
        }
        return res;
    }

    T bell(ll n, ll k) {
        assert(n >= 0 && k >= 0);
        expand(k);
        vector<T> tmp(k+1);
        int sign = 1;
        tmp[0] = 1;
        FOR(i, 1, k+1) {
            sign *= -1;
            tmp[i] = tmp[i-1] + sign * ifact[i];
        }
        T res = 0;
        REP(i, k+1) {
            res += T(i).pow(n) * ifact[i] * tmp[k-i];
        }
        return res;
    }

    vector<vector<T>> partition_table(ll n) {
        assert(n >= 0);
        vector<vector<T>> res(n+1, vector<T>(n+1));
        REP(i, n+1) res[0][i] = 1;
        FOR(i, 1, n+1) FOR(j, 1, n+1) {
            res[i][j] = res[i][j-1] + (i<j? 0 : res[i-j][j]);
        }
        return res;
    }
};
#line 2 "Contests/Dummy/main.cpp"

/* #include "atcoder/all" */
/* using namespace atcoder; */
/* #include "KowerKoint/expansion/ac-library/all.hpp" */

void solve(){
    auto list = prime_list(1300000);
    while(1) {
        ll n; cin >> n;
        if(!n) break;
        auto it = lower_bound(ALL(list), n);
        if(*it == n) print(0);
        else print(*it - *(it-1));
    }
}

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
    // Fasterize input/output script
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(100);
    // scanf/printf user should delete this fasterize input/output script

    int t = 1;
    //cin >> t; // comment out if solving multi testcase
    for(int testCase = 1;testCase <= t;++testCase){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 1040
memory_kb: 4024
final_rank: 19

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

vector<int> prime;
void solve(int K) {
    auto it = lower_bound(prime.begin(), prime.end(), K);
    if (*it == K) {
        cout << "0\n";
        return;
    }

    auto r = upper_bound(prime.begin(), prime.end(), K);
    auto l = r - 1;
    cout << *r - *l << "\n";
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    for (int i = 2; i <= 1299709; ++i) {
        bool flag = true;
        for (int j = 2; j * j <= i; ++j) {
            if (i % j == 0) {
                flag = false;
                break;
            }
        }
        if (flag) prime.emplace_back(i);
    }

    int K;
    while (cin >> K, K)
        solve(K);

    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3508
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr);

// inititalize: O(NloglogN)
// primes: O(N)
// factorize: O(logN)
// divisors: O(約数の個数)
struct Eratosthenes {
 private:
  int _n;
  vector<bool> IsPrime;
  vector<int> MinFactor;
  vector<int> Mobius;

 public:
  Eratosthenes(int n = 1 << 22) : _n(n), IsPrime(n + 1, true), MinFactor(n + 1, -1), Mobius(n + 1, 1) {
    IsPrime[0] = IsPrime[1] = false;
    for (int p = 2; p <= n; p++) {
      if (!IsPrime[p]) continue;
      MinFactor[p] = p;
      Mobius[p] = -1;

      for (int q = 2 * p; q <= n; q += p) {
        IsPrime[q] = false;
        if (MinFactor[q] == -1) MinFactor[q] = p;
        if (q / p % p == 0) {
          Mobius[q] = 0;
        } else {
          Mobius[q] = -Mobius[q];
        }
      }
    }
  }

  bool is_prime(int x) { return IsPrime[x]; }

  vector<int> primes() {
    vector<int> res;
    for (int i = 2; i <= _n; i++) {
      if (IsPrime[i]) res.push_back(i);
    }
    return res;
  }

  vector<pair<int, int>> factorize(int n) {
    assert(2 <= n && n <= _n);

    vector<pair<int, int>> res;
    while (n > 1) {
      auto p = MinFactor[n];
      int ex = 0;
      while (MinFactor[n] == p) {
        ex++;
        n /= p;
      }
      res.emplace_back(p, ex);
    }

    return res;
  }

  vector<int> divisors(int n) {
    vector<int> res = {1};
    if (n == 1) return res;

    auto pf = factorize(n);
    for (auto [p, ex] : pf) {
      int sz = (int)res.size();
      for (int i = 0; i < sz; i++) {
        int v = 1;
        for (int j = 0; j < ex; j++) {
          v *= p;
          res.push_back(res[i] * v);
        }
      }
    }

    return res;
  }

  vector<int> get_mobius() { return Mobius; }
};

Eratosthenes E;

void solve(int &a) {
  auto get_prev_p = [&](int x) {
    for (int i = x - 1; i >= 2; i--) {
      if (E.is_prime(i)) return i;
    }
    return -1;
  };
  auto get_nxt_p = [&](int x) {
    for (int i = x + 1; i <= 1299709; i++) {
      if (E.is_prime(i)) return i;
    }
    return -1;
  };

  if (E.is_prime(a)) {
    cout << 0 << endl;
  } else {
    int pre = get_prev_p(a);
    int nxt = get_nxt_p(a);
    assert(pre != -1 && nxt != -1);
    cout << nxt - pre << endl;
  }
}

int main() {
  bokusunny;
  int a;
  while (cin >> a) {
    if (a == 0) break;
    solve(a);
  }

  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 36200
final_rank: 20

code_snippet: #include <stdio.h>

int prime[103000];
int pp = 0;

int main(void) {
    int n, t;
    for (int i = 2; i <= 1299709; i++) {
        t = 1;
        for (int x = 2; x * x <= i; x++) {
            if (i % x == 0) {
                t = 0;
                break;
            }
        }
        if (t) prime[pp++] = i;
    }
    for (int tt = 0;; tt++) {
        scanf("%d", &n);
        if (n == 0) break;
        for (int i = 0; i < pp; i++) {
            if (prime[i] == n) {
                printf("0\n");
                break;
            }
            else if (prime[i] > n) {
                printf("%d\n", prime[i] - prime[i - 1]);
                break;
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 2960
final_rank: 10

code_snippet: #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define pp pair<ll,ll>
#define ll long long
#define ld long double
#define mk make_pair
#define INF32 2147483647 
#define INF64 9223372036854775807
#define rep(i, n) for(int i = 0; i < n; i++)
const ll MOD = 1000000007;//10^9+7
const ll mod = 998244353;

vector<int> Eratosthenes( const int N )
{
    std::vector<bool> is_prime( N + 1 );
    for( int i = 0; i <= N; i++ )
    {
        is_prime[ i ] = true;
    }
    std::vector<int> P;
    for( int i = 2; i <= N; i++ )
    {
        if( is_prime[ i ] )
        {
            for( int j = 2 * i; j <= N; j += i )
            {
                is_prime[ j ] = false;
            }
            P.push_back( i );
        }
    }
    return P;
}

int main() {
    vector<int> P = Eratosthenes(1599709);
    vector<int> Primelist; Primelist.push_back(1);
    for(int i=0; i<P.size(); i++){
        Primelist.push_back(P[i]);
    }
    vector<int> ans;
    while(1){
        int N; cin >> N;
        if(N==0) break;
        auto itr = lower_bound(Primelist.begin(),Primelist.end(),N);
        int index = distance(P.begin(),itr);
        if(*itr == N){
            ans.push_back(0);
        }else{
            int aP = *prev(itr);
            ans.push_back(-(aP - *itr));
        }
    }

    for(int a: ans){
        cout << a << endl;
    }
    


}
accuracy: 1
time_ms: 10
memory_kb: 3932
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
using vl = vector<ll>;
using vc = vector<char>;
using vst = vector<string>;
using vb = vector<bool>;
using vvl = vector<vl>;
using vvc = vector<vc>;
using vvb = vector<vb>;
using mll = map<ll, ll>;
using pll = pair<ll, ll>;
using pss = pair<string, string>;
using psl = pair<string, ll>;
using vpsl = vector<psl>;
using vpll = vector<pll>;
using spss = set<pss>;
using sl = set<ll>;
using vsl = vector<sl>;
using sst = set<string>;
using ql = queue<ll>;
using vql = vector<ql>;
using dl = deque<ll>;
using pql = priority_queue<ll>;
using pqg = priority_queue<ll, vector<ll>, greater<ll>>;
#define uns unordered_set
#define unm unordered_map
#define rep(i, a, b) for(ll i = (ll)(a); i < (ll)(b); i++)
#define rrep(i, a, b) for(ll i = (ll)(a); i >= (ll)(b); i--)
#define mrep(i, a, b, c) for(ll i = (ll)(a); (bool)(b); c)
#define sep(n) for(ll i = 0; i < n; i++)
#define vin(v) rep(i, 0, v.size()) cin >> v[i]
#define vin2(v1, v2) rep(i, 0, v1.size()) cin >> v1[i] >> v2[i]
#define vvin(v) rep(i, 0, v.size()) rep(j, 0, v[i].size()) cin >> v[i][j]
#define all(x) (x).begin(),(x).end()
#define vsort(v) sort((v).begin(), (v).end())
#define vreve(v) reverse((v).begin(), (v).end())
#define rsort(v) sort((v).rbegin(), (v).rend())
#define For(x, v) for(auto x : v)
#define vout(v) For(x, v) cout << x << endl;
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define end0 return 0
#define elif else if
#define MP make_pair
#define MT make_tuple
#define PB push_back
ll vsum(vl v){ll sum = 0; For(x, v) sum += x; return sum;}
ll exp(ll x, ll y){ll a = 1; rep(i, 0, y) a *= x; return a;}
ll gcd(ll a, ll b){return a ? gcd(b%a, a) : b;}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
double factorial(ll x){ll ans=1; rep(i, 1, x+1){ans*=i;}; return ans;}
bool isinteger(double x){return floor(x)==x;}
bool iscomposite(ll x){rep(i, 2, sqrt(x)+1){if(x%i == 0) return true;}; return false;}
bool isprime(ll x){return !iscomposite(x);}
string alp = "abcdefghijklmnopqrstuvwxyz";

int main(){
    ll n; cin >> n;
    while(n != 0){
        ll i = 0, j = 0;
        while(iscomposite(n+i)) i++;
        while(iscomposite(n-j)) j++;
        cout << i + j << endl;
        cin >> n;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3352
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcount(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=1LL<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){return (a+b-1)/b;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    vector<bool> t(2000001,true);
    vector<int> p;
    t[0]=t[1]=false;
    for(int i=2;i<=2000000;i++){
        if(!t[i])continue;
        p.push_back(i);
        for(int j=2*i;j<=2000000;j+=i){
            t[j]=false;
        }
    }
    while(1){
        INT(n);
        if(n==0)break;
        if(n<=2){
            out(0);
            continue;
        }
        int j=lower_bound(all(p),n)-p.begin();
        if(n==p[j]){
            out(0);
            continue;
        } 
        out(p[j]-p[j-1]);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 4108
final_rank: 4

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

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

const int M=1334567;
bool notp[M];

int main(){
  for(int i=2;i<M;i++){
    if(notp[i])continue;
    for(int j=i*2;j<M;j+=i)notp[j]=true;
  }
  set<int> s;
  REP(i,M)if(!notp[i])s.insert(i);
  int k;
  while(cin>>k,k){
    if(s.count(k))cout<<0<<endl;
    else{
      int l=*--s.upper_bound(k),r=*s.lower_bound(k);
      cout<<r-l<<endl;
    }
  }
}
accuracy: 1
time_ms: 20
memory_kb: 9284
final_rank: 8

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

struct Eratosthenes {
    vector<bool> isprime;
    vector<int> primes;
    vector<int> spf; // smallest prime factors
    vector<int> mobius;

    Eratosthenes(int N) : isprime(N + 1, true),
                          spf(N + 1, -1),
                          mobius(N + 1, 1) {
        isprime[1] = false;
        spf[1] = 1;

        for(int p = 2; p <= N; p++){
            if(!isprime[p]) continue;
            primes.push_back(p);
            spf[p] = p;
            mobius[p] = -1;
            for(int q = p * 2; q <= N; q += p){
                isprime[q] = false;
                if(spf[q] == -1) spf[q] = p;
                mobius[q] = ((q / p) % p == 0 ? 0 : -mobius[q]);
            }
        }
    }

    vector<pair<int,int>> factorize(int n) {
        vector<pair<int,int>> res;
        while(n > 1) {
            int p = spf[n], e = 0;
            while(spf[n] == p) n /= p, e++;
            res.push_back({p, e}); // p^e
        }
        return res;
    }

    vector<int> divisors(int n) {
        vector<int> res({1});
        auto pf = factorize(n);
        for(auto p : pf) {
            int s = (int)res.size();
            for(int i = 0; i < s; i++) {
                int v = 1;
                for(int j = 0; j < p.second; j++) {
                    v *= p.first;
                    res.push_back(res[i] * v);
                }
            }
        }
        return res;
    }

    template<class T> void fast_zeta(vector< T > &f) {
        int N = f.size();
        vector<bool> isprime = Eratosthenes(N);
        for(int p = 2; p < N; p++) {
            if(!isprime[p]) continue;
            for(int k = (N - 1) / p; k >= 1; k--) {
                f[k] += f[k * p];
            }
        }
    }

    template<class T> void fast_mobius(vector< T > &F) {
        int N = F.size();
        vector<bool> isprime = Eratosthenes(N);
        for(int p = 2; p < N; p++) {
            if(!isprime[p]) continue;
            for(int k = 1; k * p < N; k++) {
                F[k] -= F[k * p];
            }
        }
    }

    template<class T> vector< T > gcd_convolution(const vector< T > &f, const vector< T > &g) {
        int N = max(f.size(), g.size());
        vector< T > F(N, 0), G(N, 0), H(N);
        for(int i = 0; i < f.size(); i++) F[i] = f[i];
        for(int i = 0; i < g.size(); i++) G[i] = g[i];

        fast_zeta(F);
        fast_zeta(G);

        for(int i = 1; i < N; i++) H[i] = F[i] * G[i];

        fast_mobius(H);
        return H;
    }

    int fast_euler_phi(int n) {
        auto pf = factorize(n);
        int res = n;
        for(auto p : pf) {
            res *= p.first - 1;
            res /= p.first;
        }
        return res;
    }
};

struct prime_factorize {
    long long N;
    vector<pair<long long, long long>> pf;

    prime_factorize(long long N) : N(N) {
        for(long long p = 2; p * p <= N; p++) {
            if(N % p != 0) continue;
            long long e = 0;
            while(N % p == 0) N /= p, e++;
            pf.push_back({p, e});
        }
        if(N != 1) pf.push_back({N, 1});
    }

    long long euler_phi() {
        long long res = N;
        for(auto p : pf) {
            res *= p.first - 1;
            res /= p.first;
        }
        return res;
    }
};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    int MX = 1299709;
    auto p = Eratosthenes(MX);
    int n;
    while(cin >> n, n) {
        if(p.isprime[n]){
            cout << 0 << '\n'; continue;
        }

        int l = n, r = n;
        while(!p.isprime[l]) l--;
        while(!p.isprime[r]) r++;
        cout << r - l << '\n';
    }
}
accuracy: 1
time_ms: 30
memory_kb: 13880
final_rank: 13

code_snippet: #ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
 
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
 
#pragma region Macros
// rep macro
#define foa(v, a) for(auto &v : a)
#define REPname(a, b, c, d, e, ...) e
#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int i = 0; i < (x); ++i)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))
#define REPSname(a, b, c, ...) c
#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)
#define REPS0(x) for(int i = 1; i <= (x); ++i)
#define REPS1(i, x) for(int i = 1; i <= (x); ++i)
#define RREPname(a, b, c, d, e, ...) e
#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)
#define RREP0(x) for(int i = (x)-1; i >= 0; --i)
#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))
#define RREPSname(a, b, c, ...) c
#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)
#define RREPS0(x) for(int i = (x); i >= 1; --i)
#define RREPS1(i, x) for(int i = (x); i >= 1; --i)
 
// name macro
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
template <class T = int>
using V = std::vector<T>;
template <class T = int>
using VV = std::vector<std::vector<T>>;
template <class T = int>
using VVV = std::vector<std::vector<std::vector<T>>>;
template <class T>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using ll = long long;
using ld = long double;
using int128 = __int128_t;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using Tp = tuple<ll,ll,ll>;
 
// input macro
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
	is >> p.first >> p.second;
	return is;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
	for(T &i : v) is >> i;
	return is;
}
std::istream &operator>>(std::istream &is, __int128_t &a) {
	std::string s;
	is >> s;
	__int128_t ret = 0;
	for(int i = 0; i < s.length(); i++)
		if('0' <= s[i] and s[i] <= '9')
			ret = 10 * ret + s[i] - '0';
	a = ret * (s[0] == '-' ? -1 : 1);
	return is;
}
namespace scanner {
void scan(int &a) { std::cin >> a; }
void scan(long long &a) { std::cin >> a; }
void scan(std::string &a) { std::cin >> a; }
void scan(char &a) { std::cin >> a; }
void scan(char a[]) { std::scanf("%s", a); }
void scan(double &a) { std::cin >> a; }
void scan(long double &a) { std::cin >> a; }
template <class T, class U>
void scan(std::pair<T, U> &p) { std::cin >> p; }
template <class T>
void scan(std::vector<T> &a) { std::cin >> a; }
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner
#define VEC(type, name, size)     \
	std::vector<type> name(size); \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                                    \
	std::vector<std::vector<type>> name(h, std::vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)             \
	std::string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
 
// output-macro
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {
	for(int i = 0; i < int(a.size()); ++i) {
		if(i) os << " ";
		os << a[i];
	}
	return os;
}
std::ostream &operator<<(std::ostream &dest, __int128_t &value) {
	std::ostream::sentry s(dest);
	if(s) {
		__uint128_t tmp = value < 0 ? -value : value;
		char buffer[128];
		char *d = std::end(buffer);
		do {
			--d;
			*d = "0123456789"[tmp % 10];
			tmp /= 10;
		} while(tmp != 0);
		if(value < 0) {
			--d;
			*d = '-';
		}
		int len = std::end(buffer) - d;
		if(dest.rdbuf()->sputn(d, len) != len) {
			dest.setstate(std::ios_base::badbit);
		}
	}
	return dest;
}
template <class T>
void print(const T a) { std::cout << a << '\n'; }
template <class Head, class... Tail>
void print(Head H, Tail... T) {
	std::cout << H << ' ';
	print(T...);
}
template <class T>
void printel(const T a) { std::cout << a << '\n'; }
template <class T>
void printel(const std::vector<T> &a) {
	for(const auto &v : a)
		std::cout << v << '\n';
}
template <class Head, class... Tail>
void printel(Head H, Tail... T) {
	std::cout << H << '\n';
	printel(T...);
}
void Yes(const bool b = true) { std::cout << (b ? "Yes\n" : "No\n"); }
void No() { std::cout << "No\n"; }
void YES(const bool b = true) { std::cout << (b ? "YES\n" : "NO\n"); }
void NO() { std::cout << "No\n"; }
void err(const bool b = true) {
	if(b) {
		std::cout << "-1\n", exit(0);
	}
}
 
//debug macro
namespace debugger {
template <class T>
void view(const std::vector<T> &a) {
	std::cerr << "{ ";
	for(const auto &v : a) {
		std::cerr << v << ", ";
	}
	std::cerr << "\b\b }";
}
template <class T>
void view(const std::vector<std::vector<T>> &a) {
	std::cerr << "{\n";
	for(const auto &v : a) {
		std::cerr << "\t";
		view(v);
		std::cerr << "\n";
	}
	std::cerr << "}";
}
template <class T, class U>
void view(const std::vector<std::pair<T, U>> &a) {
	std::cerr << "{\n";
	for(const auto &p : a) std::cerr << "\t(" << p.first << ", " << p.second << ")\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &p : m) std::cerr << "\t[" << p.first << "] : " << p.second << "\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::pair<T, U> &p) { std::cerr << "(" << p.first << ", " << p.second << ")"; }
template <class T>
void view(const std::set<T> &s) {
	std::cerr << "{ ";
	for(auto &v : s) {
		view(v);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}
 
template <class T>
void view(const T &e) { std::cerr << e; }
}  // namespace debugger
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	debugger::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(false)
#else
#define debug(...) (void(0))
#endif
 
// vector macro
template <class T>
int lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }
template <class T>
int ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }
template <class T>
void UNIQUE(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<T> press(std::vector<T> &a) {
	auto res = a;
	UNIQUE(res);
	for(auto &v : a)
		v = lb(res, v);
	return res;
}
#define SORTname(a, b, c, ...) c
#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)
#define SORT0(a) std::sort((a).begin(), (a).end())
#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })
template <class T>
void ADD(std::vector<T> &a, const T x) {
	for(auto &v : a) v += x;
}
template <class T>
void SUB(std::vector<T> &a, const T x = 1) {
	for(auto &v : a) v -= x;
}
template <class T>
void MUL(std::vector<T> &a, const T x) {
	for(auto &v : a) v *= x;
}
template <class T>
void DIV(std::vector<T> &a, const T x) {
	for(auto &v : a) v /= x;
}
 
// math macro
template <class T, class U>
inline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <class T, class U>
inline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <class T>
T divup(T x, T y) { return (x + y - 1) / y; }
template <class T>
T POW(T a, long long n) {
	T ret = 1;
	while(n) {
		if(n & 1) ret *= a;
		a *= a;
		n >>= 1;
	}
	return ret;
}

template<typename T>
T ADD(T a, T b){
	T res;
	return __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;
}

template<typename T>
T MUL(T a, T b){
	T res;
	return __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;
}

// modpow
long long POW(long long a, long long n, const int mod) {
	long long ret = 1;
	while(n) {
		if(n & 1) (ret *= a) %= mod;
		(a *= a) %= mod;
		n >>= 1;
	}
	return ret;
}
 
// others
struct fast_io {
	fast_io() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} fast_io_;

#pragma endregion

using R = long double;
constexpr R EPS=1E-11;

// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.
int sgn(const R& r){ return (r > EPS) - (r < -EPS); }
// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.
int sgn(const R& a, const R &b){ return sgn(a-b); }
// a > 0 は sgn(a) > 0
// a < b は sgn(a, b) < 0
// a >= b は sgn(a, b) >= 0
// のように書く.
// return s * 10^n

//https://atcoder.jp/contests/abc191/submissions/20028529
long long x10(const string& s, size_t n) {
    if (s.front() == '-') return -x10(s.substr(1), n);
    auto pos = s.find('.');
    if (pos == string::npos) return stoll(s + string(n, '0'));
    return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));
}
 
long long ceildiv(long long a, long long b) {
    if (b < 0) a = -a, b = -b;
    if (a >= 0) return (a + b - 1) / b;
    else return a / b;
}
 
long long floordiv(long long a, long long b) {
    if (b < 0) a = -a, b = -b;
    if (a >= 0) return a / b;
    else return (a - b + 1) / b;
}
 
long long floorsqrt(long long x) {
    assert(x >= 0);
    long long ok = 0;
    long long ng = 1;
    while (ng * ng <= x) ng <<= 1;
    while (ng - ok > 1) {
        long long mid = (ng + ok) >> 1;
        if (mid * mid <= x) ok = mid;
        else ng = mid;
    }
    return ok;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){//popcount%2
	return __builtin_parity(x);
}

const int inf = 1e9;
const ll INF = 1e18;

void main_() {
    ll N_MAX = 2e6;
	V<ll> p(N_MAX),q(N_MAX);
	for(ll i = 2;i < N_MAX;i++){
	    if(p[i] == 0){
	        q[i] = 1;
	        for(ll j = i;j < N_MAX;j+=i){
	            p[j] = 1;
	        }
	    }
	}
	V<ll> prime;
	REP(i,N_MAX){
	    if(q[i]){
	        prime.push_back(i);
	    }
	}
	ll n;
	cin >> n;
	while(n!=0){
	    if(q[n]){
	        cout << 0 << endl;
	    }
	    else{
	        auto it = upper_bound(prime.begin(),prime.end(),n) - prime.begin();
	        ll ans = prime[it] - prime[it-1];
	        cout << ans << endl;
	    }
	    cin >> n;
	}
}
 
int main() {
	int t = 1;
	//cin >> t;
	while(t--) main_();
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 36260
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
//SET PRECIOSION   cout << fixed ;  cout << setprecision(n) << ans << endl;
#define ll long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
ll SET(ll n, ll pos) { return n = n | (1 << pos ); }
ll RESET(ll n, ll pos) { return n = n & ~(1<<pos);}
bool CHECK(ll n, ll pos) { return (bool) (n & (1<<pos));}
vector <ll> v;
ll x,y,z,a,b,c,sum,ans,total,t,cnt,n,m,k,p,q,r,l,w,right,left,row,col,kase;
string s1,s2;
map<ll,ll> mp;
set<ll> s;
#define SIZE 10000008
map<ll,ll> :: iterator itr1, ptr1;
set<ll> :: iterator itr, ptr;
vector <int> prime; // Stores generated primes
char sieve[SIZE]; // 0 means prime
void primeSieve ( int n ) {
    sieve[0] = sieve[1] = 1; // 0 and 1 are not prime

    prime.push_back(2); // Only Even Prime
    for ( int i = 4; i <= n; i += 2 ) sieve[i] = 1; // Remove multiples of 2

    int sqrtn = sqrt ( n );
    for ( int i = 3; i <= sqrtn; i += 2 ) {
        if ( sieve[i] == 0 ) {
            for ( int j = i * i; j <= n; j += 2 * i ) sieve[j] = 1;
        }
    }

    for ( int i = 3; i <= n; i += 2 ) if ( sieve[i] == 0 ) prime.push_back(i);
}

int main()
{
	///Peace be with you.
	FAST;
	primeSieve(SIZE-8);
	while(cin>>n)
    {
        if(n==0)
            break;

        if(sieve[n]==0)
            cout<<0<<endl;
        else
        {
            auto itr = upper_bound(prime.begin(),prime.end(),n) ;
            x = *itr;
         itr--;
         y = *itr;
            cout<< x - y  <<endl;
        }
    }

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 16548
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
int isprime(int n)
{
    int flag=1;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            flag=0;
        }
    }
    return flag;
}
int main()
{



    int k;
    while(cin>>k)
    {
      if(k==0)
        break;
      if(isprime(k)==1)
        cout<<"0"<<endl;
      else
      {
          int res=0;
          for(int i=k+1;k<=1299709;i++)
          {

              res++;
              if(isprime(i)==1)
                break;
          }
          //cout<<"res="<<res<<endl;
           for(int i=k;k>=1;i--)
          {
              if(isprime(i)==1)
                break;
              res++;
          }
          cout<<res<<endl;
      }
    }


    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3036
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rrep(i,n) for(int (i)=((n)-1);(i)>=0;(i)--)
#define itn int
#define miele(v) min_element(v.begin(), v.end())
#define maele(v) max_element(v.begin(), v.end())
#define SUM(v) accumulate(v.begin(), v.end(), 0LL)
#define lb(a, key) lower_bound(a.begin(),a.end(),key)
#define ub(a, key) upper_bound(a.begin(),a.end(),key)
#define COUNT(a, key) count(a.begin(), a.end(), key) 
#define BITCOUNT(x) __builtin_popcount(x)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
using P = pair <int,int>;
using WeightedGraph = vector<vector <P>>;
using UnWeightedGraph = vector<vector<int>>;
using Real = long double;
using Point = complex<Real>; //Point and Vector2d is the same!
using Vector2d = complex<Real>;
const long long INF = 1LL << 60;
const int MOD = 1000000007;
const double EPS = 1e-15;
const double PI=3.14159265358979323846;
template <typename T> 
int getIndexOfLowerBound(vector <T> &v, T x){
    return lower_bound(v.begin(),v.end(),x)-v.begin();
}
template <typename T> 
int getIndexOfUpperBound(vector <T> &v, T x){
    return upper_bound(v.begin(),v.end(),x)-v.begin();
}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
#define DUMPOUT cerr
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
istream &operator>>(istream &is, Point &p) {
    Real a, b; is >> a >> b; p = Point(a, b); return is;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T,U> &p_var) {
    is >> p_var.first >> p_var.second;
    return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec) is >> x;
    return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
    DUMPOUT<<'{';
    os << pair_var.first;
    DUMPOUT<<',';
    os << " "<< pair_var.second;
    DUMPOUT<<'}';
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec) {
    DUMPOUT<<'[';
    for (int i = 0; i < vec.size(); i++) 
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    DUMPOUT<<']';
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<vector<T>> &df) {
  for (auto& vec : df) os<<vec;
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
    DUMPOUT << "{";
    repi(itr, map_var) {
        os << *itr;
        itr++;
        if (itr != map_var.end()) DUMPOUT << ", ";
        itr--;
    }
    DUMPOUT << "}";
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
    DUMPOUT << "{";
    repi(itr, set_var) {
        os << *itr;
        itr++;
        if (itr != set_var.end()) DUMPOUT << ", ";
        itr--;
    }
    DUMPOUT << "}";
    return os;
}
void print() {cout << endl;}
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(tail) != 0) cout << " ";
  print(forward<Tail>(tail)...);
}
void dump_func() {DUMPOUT << '#'<<endl;}
template <typename Head, typename... Tail>
void dump_func(Head &&head, Tail &&... tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) > 0) DUMPOUT << ", ";
    dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define dump(...)                                                              \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
            << endl                                                            \
            << "    ",                                                         \
        dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

vector <bool>  primes(1000000,1) ;
void Eratosthenes(int n){
    for(int i = 2; i <= sqrt(n); i++){
		if(primes[i]){
			for(int j = 0; i * (j + 2) <= n; j++){
				primes[i *(j + 2)] = 0;
			}
		}
	}
	primes[0] = 0; primes[1] = 0;
	for(int i = 0; i <= n; i++){
		if(primes[i]){
			cout << i << endl;
		}
	}
}
signed main(void) { cin.tie(0); ios::sync_with_stdio(false);
    int n = 2000000;
    vector <bool> isPrime(n+10, true);
    isPrime[1] = true;
    for(int i=2;i*i<=n;i++){
        if(isPrime[i]){
            for(int j=1;i*(j+1)<=n;j++){
                isPrime[i + i*(j)] = false;
            }
        }
    }
    
    vector <int> maePrime(n+10), nextPrime(n+10);
    int mae = 0;
    for(int i=0;i<n;i++){
        if(isPrime[i]) mae = i;
        maePrime[i] = mae;
    }
    int next = INF;
    for(int i=n-1;i>=0;i--){
        if(isPrime[i]) next = i;
        nextPrime[i] = next;
    }
    
    while(1){
        int n; cin>>n;
        if(n == 0) exit(0);
        print(abs(nextPrime[n] - maePrime[n]));
    }
}
accuracy: 1
time_ms: 10
memory_kb: 34764
final_rank: 17