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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int prime[n + 1], i, j;
    for (i = 0; i <= n; i++)
      prime[i] = 1;
    prime[0] = 0;
    prime[1] = 0;
    for (i = 2; i <= sqrt(n); i++) {
      if (prime[i] == 1) {
        for (j = 2; i * j <= n; j++)
          prime[i * j] = 0;
      }
    }
    int cnt = 0;
    for (i = 0; i <= n; i++) {
      if (prime[i] != 0)
        cnt++;
    }
    cout << cnt << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
  int a = 999999;
  bool list[a + 1];
  memset(list, true, sizeof(list));
  list[1] = false;
  for (int i = 4; i <= a; i = i + 2) {
    list[i] = false;
  }
  for (int i = 3; i * i <= a; i = i + 2) {
    if (list[i] == true) {
      for (int k = i * i; k <= a; k = k + i + i) {
        list[k] = false;
      }
    }
  }
  int temp;
  while (scanf("%d", &temp)) {
    int count = 0;
    for (int i = 1; i <= temp; i++) {
      if (list[i] == true) {
        count++;
      }
    }
    printf("%d\\n", count);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
const int N = 1000000;
int main() {
  int n;
  vector<bool> v(N, true);
  v[0] = false;
  v[1] = false;
  for (int i = 2; i < N; ++i) {
    if (v[i] == true) {
      for (int j = 2 * i; j < N; j += i) {
        v[j] = false;
      }
    }
  }
  vector<int> cnt(N);
  for (int i = 2; i < N; ++i) {
    if (v[i])
      cnt[i] = cnt[i - 1] + 1;
    else
      cnt[i] = cnt[i - 1];
  }
  while (cin >> n) {
    cout << cnt[n] << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  while (cin >> n) {
    int cnt = 0;
    for (int i = 2; i <= n; ++i) {
      bool op = false;
      for (int j = 2; j <= sqrt(i); ++j) {
        if (i % j == 0) {
          op = true;
          break;
        }
      }
      if (op == false)
        ++cnt;
    }
    cout << cnt << '\\n';
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
int prime[1000005];
bool is_prime[1000005];
int seve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p;
}
int main(void) {
  int n;
  while (scanf("%d", &n) != EOF) {
    printf("%d\\n", seve(n));
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool IsPrime[1000010]; // 保证不做素数的倍数
int Prim[1000010];     // 存素数
void euler_prime(int n) {
  int num = 0, j;
  for (int i = 2; i <= n; i++) {
    if (!IsPrime[i])   // 不是目前找到的素数的倍数    1不是素数
      Prim[num++] = i; // 记录质数
    for (j = 0; j < num; j++) {
      if (i * Prim[j] > n)
        break;
      IsPrime[i * Prim[j]] = true; // 筛;//找到的素数的倍数不访问
      if (i % Prim[j] == 0)
        break; // i可以看做prime[j]*某个数， i*prime[j+1]就可以看做
               // prime[j]*某个数*prime[j+1] 。而 prime[j] 必定小于
               // prime[j+1]，所以 i*prime[j+1] 必定已经被 prime[j]*某个数
               // 筛掉，就不用再做了（等到i=6即最大合因子时会筛掉）
    }
  }
}

int setans(int x) {
  int cnt = 0;
  for (int i = 2; i <= x; i++) {
    if (!IsPrime[i])
      cnt++;
  }
  return cnt;
}
int main() {
  int n;
  euler_prime(1000005);
  while (scanf("%d", &n))

    printf("%d\\n", setans(n));

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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 999999

using namespace std;

typedef long long ll;

vector<int> primenumber;
int d[1050000];

int main() {
  int n;

  primenumber.push_back(2);
  d[2] = 1;

  int maxn = 0;

  while (cin >> n) {
    if (maxn < n) {
      for (int i = max(maxn + 1, 3); i <= n; i++) {
        int ope = 1;
        for (int j = 0; j < primenumber.size(); j++) {
          if (primenumber[j] * primenumber[j] > i)
            break;
          if (i % primenumber[j] == 0) {
            ope = 0;
            break;
          }
        }
        if (ope == 1)
          primenumber.push_back(i);
        d[i] = primenumber.size();
      }
      maxn = n;
    }
    cout << d[n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 999999

using namespace std;

typedef long long ll;

int main() {
  int n;

  while (cin >> n) {
    int count = 0;
    for (int i = 2; i <= n; i++) {
      int ope = 1;
      for (int j = 2; j * j <= i; j++) {
        if (i % j == 0) {
          ope = 0;
          break;
        }
      }
      count += ope;
    }
    cout << count << endl;
  }

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

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

typedef long long ll;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

ll mod_mul(ll x, ll n, ll mod) {
  x %= mod;
  n %= mod;
  return x * n % mod;
}

bool millrr_rabin(ll n) {
  if (n == 2)
    return true;
  if ((n & 1) == 0 || n <= 1)
    return false;
  int a[] = {2, 3, 5, 7, 11, 13, 17, -1}, r;
  ll s = 0, d, x;
  d = n - 1;
  while ((d & 1) == 0) {
    s++;
    d = d >> 1;
  }

  for (int i = 0; a[i] != -1 && a[i] < n; i++) {
    if ((x = mod_pow(a[i], d, n)) != 1) {
      for (r = 0; r < s; r++) {
        if (x == n - 1)
          break;
        x = mod_mul(x, x, n);
      }
      if (r == s)
        return false;
    }
  }
  return true;
}

int main() {
  int sum[1000010];
  sum[0] = 0;

  for (int i = 1; i <= 1000000; i++) {
    sum[i] = sum[i - 1];
    if (millrr_rabin(i))
      sum[i]++;
  }

  int n;
  while (cin >> n) {
    cout << sum[n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

ll mod_mul(ll x, ll n, ll mod) {
  x %= mod;
  n %= mod;
  return x * n % mod;
}

bool millrr_rabin(ll n) {
  if (n == 2)
    return true;
  if ((n & 1) == 0 || n <= 1)
    return false;
  int a[] = {2, 3, 5, 7, 11, 13, 17, -1}, r;
  ll s = 0, d, x;
  d = n - 1;
  while ((d & 1) == 0) {
    s++;
    d = d >> 1;
  }

  for (int i = 0; a[i] != -1 && a[i] < n; i++) {
    if ((x = mod_pow(a[i], d, n)) != 1) {
      for (r = 0; r < s; r++) {
        if (x == n - 1)
          break;
        x = mod_mul(x, x, n);
      }
      if (r == s)
        return false;
    }
  }
  return true;
}

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

int main() {

  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime(i))
        ans++;
    }
    cout << ans << endl;
  }

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

#include <iostream>
#include <string.h>
using namespace std;
int n, ans;
bool a[1000000];
int main() {
  while (cin >> n) {
    memset(a, true, sizeof(a));
    a[0] = false;
    a[1] = false;
    ans = 0;
    for (int i = 2; i <= n; i++) {
      if (a[i])
        for (int j = 2 * i; j <= n; j += i)
          a[j] = false;
    }
    for (int i = 1; i <= n; i++)
      if (a[i])
        ans++;
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;
int n, ans;
bool a[10005];
int main() {
  while (cin >> n) {
    memset(a, true, sizeof(a));
    a[0] = false;
    a[1] = false;
    ans = 0;
    for (int i = 2; i <= n; i++) {
      if (a[i])
        for (int j = 2 * i; j <= n; j += i)
          a[j] = false;
    }
    for (int i = 1; i <= n; i++)
      if (a[i])
        ans++;
    cout << ans << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    bool prime[1000000] = {false};
    // falseで初期化
    prime[1] = true;
    prime[2] = false;
    int count = 0;

    // エラトステネスのふるい
    for (int j = 2; j * j <= n; j++) {
      if (!prime[j]) {
        for (int k = 2; k * j <= n; k++) {
          prime[j * k] = true;
        }
      }
    }
    // falseだと素数
    for (int i = 1; i <= n; i++) {
      if (!prime[i])
        count = count + 1;
    }
    cout << count << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int is_it_primeNum(int n) {
  int a = 1;
  if (n == 0 || n == 1)
    return 0;
  else if (n == 2)
    return 1;
  else if (n % 2 == 0)
    return 0;
  else {
    for (int i = 3; i < n / 2; i += 2) {
      if (n % i == 0) {
        a = 0;
        break;
      }
    }
  }
  return a;
}
int how_much_primeNum(int n) {
  int ans = 0;
  for (int i = 2; i <= n; i++) {
    ans += is_it_primeNum(i);
  }
  return ans;
}

int main() {
  int n;
  while (cin >> n)
    cout << how_much_primeNum(n) << endl;
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long LL;
using namespace std;
#define REPF(i, a, b) for (int i = a; i <= b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define CLEAR(a, x) memset(a, x, sizeof a)
const int N = 1e7;
int vis[N];
void initPrime() {
  int num = 0, m = sqrt(N + 0.5);
  REPF(i, 1, N) vis[i] = 1;
  for (int i = 2; i <= m; ++i)
    if (vis[i] == 1)
      for (int j = i * i; j <= N; j += i)
        vis[j] = 0;
  vis[1] = 0;
  for (int i = 2; i <= N; i++)
    vis[i] += vis[i - 1];
  //    for(int i=2;i<=10;i++)
  //        cout<<"233  "<<sum[i]<<endl;
}
int main() {
  int n;
  initPrime();
  while (~scanf("%d", &n))
    printf("%d\\n", vis[n]);
  return 0;
}

Error: MLE

Fixed submission:

#pragma warning(disable : 4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#define MAX 10000000
using namespace std;
int amount[MAX];
int vis[MAX];
int prime[MAX];
int main() {
  memset(vis, 0, sizeof(vis));
  memset(prime, 0, sizeof(prime));
  prime[0] = prime[1] = 0;

  for (int i = 2; i <= MAX; i++) {
    if (!vis[i]) {

      prime[i] = 1;
      for (int j = i + i; j <= MAX; j += i) {
        vis[j] = 1;
        prime[j] = 0;
      }
    }
  }
  int sum = 0;
  for (int i = 2; i <= MAX; i++) {
    if (prime[i]) {
      sum++;
    }
    amount[i] = sum;
  }

  int n;
  cin >> n;
  cout << amount[n] << endl;
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int num[1000000];
  for (int i = 0; i < 1000000; i++) {
    num[i] = 1;
  }
  num[0] = 0;
  num[1] = 0;

  for (int i = 2; i < 1000000; i++) {
    for (int j = 2; i * j < 1000000; j++) {
      num[i * j] = 0;
    }
  }

  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = n; i >= 2; i--) {
      if (num[i]) {
        ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int i, j, n;
  int count = 0;

  while (cin >> n) {
    for (i = 2; i <= n; i++) {
      for (j = 2; j < i; j++) {
        if (i % j == 0) {
          break;
        }
      }
      if (i == j) {
        count++;
      }
    }
    cout << count << endl;
    count = 0;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string>
#include <vector>
using namespace std;

int sieve(int x) {
  int ret = 0;
  vector<bool> v(x + 1, true);
  for (int i = 2; i <= x; i++) {
    if (v[i]) {
      ret++;
      for (int j = i + i; j <= x; j += i) {
        v[j] = false;
      }
    }
  }

  return ret;
}

int main() {
  for (int n; cin >> n;) {
    cout << sieve(n) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string>
#include <vector>
using namespace std;

bool is_prime(int x) {
  for (int i = 2; i <= round(sqrt(x)) && i < x; i++)
    if (x % i == 0)
      return false;
  return true;
}

int main() {
  for (int n; cin >> n;) {
    int ans = 0;
    for (int i = 2; i <= n; i++)
      if (is_prime(i))
        ans++;
    cout << ans << endl;
  }

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

/*
题意：给定整数 n， 请问 n 以内有多少个素数？
思路：如果对许多整数进行素性测试，则采用 埃氏筛法 （枚举 n
以内的素数，辗转相除） 埃氏筛法：辗转相除： 首先，将 2 到 n
范围内的所有整数写下来。其中最小的数字 2 是素数。将表中所有 2 的倍数都划去。
                        表中剩余的最小数字是
3，它不能被更小的数整除，所以是素数。再将表中所有 3 的倍数都划去。
                        以此类推，如果表中剩余的最小数字是 m
时，m就是素数。然后将表中所有 m 的倍数都划去。 像这样反复操作，就能依次枚举 n
以内的素数。
*/

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

int prime[MAX_N];     // 第 i 个素数
bool is_prime[MAX_N]; // is_prime[i] 为 true 表示 i 为素数

// 返回 n 以内素数的个数
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p;
}

int main() {
  int s;
  //	while (cin  >>  s)
  while (scanf("%d", &s) != EOF) {
    cout << sieve(s) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

/*
题意：给定整数 n， 请问 n 以内有多少个素数？
思路：如果对许多整数进行素性测试，则采用 埃氏筛法 （枚举 n
以内的素数，辗转相除） 埃氏筛法：辗转相除： 首先，将 2 到 n
范围内的所有整数写下来。其中最小的数字 2 是素数。将表中所有 2 的倍数都划去。
                        表中剩余的最小数字是
3，它不能被更小的数整除，所以是素数。再将表中所有 3 的倍数都划去。
                        以此类推，如果表中剩余的最小数字是 m
时，m就是素数。然后将表中所有 m 的倍数都划去。 像这样反复操作，就能依次枚举 n
以内的素数。
*/

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

int prime[MAX_N];     // 第 i 个素数
bool is_prime[MAX_N]; // is_prime[i] 为 true 表示 i 为素数

// 返回 n 以内素数的个数
int sieve(int n) {
  int p = 0;
  for (int i = 0; i <= n; i++)
    is_prime[i] = true;

  is_prime[0] = is_prime[1] = false;

  for (int i = 2; i <= n; i++) {
    if (is_prime[i]) {
      prime[p++] = i;
      for (int j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p;
}

int main() {
  int s;
  //	while (cin  >>  s)
  while (scanf("%d", &s) != EOF) {
    cout << sieve(s) << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  bool prime[1000000] = {false};
  prime[1] = true;
  prime[2] = false;
  int n;
  while (cin >> n) {
    int cnt = 0;

    for (int j = 2; j * j <= n; j++)
      if (!prime[j])
        for (int k = 2; k * j <= n; k++)
          prime[j * k] = true;

    for (int i = 2; i <= n; i++)
      if (!prime[i])
        cnt++;
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int solve(int num) {
  for (int i = 2; i < num; i++)
    if (num % i == 0)
      return 0;
  return 1;
}

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    for (int i = 2; i <= n; i++)
      cnt += solve(i);
    cout << cnt << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0009
// for ref

#include <cmath>
#include <cstring>
#include <iostream>
#define Rep(i, b, e) for (int i = (int)b; i < (int)e; i++)

using namespace std;

const int MAXN = 999999;
bool isPrime[MAXN];

int countPrime(int n) {
  memset(isPrime, true, sizeof isPrime);
  Rep(i, 0, 1000) isPrime[i] = true;

  isPrime[0] = false;
  isPrime[1] = false;
  // isPrime[2] = true;
  // isPrime[3] = true;

  // Rep(i, 2, sqrt(n)+1) {
  // Rep (k, 2, sqrt(n)+1) {
  Rep(i, 2, n) {
    Rep(k, 2, n) {
      if (i * k > n)
        break;
      isPrime[i * k] = false;
    }
  }

  int primeCount = 0;
  int j = 0;
  // while (primeCount < n) {     // GS q
  while (j <= n) {
    if (isPrime[j]) {
      // cout << primeCount << " th prime\\t";
      // cout << j << endl;
      primeCount++;
    }
    j++;
  }

  return primeCount;
}

int num;

int main() {
  while (cin >> num) {
    cout << countPrime(num) << endl;
  }
  //   cout << "Hello world!" << endl;
  return 0;
}

Error: 0

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0009
// for ref

#include <cstring>
#include <iostream>
#define Rep(i, b, e) for (int i = (int)b; i < (int)e; i++)

using namespace std;

const int MAXN = 999999;
bool isPrime[MAXN];

int countPrime(int n) {
  memset(isPrime, true, sizeof isPrime);
  Rep(i, 0, 1000) isPrime[i] = true;

  isPrime[0] = false;
  isPrime[1] = false;
  // isPrime[2] = true;
  // isPrime[3] = true;

  Rep(i, 2, n) {
    Rep(k, 2, 100) { isPrime[i * k] = false; }
  }

  int primeCount = 0;
  int j = 0;
  // while (primeCount < n) {     // GS q
  while (j <= n) {
    if (isPrime[j]) {
      // cout << primeCount << " th prime\\t";
      // cout << j << endl;
      primeCount++;
    }
    j++;
  }

  return primeCount;
}

int num;

int main() {
  while (cin >> num) {
    cout << countPrime(num) << endl;
  }
  //   cout << "Hello world!" << endl;
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef unsigned long long ull;

ull qtde[1000000] = {0, 0, 1, 2};

bool isPrime(ull x) {
  ull i = 6;
  ull root = (ull)sqrt(x) + 2;
  if (x <= 1)
    return false;
  if (x == 2 || x == 3)
    return true;
  if (x % 2 == 0 || x % 3 == 0)
    return false;

  while (i <= root) {
    if (x % (i - 1) == 0)
      return false;
    if (x % (i + 1) == 0)
      return false;
    i += 6;
  }

  return true;
}

int main(int argc, char const *argv[]) {
  ull n;
  ull i;

  for (int i = 3; i < 1000000; i++)
    qtde[i] = qtde[i - 1] + isPrime(i);

  while (cin >> n)
    cout << qtde[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
using namespace std;
bool isPrime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int res = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime(i)) {
        res++;
      }
    }
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int i, j, n, *prime;
  while (cin >> n) {
    int ans = 0;
    prime = new int[n];
    for (i = 0; i < n; ++i)
      prime[i] = 1;
    prime[0] = 0;
    for (i = 1; i < sqrt(n); ++i) {
      if (prime[i] == 1) {
        for (j = (i + 1); (i + 1) * j <= n; ++j) {
          prime[(i + 1) * j - 1] = 0;
        }
      }
    }
    for (i = 0; i < n; ++i) {
      if (prime[i] == 1)
        ++ans;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

int isprime(int x) {
  int i;
  for (i = 3; i <= sqrt(x); i += 2) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}

int main() {
  int n, i, count;
  while (cin >> n) {
    count = 0;
    for (i = 2; i <= n; i++) {
      if (i == 2 || i == 3)
        count++;
      else if (isprime(i) == 1 && i % 2 != 0)
        count++;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main(void) {
  bool prime[1000000];
  unsigned long n;

  for (unsigned long j = 2; j <= 999999; j++)
    prime[j] = true;
  for (int i = 2; i <= 1000; i++) {
    if (prime[i] == true) {
      for (unsigned long j = i * i; j <= 999999; j += i) {
        prime[j] = false;
      }
    }
  }

  while (cin >> n) {
    unsigned cnt = 0;
    for (unsigned long j = 2; j <= n; j++) {
      if (prime[j] == true) {
        cnt++;
      }
    }

    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int prime[168] = {0};
  unsigned long n;
  prime[0] = 2, prime[1] = 3;

  for (int i = 5; i < 1000; i += 2) {
    bool p_flag = true;
    int j;
    for (j = 0; prime[j] != 0; j++) {
      if (i % prime[j] == 0) {
        p_flag = false;
        break;
      }
    }
    if (p_flag == true) {
      prime[j] = i;
    }
  }

  while (cin >> n) {
    unsigned long cnt = 2;

    if (n == 1) {
      cout << 0 << endl;
      continue;
    } else if (n == 2) {
      cout << 1 << endl;
      continue;
    } else if (n == 3 || n == 4) {
      cout << 2 << endl;
      continue;
    }

    for (unsigned long i = 5; i <= n; i += 2) {
      bool p_flag = true;
      for (int j = 1; prime[j] <= (int)sqrt((double)i) + 1; j++) {
        if (i % prime[j] == 0) {
          p_flag = false;
          break;
        }
      }
      if (p_flag == true) {
        cnt++;
      }
    }

    cout << cnt << endl;
  }

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

#include <algorithm>
#include <iostream>

using namespace std;

#define N 1000000

int main() {

  bool primes[N];
  int n;

  fill(primes, primes + (sizeof primes / sizeof *primes), true);

  primes[0] = false;
  primes[1] = false;

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

  while (cin >> n) {
    int count = 0;
    for (int i = 2; i <= n; ++i) {
      if (primes[i])
        count++;
    }
    cout << count << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define N 100000

int main() {

  bool primes[N];
  int n;

  fill(primes, primes + (sizeof primes / sizeof *primes), true);

  primes[0] = false;
  primes[1] = false;

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

  while (cin >> n) {
    int count = 0;
    for (int i = 2; i <= n; ++i) {
      if (primes[i])
        count++;
    }
    cout << count << endl;
  }

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

#include <iostream>
#include <map>
using namespace std;
#define MAX_N 999999
bool notPrime[MAX_N + 2];
int main() {
  map<int, int> count;
  for (int i = 2; i <= MAX_N; i++) {
    if (!notPrime[i]) {
      for (int g = i + i; g <= MAX_N; g += i) {
        notPrime[g] = true;
      }
    }
  }
  int cnt = 0;
  count[1] = 0;
  for (int g = 2; g <= MAX_N; g++) {
    if (!notPrime[g])
      cnt++;
    count[g] = cnt;
  }
  for (int n; cin >> n; n++) {
    cout << count[n] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
using namespace std;
#define MAX_N 999999
int main() {
  int n;
  map<int, bool> notPrime;
  map<int, int> count;
  for (int i = 2; i <= MAX_N; i++) {
    if (!notPrime[i]) {
      for (int g = i + i; g <= MAX_N; g += i) {
        notPrime[g] = true;
      }
    }
  }
  int cnt = 0;
  count[1] = 0;
  for (int g = 2; g <= MAX_N; g++) {
    if (!notPrime[g])
      cnt++;
    count[g] = cnt;
  }
  while (cin >> n) {
    cout << count[n] << endl;
  }
}