alexjercan/bugnet huggingface dataset:
Problem p00855 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
vector<ll> p(1300001);

void era() {
  for (int i = 2; i < 1300001; i++) {
    if (p[i] == 0)
      continue;
    int j = i;
    while (j < 1300001) {
      j += i;
      if (j >= 1300001)
        break;
      p[j] = 0;
    }
  }
}

int main() {
  rep(i, 1300001) p[i] = i;
  era();
  for (;;) {
    ll n;
    cin >> n;
    if (!n)
      break;
    ll ans = 0;
    ll i = n, j = n;
    while (p[i] == 0) {
      i++;
    }
    while (p[j] == 0) {
      j--;
    }
    ans = p[i] - p[j];
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
vector<ll> p(1000001);

void era() {
  for (int i = 2; i < 1000001; i++) {
    if (p[i] == 0)
      continue;
    int j = i;
    while (j < 1000001) {
      j += i;
      if (j >= 1000001)
        break;
      p[j] = 0;
    }
  }
}

int main() {
  rep(i, 1000001) p[i] = i;
  era();
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    ll ans = 0;
    int i = n, j = n;
    while (p[i] == 0) {
      i++;
    }
    while (p[j] == 0) {
      j--;
    }
    ans = p[i] - p[j];
    cout << ans << endl;
  }
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

const int max_p = 1300000;
bool prime[max_p + 1];

void initial_prime() {
  for (int i = 0; i < max_p; i++)
    prime[i] = true;
  prime[0] = prime[1] = false;

  for (int i = 2; i * i < max_p; i++)
    if (prime[i])
      for (int j = 2 * i; j <= max_p; j += i)
        prime[j] = false;
}

int main() {
  int k;

  initial_prime();

  while (cin >> k, k) {
    int l, r;
    int ans = 0;

    l = r = k;
    while (!prime[l--])
      ans++;
    while (!prime[r++])
      ans++;
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

const int max_p = 1299709;
bool prime[max_p + 1];

void initial_prime() {
  for (int i = 0; i < max_p; i++)
    prime[i] = true;
  prime[0] = prime[1] = false;

  for (int i = 2; i * i < max_p; i++)
    if (prime[i])
      for (int j = 2 * i; j <= max_p; j += i)
        prime[j] = false;
}

int main() {
  int k;

  initial_prime();

  while (cin >> k, k) {
    int l, r;
    int ans = 0;

    l = r = k;
    while (!prime[l--])
      ans++;
    while (!prime[r++])
      ans++;
    cout << ans << endl;
  }

  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

vector<ll> prime;

int main() {
  prime.push_back(2);
  for (ll i = 3; i < 1300000; i += 2) {
    bool flag = true;
    for (ll j = 0; j < prime.size() && i >= prime[j] * prime[j]; j++) {
      if (i % prime[j] == 0) {
        flag = false;
        break;
      }
    }
    if (flag)
      prime.push_back(i);
  }
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    ll to;
    for (to = 0; n > prime[to]; to++)
      ;
    if (n == prime[to])
      cout << "0" << endl;
    else
      cout << prime[to] - prime[to - 1] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;

vector<ll> prime;

int main() {
  prime.push_back(2);
  for (ll i = 3; i < 1200000; i += 2) {
    bool flag = true;
    for (ll j = 0; j < prime.size() && i >= prime[j] * prime[j]; j++) {
      if (i % prime[j] == 0) {
        flag = false;
        break;
      }
    }
    if (flag)
      prime.push_back(i);
  }
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    ll to;
    for (to = 0; n > prime[to]; to++)
      ;
    if (n == prime[to])
      cout << "0" << endl;
    else
      cout << prime[to] - prime[to - 1] << endl;
  }
  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
using namespace std;

int prime[1299709];
void init() {
  for (int i = 2; i < 1300000; i++) {
    if (prime[i])
      continue;
    prime[i] = 1;
    for (int j = i + i; j < 1300000; j += i)
      prime[j] = -1;
  }
}

int main() {
loop:
  init();
  int n;
  cin >> n;
  if (!n)
    return 0;
  if (prime[n] == 1) {
    cout << 0 << endl;
    goto loop;
  }
  int b;
  for (int i = 0; i < 1299709; i++) {
    if (prime[i] == 1 && i > n) {
      cout << i - b << endl;
      goto loop;
    } else if (prime[i] == 1)
      b = i;
  }
}

Error: TLE

Fixed submission:


#include <iostream>
using namespace std;

int prime[1300000];
void init() {
  for (int i = 2; i < 1300000; i++)
    prime[i] = -1;
  for (int i = 2; i < 1300000; i++) {
    if (prime[i] != -1)
      continue;
    prime[i] = 1;
    for (int j = i + i; j < 1300000; j += i)
      prime[j] = 0;
  }
}

int main() {
loop:
  init();
  int n;
  cin >> n;
  if (!n)
    return 0;
  if (prime[n]) {
    cout << 0 << endl;
    goto loop;
  }
  int b;
  for (int i = 0; i < 1300000; i++) {
    if (prime[i] && i > n) {
      cout << i - b << endl;
      goto loop;
    } else if (prime[i])
      b = i;
  }
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

bool prime[2000000];
int gap[1300000];

int main() {
  rep(i, 2000000) prime[i] = true;
  prime[0] = prime[1] = false;
  loop(i, 2, 10000) for (int j = i * 2; j < 2000000; j += i) prime[j] = false;
  rep(i, 1300000) gap[i] = -1;
  int pre = 2;
  loop(i, 3, 1300000) {
    if (prime[i]) {
      gap[i] = i - pre;
      pre = i;
    }
  }
  for (int i = 1300000 - 1; i >= 2; i--) {
    if (gap[i] == -1)
      gap[i] = pre;
    else {
      pre = gap[i];
      gap[i] = 0;
    }
  }
  gap[2] = gap[1] = gap[0] = 0;
  int k;
  while (cin >> k, k)
    cout << gap[k] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

bool prime[1000000];
int gap[1300000];

int main() {
  rep(i, 1000000) prime[i] = true;
  prime[0] = prime[1] = false;
  loop(i, 2, 1000) for (int j = i * 2; j < 1000000; j += i) prime[j] = false;
  rep(i, 1300000) gap[i] = -1;
  int pre = 2;
  loop(i, 3, 1300000) {
    if (prime[i]) {
      gap[i] = i - pre;
      pre = i;
    }
  }
  for (int i = 1300000 - 1; i >= 2; i--) {
    if (gap[i] == -1)
      gap[i] = pre;
    else {
      pre = gap[i];
      gap[i] = 0;
    }
  }
  gap[2] = gap[1] = gap[0] = 0;
  int k;
  while (cin >> k, k)
    cout << gap[k] << endl;
  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[1299710] = {1, 2, 0, 0}, c, i, j;
  for (i = 4; i < 1299710; i++) {
    for (j = 2, c = 0; j * j <= i; j++)
      if (i % j == 0)
        c++;
    if (c)
      a[i] += a[i - 1] + 1;
    else
      a[i] = 0;
  }
  for (i = 1299709; i >= 0; i--) {
    if (a[i]) {
      for (j = 0, c = a[i]; j < c; j++)
        a[i--] = c + 1;
      i++;
    }
  }
  while (cin >> n, n)
    cout << a[n] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[1000001] = {1, 2, 0, 0}, c, i, j;
  for (i = 4; i < 1000000; i++) {
    for (j = 2, c = 0; j * j <= i; j++)
      if (i % j == 0)
        c++;
    if (c)
      a[i] += a[i - 1] + 1;
    else
      a[i] = 0;
  }
  for (i = 999999; i >= 0; i--) {
    if (a[i]) {
      for (j = 0, c = a[i]; j < c; j++)
        a[i--] = c + 1;
      i++;
    }
  }
  while (cin >> n, n)
    cout << a[n] << endl;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

const int MAX = 10000100;
bool prime[MAX];
void make_prime() {
  for (int i = 0; i < MAX; i++)
    prime[i] = true;
  prime[0] = prime[1] = false;
  for (int i = 0; i < MAX; i++)
    if (prime[i]) {
      for (int j = i * 2; j < MAX; j += i)
        prime[j] = false;
    }
}

int main(void) {
  make_prime();
  long long int n;
  while (cin >> n, n) {
    if (prime[n]) {
      cout << 0 << endl;
      continue;
    }
    long long int k, j;

    for (k = n; !prime[k]; k--)
      ;
    for (j = n; !prime[j]; j++)
      ;
    cout << j - k << endl;
  }
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

const int MAX = 100001;
bool prime[MAX];
void make_prime() {
  for (int i = 0; i < MAX; i++) {
    prime[i] = true;
  }

  prime[0] = false;
  prime[1] = false;

  for (int i = 2; i <= MAX; i++) {
    if (prime[i]) {
      for (int j = i + i; j <= MAX; j += i) {
        prime[j] = false;
      }
    }
  }
}

int main(void) {
  make_prime();
  int n;
  while (cin >> n, n) {
    if (prime[n]) {
      cout << 0 << endl;
      continue;
    }
    int k, j;
    for (k = n; !prime[k]; k--)
      ;
    for (j = n; !prime[j]; j++)
      ;
    cout << j - k << endl;
  }
}
Problem p00855 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX_LIST 10000000

int main() {
  bool *prime = (bool *)malloc(sizeof(bool) * MAX_LIST);
  // bool prime[MAX_LIST];
  for (int i = 0; i < MAX_LIST; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;
  for (int i = 0; i * i < MAX_LIST; i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < MAX_LIST; j = j + i) {
        prime[j] = 0;
      }
    }
  }
  int in;
  while (cin >> in, in) {
    int ans = 0;
    int i = 0;
    while (prime[in + i] == 0) {
      ans++;
      i++;
    }
    i = 0;
    while (prime[in - i] == 0) {
      ans++;
      i++;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX_LIST 100000000

int main() {
  bool *prime = (bool *)malloc(sizeof(bool) * MAX_LIST);
  // bool prime[MAX_LIST];
  for (int i = 0; i < MAX_LIST; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;
  for (int i = 0; i * i < MAX_LIST; i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < MAX_LIST; j = j + i) {
        prime[j] = 0;
      }
    }
  }
  int in;
  while (cin >> in, in) {
    int ans = 0;
    int i = 0;
    while (prime[in + i] == 0) {
      ans++;
      i++;
    }
    i = 0;
    while (prime[in - i] == 0) {
      ans++;
      i++;
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
#define MAX 1300000

vector<int> prime(MAX);

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

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

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

Error: 0

Fixed submission:

#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;
      }
    }
    int ans = e - s;
    cout << ans << endl;
  }
}
Problem p00855 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int X, Y, A, i, j;
int main() {
  while (true) {
    cin >> A;
    if (A == 0) {
      break;
    }
    for (i = A; i >= 2; i--) {
      for (j = 2; j <= sqrt(i); j++) {
        if (i % j == 0) {
          goto E;
        }
      }
      X = i;
      goto F;
    E:;
    }
  F:;
    for (i = A; i < 99999999; i++) {
      for (j = 2; j <= sqrt(i); j++) {
        if (i % j == 0) {
          goto G;
        }
      }
      Y = i;
      goto H;
    G:;
    }
  H:;
    cout << Y - X << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int X, Y, A, i, j;
int main() {
  while (true) {
    cin >> A;
    for (i = A; i >= 2; i--) {
      for (j = 2; j <= sqrt(i); j++) {
        if (i % j == 0) {
          goto E;
        }
      }
      X = i;
      goto F;
    E:;
    }
  F:;
    for (i = A; i < 99999999; i++) {
      for (j = 2; j <= sqrt(i); j++) {
        if (i % j == 0) {
          goto G;
        }
      }
      Y = i;
      goto H;
    G:;
    }
  H:;
    cout << Y - X << endl;
  }
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void) {
  const int N = 1299710;
  bool isP[N];
  rep(i, N) isP[i] = true;
  for (int i = 2; i * i < N; i++)
    if (isP[i])
      for (int j = i; i * j < N; j++)
        isP[i * j] = false;

  for (int n, r, res; cin >> n, n; cout << res << endl) {
    r = n;
    if (isP[n])
      res = 0;
    else {
      while (!isP[--r])
        ;
      while (!isP[++n])
        ;
    }
    res = n - r;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define COUNT(i, n) for (int i = 1; i <= n; i++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for (ITER(c) it = (c).begin(); it != (c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void) {
  const int N = (int)1e6;
  bool isP[N];
  rep(i, N) isP[i] = true;
  for (int i = 2; i * i < N; i++)
    if (isP[i])
      for (int j = i; i * j < N; j++)
        isP[i * j] = false;

  for (int n, r, res; cin >> n, n; cout << res << endl) {
    r = n;
    if (isP[n])
      res = 0;
    else {
      while (!isP[--r])
        ;
      while (!isP[++n])
        ;
    }
    res = n - r;
  }

  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int p[1322501];

int main() {
  rep(i, 2, 1150) {
    int j = i;
    while (j + i < 1322500) {
      j += i;
      p[j]++;
    }
  }

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

Error: -11

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int p[1322501];

int main() {
  rep(i, 2, 1150) {
    int j = i;
    while (j < 1322500) {
      j += i;
      p[j]++;
    }
  }

  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    if (!p[n])
      cout << 0 << endl;
    else {
      int l = n, r = n;
      while (p[l])
        l--;
      while (p[r])
        r++;
      cout << r - l << endl;
    }
  }
  return 0;
}
Problem p00855 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  bool pri[1300000];
  //  vector<int> price;
  fill(pri, pri + 1300000, true);
  pri[0] = pri[1] = false;
  for (int i = 2; i < 1300000; i++) {
    if (pri[i] == 0)
      continue;
    //  price.push_back(i);
    for (int j = i + i; j < 1300000; j += i) {
      pri[j] = false;
    }
  }
  int n;
  for (; cin >> n, n;) {

    if (pri[n])
      cout << "0" << endl;
    else {
      int max, min;
      for (int i = n - 1;; i--) {
        if (pri[i]) {
          min = i;
          break;
        }
      }
      for (int i = n + 1;; i++) {
        if (pri[i]) {
          max = i;
          break;
        }
      }
      cout << max - min << endl;
    }
  }
}

Error: 0

Fixed submission:

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

int main() {
  bool pri[1000001];
  //  vector<int> price;
  fill(pri, pri + 1000001, true);
  pri[0] = pri[1] = false;
  for (int i = 2; i < 1000001; i++) {
    if (pri[i] == 0)
      continue;
    //  price.push_back(i);
    for (int j = i + i; j < 1000001; j += i) {
      pri[j] = false;
    }
  }
  int n;
  for (; cin >> n, n;) {

    if (pri[n])
      cout << "0" << endl;
    else {
      int max, min;
      for (int i = n - 1;; i--) {
        if (pri[i]) {
          min = i;
          break;
        }
      }
      for (int i = n + 1;; i++) {
        if (pri[i]) {
          max = i;
          break;
        }
      }
      cout << max - min << endl;
    }
  }
}
Problem p00855 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

const int M = 1299800;
bool isPrime[M];

vector<int> primes;

void sieve() {
  for (int i = 2; i < M; i++)
    isPrime[i] = true;
  for (int i = 2; i * i < M; i++) {
    if (!isPrime[i])
      continue;
    for (int j = i * i; j < M; j += i) {
      isPrime[j] = false;
    }
  }
  for (int i = 2; i < M; i++) {
    if (isPrime[i])
      primes.push_back(i);
  }
}

int main() {
  sieve();
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    if (isPrime[n]) {
      cout << 0 << endl;
    } else {
      cout << (*upper_bound(all(primes), n)) -
                  *(upper_bound(all(primes), n) - 1)
           << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

const int M = 1299800;
bool isPrime[M];

vector<int> primes;

void sieve() {
  for (int i = 2; i < M; i++)
    isPrime[i] = true;
  for (int i = 2; i * i < M; i++) {
    if (!isPrime[i])
      continue;
    for (int j = i * i; j < M; j += i) {
      isPrime[j] = false;
    }
  }
  for (int i = 2; i < M; i++) {
    if (isPrime[i])
      primes.push_back(i);
  }
}

int main() {
  sieve();
  while (true) {
    int n;
    cin >> n;
    if (isPrime[n]) {
      cout << 0 << endl;
    } else {
      cout << (*upper_bound(all(primes), n)) -
                  *(upper_bound(all(primes), n) - 1)
           << endl;
    }
  }
  return 0;
}