alexjercan/bugnet huggingface dataset:
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {

  vector<int> arr(80000);

  for (int i = 0; i < 80000; ++i)
    arr[i] = 1;

  for (int i = 2; i < sqrt(80000); ++i) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < 80000; j++)
        arr[i * (j + 2)] = 0;
    }
  }

  int n;

  while (cin >> n) {

    for (int i = n - 1; i >= 0; --i) {
      if (arr[i] == 1) {
        cout << i << " ";
        break;
      }
    }

    for (int i = n + 1; i <= 80000; ++i) {
      if (arr[i] == 1) {
        cout << i << endl;
        break;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  vector<int> arr(80000);

  for (int i = 0; i < 80000; ++i)
    arr[i] = 1;

  for (int i = 2; i < sqrt(80000); ++i) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < 80000; j++)
        arr[i * (j + 2)] = 0;
    }
  }

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

    for (int i = n - 1; i >= 0; --i) {
      if (arr[i] == 1) {
        cout << i << " ";
        break;
      }
    }

    for (int i = n + 1; i <= 80000; ++i) {
      if (arr[i] == 1) {
        cout << i << endl;
        break;
      }
    }
  }
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string.h>
using namespace std;

int isprime[50101];

int main() {
  memset(isprime, 0, sizeof(isprime));
  for (int i = 4; i < 50101; i += 2)
    isprime[i] = 1;
  isprime[1] = 1;
  for (int i = 3; i * i < 50101; i += 2)
    if (isprime[i] == 0)
      for (int j = 2; i * j < 50101; ++j)
        isprime[i * j] = 1;

  int n;
  while (cin >> n) {
    for (int k = n - 1; k > 0; --k)
      if (isprime[k] == 0) {
        cout << k << " ";
        break;
      }

    for (int k = n + 1; k < 50101; ++k)
      if (isprime[k] == 0) {
        cout << k << endl;
        break;
      }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;

int isprime[50101];

int main() {
  memset(isprime, 0, sizeof(isprime));
  for (int i = 4; i < 50101; i += 2)
    isprime[i] = 1;
  isprime[1] = 1;
  for (int i = 3; i * i < 50101; i += 2)
    if (isprime[i] == 0)
      for (int j = 2; i * j < 50101; ++j)
        isprime[i * j] = 1;

  int n;
  while (cin >> n, n) {
    for (int k = n - 1; k > 0; --k)
      if (isprime[k] == 0) {
        cout << k << " ";
        break;
      }

    for (int k = n + 1; k < 50101; ++k)
      if (isprime[k] == 0) {
        cout << k << endl;
        break;
      }
  }
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
using namespace std;

int primemax(int m);
int primemin(int m);

int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    cout << primemax(n - 1) << " " << primemin(n + 1) << endl;
  }
}

int primemax(int n) {
  int t;
  for (n;; n--) {
    for (t = 2; t <= n; t++) {
      if (t == n) {
        return n;
      } else if (n % t == 0) {
        break;
      }
    }
  }
}
int primemin(int n) {
  int t;
  for (n;; n++) {
    for (t = 2; t <= n; t++) {
      if (t == n) {
        return t;
      } else if (n % t == 0) {
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

int primemax(int m);
int primemin(int m);

int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    cout << primemax(n - 1) << " " << primemin(n + 1) << endl;
  }
}

int primemax(int m) {
  int result = 0;
  while (true) {
    for (int i = m;; i--) {
      for (int j = i; j > 0; j--) {
        if ((i % j) == 0)
          break;
        if (j == 2) {
          result = i;
          return result;
        }
      }
    }
  }
}

int primemin(int m) {
  int result = 0;
  while (1) {
    for (int i = m;; i++) {
      for (int j = i; j > 0; j--) {
        if ((i % j) == 0)
          break;
        if (j == 2) {
          result = i;
          return result;
        }
      }
    }
  }
}
Problem p00044 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <math.h>
using namespace std;

int main() {
  bool is_prime[52000];
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i < 52000; i++)
    is_prime[i] = true;
  for (int i = 2; i <= sqrt(51999); i++) {
    if (is_prime[i]) {
      for (int j = 2; i * j <= 51999; j++)
        is_prime[i * j] = false;
    }
  }

  int n;
  while (cin >> n) {
    int index = n - 1;
    while (!is_prime[index]) {
      index--;
    }
    cout << index << ' ';

    index = n + 1;
    while (!is_prime[index]) {
      index++;
    }
    cout << index << endl;
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;

int main() {
  bool is_prime[52000];
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i < 52000; i++)
    is_prime[i] = true;
  for (int i = 2; i <= sqrt(51999); i++) {
    if (is_prime[i]) {
      for (int j = 2; i * j <= 51999; j++)
        is_prime[i * j] = false;
    }
  }

  int primes[52000];
  int index = 0;
  for (int i = 2; i <= 51999; i++)
    if (is_prime[i]) {
      primes[index] = i;
      index++;
    }

  int n;
  index = 0;
  while (cin >> n) {
    while (primes[index] != n) {
      index++;
    }
    cout << primes[index - 1] << ' ' << primes[index + 1] << endl;
  }
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  const int END = -1;
  const int NUM = 50022;
  int num;
  int i, index, index2, index3;

  cin >> num;

  vector<int> eratos(NUM);

  for (i = 0; i < NUM - 1; i++)
    eratos[i] = i + 2;
  eratos[i] = END;

  while (!cin.eof()) {
    i = 0;
    index3 = 0;
    while (eratos[i] != END) {
      if (eratos[i] != 0) {
        index = i + 1;
        index3 = i + 1;
        while (eratos[index] != END) {
          if (eratos[index] % eratos[i] != 0) {
            eratos[index3] = eratos[index];
            index3++;
          }
          index++;
        }
        eratos[index3] = END;
      }
      i++;
    }

    for (i = 0; i < index3; i++) {
      if (eratos[i] == num) {
        cout << eratos[i - 1] << " " << eratos[i + 1] << endl;
        break;
      } else if (eratos[i] > num) {
        cout << eratos[i - 1] << ' ' << eratos[i] << endl;
        break;
      }
    }
    cin >> num;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  const int END = -1;
  const int NUM = 50000;
  int num;
  int i, index, index2;

  cin >> num;

  vector<int> eratos(NUM);

  for (i = 0; i < NUM - 1; i++)
    eratos[i] = i + 2;
  eratos[i] = END;

  while (!cin.eof()) {
    i = 0;
    index2 = 0;
    while (eratos[i] != END) {
      if (eratos[i] != 0) {
        index = i + 1;
        while (eratos[index] != END) {
          if (eratos[index] % eratos[i] == 0) {
            eratos[index] = 0;
          }
          index++;
        }

        eratos[index2] = eratos[i];

        index2++;
      }

      if (eratos[index2 - 1] > num)
        break;
      i++;
    }
    if (eratos[index2 - 2] == num)
      cout << eratos[index2 - 3] << " " << eratos[index2 - 1] << endl;
    else
      cout << eratos[index2 - 2] << " " << eratos[index2 - 1] << endl;

    cin >> num;
  }
  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define N (50500)

bool prime[N + 1];

int main() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;

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

  int n;
  while (~scanf("%d", &n)) {
    int l = n - 1;
    while (!prime[l])
      l--;
    int r = n + 1;
    while (!prime[r])
      r++;
    printf("%d %d\\n", l, r);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define N (50000)

bool prime[N + 1];

int main() {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;

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

  int n;
  while (~scanf("%d", &n)) {
    int l = n - 1;
    while (!prime[l])
      l--;
    int r = n + 1;
    while (!prime[r])
      r++;
    printf("%d %d\\n", l, r);
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

bool isPrime(int p) {

  if (p < 2)
    return false;
  if (p == 2)
    return true;
  if ((p & 1) == 0)
    return false;

  for (int x = 2; x <= sqrt(p); ++x)
    if (p % x == 0)
      return false;

  return true;
}

int main() {
  int n;
  while (cin >> n) {
    int j = n - 1;
    while (!isPrime(j))
      j--;
    cout << j << " ";
    j = n + 1;
    while (!isPrime(j))
      j++;
    cout << j << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
bool isPrime[50005];

void generate() {
  memset(isPrime, true, sizeof(isPrime));
  isPrime[0] = isPrime[1] = false;
  for (int x = 2; x < 50005; ++x) {
    if (isPrime[x]) {
      for (int i = x * x; i < 50005; i += x) {
        isPrime[i] = false;
      }
    }
  }
}

int main() {
  generate();
  int n;
  while (cin >> n) {
    int j = n - 1;
    while (!isPrime[j]) {
      j--;
    }
    cout << j << " ";
    j = n + 1;
    while (!isPrime[j]) {
      j++;
    }
    cout << j << endl;
  }
  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  bool is_prime[100000];

  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < 100000; i++) {
    is_prime[i] = true;
  }

  for (int i = 2; i * i < 100000; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j < 100000; j = j + i) {
        is_prime[j] = false;
      }
    }
  }

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

    if (cin.eof())
      break;

    int high = n;
    int low = n;

    while (1) {
      high++;
      if (is_prime[high]) {
        break;
      }
    }

    while (1) {
      low--;
      if (is_prime[low]) {
        break;
      }
    }

    cout << low << " " << high << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  bool is_prime[100000];

  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < 100000; i++) {
    is_prime[i] = true;
  }

  for (int i = 2; i * i < 100000; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j < 100000; j = j + i) {
        is_prime[j] = false;
      }
    }
  }

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

    int a, b;
    int cnt = 0;
    for (int i = 3; i <= n; i++) {
      if (is_prime[i]) {
        a = i;
        for (int j = 3; j <= n; j++) {
          if (is_prime[j]) {
            b = j;
            if (a + b == n) {
              cnt++;
            }
          }
        }
      }
    }
    if (n == 0)
      break;

    cout << cnt / 2 + 1 << endl;
  }

  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define MAX 500021

int main() {
  bool b[MAX];
  fill(b, b + MAX, false);
  b[0] = b[1] = true;
  for (int i = 2; i < MAX; i++) {
    if (!b[i]) {
      for (int j = 2; i * j < MAX; j++) {
        b[i * j] = true;
      }
    }
  }

  int n, x, y;
  while (cin >> n) {
    for (x = n - 1; b[x]; x--)
      ;
    for (y = n + 1; b[y]; y++)
      ;
    cout << x << " " << y << endl;
  }
  return 0;
}
/*
1
y eazqyp pnop pngtg ye obmpngt xmybp mr lygw
*/

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 50009

int main() {
  bool b[MAX];
  fill(b, b + MAX, false);
  b[0] = b[1] = true;
  for (int i = 2; i < MAX; i++) {
    if (!b[i]) {
      for (int j = 2; i * j < MAX; j++) {
        b[i * j] = true;
      }
    }
  }

  int n, x, y;
  while (cin >> n, n) {
    for (x = n - 1; b[x]; x--)
      ;
    for (y = n + 1; b[y]; y++)
      ;
    cout << x << " " << y << endl;
  }
  return 0;
}
/*
1
y eazqyp pnop pngtg ye obmpngt xmybp mr lygw
*/
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define prime 50021
#define Sprime 223

int main() {
  int sn, bn, n;
  bool not_prime[prime + 1];
  for (int i = 2; i <= Sprime; i++) {
    if (!not_prime[i]) {
      for (int j = i + i; j <= prime; j += i) {
        not_prime[j] = true;
      }
    }
  }
  while (cin >> n) {
    sn = bn = n;
    while (not_prime[--sn])
      ;
    while (not_prime[++bn])
      ;
    cout << sn << " " << bn << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool prime(int n) {
  switch (n) {
  case 1:
    return false;
  case 2:
    return true;
  case 3:
    return true;
  }
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  if (n % 6 != 1 && n % 6 != 5)
    return false;
  for (int i = 5; i * i <= n; i += 6) {
    if (n % i == 0)
      return false;
    if (n % (i + 2) == 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  while (cin >> n) {
    int sn = 0;
    int bn = 0;
    if (n % 2 == 0) {
      for (int i = 1;; i += 2) {
        if (sn == 0 && prime(n - i) == true)
          sn = n - i;
        if (bn == 0 && prime(n + i) == true)
          bn = n + i;
        if (sn != 0 && bn != 0)
          break;
      }
    } else {
      for (int i = 2;; i += 2) {
        if (sn == 0 && prime(n - i) == true)
          sn = n - i;
        if (bn == 0 && prime(n + i) == true)
          bn = n + i;
        if (sn != 0 && bn != 0)
          break;
      }
    }
    cout << sn << " " << bn << endl;
  }
  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
vector<int> sosu(60000, 1);

void eratos() {
  for (int i = 2; i < 30000; i++) {
    sosu[2 * i] = 0;
  }
  for (int i = 3; i < sqrt(60000); i += 2) {
    if (sosu[i] == 1) {
      for (int j = 1; i * (j + 2) < 60000; j++) {
        sosu[i * (j + 2)] = 0;
      }
    }
  }
}

int main() {
  eratos();
  int n, i;

  while (cin >> n) {

    for (i = n - 1; i >= 0; i--) {
      if (sosu[i] == 1) {
        cout << i << ' ';
        break;
      }
    }

    for (i = n + 1; i < 60000; i++) {
      if (sosu[i] == 1) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
vector<int> sosu(60000, 1);

void eratos() {
  for (int i = 2; i < 30000; i++) {
    sosu[2 * i] = 0;
  }
  for (int i = 3; i < sqrt(60000); i += 2) {
    if (sosu[i] == 1) {
      for (int j = 1; i * (j + 2) < 60000; j++) {
        sosu[i * (j + 2)] = 0;
      }
    }
  }
}

int main() {
  eratos();

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

    for (i = n - 1; sosu[i] == 0; i--)
      ;
    cout << i << ' ';

    for (i = n + 1; sosu[i] == 0; i++)
      ;
    cout << i << endl;
  }
  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

bool is_prime(int n) {
  if (n <= 1)
    return false;

  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n;

  bool is_prime[1000000];
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < 1000000; i++) {
    is_prime[i] = true;
  }
  for (int i = 2; i * i < 1000000; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j < 1000000; j += i) {
        is_prime[j] = false;
      }
    }
  }

  int n_prime[1000000];

  int cnt = 0;
  for (int i = 0; i < 1000000; i++) {
    if (is_prime[i]) {
      cnt++;
    }
    n_prime[i] = cnt;
  }

  while (1) {
    int primen;
    cin >> n;
    if (cin.eof())
      break;

    for (int i = n - 1;; i--) {
      if (is_prime[i]) {
        cout << i << " ";
        break;
      }
    }

    for (int i = n + 1;; i++) {
      if (is_prime[i]) {
        cout << i << endl;
        break;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define N 50000

int main() {
  int n;

  bool is_prime[N];
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < N; i++) {
    is_prime[i] = true;
  }
  for (int i = 2; i * i < N; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j < N; j += i) {
        is_prime[j] = false;
      }
    }
  }

  while (1) {
    int counter = 0;
    cin >> n;
    if (n == 0)
      break;

    int i = 0;
    while (i <= n / 2) {
      if (is_prime[n - i] && is_prime[n])
        counter++;
      i++;
    }
    cout << counter << endl;
  }
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n;
  int max, first;
  bool flag = false;
  while (cin >> n, !cin.eof()) {
    for (int i = 2; i < n; i++) {
      for (int j = 2; j < i; j++) {
        if (i % j == 0) {
          break;
        }
        if (i / 2 < j) {
          max = i;
          break;
        }
      }
    }
    if (n == 3) {
      max = 2;
    }
    cout << max << ' ';
    bool flag = false;
    for (int i = n + 1;; i++) {
      for (int j = 2; j < i; j++) {
        if (i % j == 0) {
          break;
        }
        if (i / 2 < j) {
          first = i;
          flag = true;
          break;
        }
      }
      if (flag)
        break;
    }
    cout << first << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  int max, first;
  bool flag = false;
  while (cin >> n, !cin.eof()) {
    for (int i = 2; i < n; i++) {
      for (int j = 2; j < i; j++) {
        if (i % j == 0) {
          break;
        }
        if (i / 2 < j) {
          max = i;
        }
      }
    }
    cout << max << ' ';
    bool flag = false;
    for (int i = n + 1;; i++) {
      for (int j = 2; j < i; j++) {
        if (i % j == 0) {
          break;
        }
        if (i / 2 < j) {
          first = i;
          flag = true;
          break;
        }
      }
      if (flag)
        break;
    }
    cout << first << endl;
  }
  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

bool is_prime[70001];

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

int main(void) {
  sieve(70000);
  int a, b;
  bool ch = true;
  while (~scanf("%d", &a)) {
    ch = true;
    b = a;
    while (ch) {
      b--;
      if (is_prime[b]) {
        printf("%d ", b);
        ch = false;
      }
    }
    ch = true;
    b = a;
    while (ch) {
      b++;
      if (is_prime[b]) {
        printf("%d\\n", b);
        ch = false;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

bool is_prime[50001];

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

int main(void) {
  sieve(50000);
  int a, b;
  bool ch = true;
  while (~scanf("%d", &a)) {
    ch = true;
    b = a;
    while (ch) {
      b--;
      if (is_prime[b]) {
        printf("%d ", b);
        ch = false;
      }
    }
    ch = true;
    b = a;
    while (ch) {
      b++;
      if (is_prime[b]) {
        printf("%d\\n", b);
        ch = false;
      }
    }
  }
  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stdio.h>
#define N 50060

void Eratos(void);

int prime[N];

int main(void) {
  int n;
  int lessmax, bigmin;

  Eratos();

  while (scanf("%d", &n) != EOF) {
    for (int i = n - 1;; i--) {
      if (prime[i] == 1) {
        lessmax = i;
        break;
      }
    }
    for (int i = n + 1;; i++) {
      if (prime[i] == 1) {
        bigmin = i;
        break;
      }
    }

    printf("%d %d\\n", lessmax, bigmin);
  }

  return 0;
}

void Eratos(void) {
  for (int i = 0; i < N; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;

  for (int i = 2; i <= sqrt(N); i++) {
    for (int j = 2; i * j < N; j++) {
      prime[i * j] = 0;
    }
  }
}

Error: 0

Fixed submission:

#include <math.h>
#include <stdio.h>
#define N 50001

void Eratos(void);

int prime[N];

int main(void) {
  int n;
  int lessmax, bigmin;

  Eratos();

  while (scanf("%d", &n) != EOF) {
    for (int i = n - 1;; i--) {
      if (prime[i] == 1) {
        lessmax = i;
        break;
      }
    }
    for (int i = n + 1;; i++) {
      if (prime[i] == 1) {
        bigmin = i;
        break;
      }
    }

    printf("%d %d\\n", lessmax, bigmin);
  }

  return 0;
}

void Eratos(void) {
  for (int i = 0; i < N; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;

  for (int i = 2; i <= sqrt(N); i++) {
    for (int j = 2; i * j < N; j++) {
      prime[i * j] = 0;
    }
  }
}
Problem p00044 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
#define MAX 1000000
  bool sosu[MAX] = {0};
  for (int i = 4; i < MAX; i += 2)
    sosu[i] = true;
  for (int i = 3; i < MAX; i += 2) {
    if (sosu[i])
      continue;
    for (int j = 3 * i; j < MAX; j += 2 * i)
      sosu[j] = true;
  }
  int n;
  while (cin >> n) {
    int i;
    for (i = n - 1; i >= 2; i--)
      if (sosu[i] == 0)
        break;
    cout << i;
    for (i = n + 1; i < MAX; i++)
      if (sosu[i] == 0)
        break;
    cout << " " << i << endl;
  }
}

Error: WA

Fixed submission:

#include <stdio.h>
int main(void) {

  int i, j;
  int num[50001] = {0, 0, 1};
  int sosu[5000] = {2};
  int count = 1;

  for (i = 3; i < 50001; i += 2) {
    if (num[i] == 1)
      continue;
    for (j = 3 * i; j < 50001; j += i * 2)
      num[j] = 1;
  }
  for (i = 3; i < 50001; i += 2) {
    if (num[i] != 1) {
      sosu[count] = i;
      count++;
    }
  }

  int in;
  while (scanf("%d", &in) != EOF) {
    for (i = 0; i < 50000; i++) {
      if (in < sosu[i])
        break;
    }
    printf("%d %d", sosu[i - 2], sosu[i]);
  }

  return 0;
}