prithivMLmods/Coder-Stat huggingface dataset:
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#define MAX 1000 // 1000010

int main() {
  bool prime[MAX];
  prime[0] = prime[1] = 0;
  for (int i = 0; i < MAX; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;
  for (int i = 2; i < MAX; i++) {
    if (prime[i] == 1) {
      for (int j = 2; i * j < MAX; j++) {
        prime[i * j] = 0;
      }
    }
  }
  int prime_under_num[MAX];
  prime_under_num[0] = 0;
  for (int i = 1; i < MAX; i++) {
    prime_under_num[i] = prime_under_num[i - 1] + prime[i];
  }

  int n;
  while ((scanf("%d", &n)) != EOF) {
    printf("%d\n", prime_under_num[n]);
  }
}

Fixed submission:
#include <stdio.h>
#define MAX 1000010

int main() {
  bool prime[MAX];
  prime[0] = prime[1] = 0;
  for (int i = 0; i < MAX; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;
  for (int i = 2; i < MAX; i++) {
    if (prime[i] == 1) {
      for (int j = 2; i * j < MAX; j++) {
        prime[i * j] = 0;
      }
    }
  }
  int prime_under_num[MAX];
  prime_under_num[0] = 0;
  for (int i = 1; i < MAX; i++) {
    prime_under_num[i] = prime_under_num[i - 1] + prime[i];
  }

  int n;
  while ((scanf("%d", &n)) != EOF) {
    printf("%d\n", prime_under_num[n]);
  }
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int main() {
  int i, k, x, cnt[1000001], data[1000000];
  cnt[0] = 0;
  data[0] = 0;
  data[1] = 0;
  for (i = 2; i <= 1000000; i++) {
    data[i] = 1;
  }
  for (i = 2; i * i <= 1000000; i++) {
    if (data[i]) {
      for (k = 2 * i; k <= 1000000; k += i) {
        data[k] = 0;
      }
    }
  }
  for (i = 0; i <= 1000000; i++) {
    cnt[i + 1] = cnt[i] + data[i];
  }
  while (1) {
    scanf("%d", &x);
    if (x == 0)
      break;
    printf("%d\n", cnt[x]);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int i, k, x, cnt[1000001], data[1000000];
  cnt[0] = 0;
  data[0] = 0;
  data[1] = 0;
  for (i = 2; i <= 1000000; i++) {
    data[i] = 1;
  }
  for (i = 2; i * i <= 1000000; i++) {
    if (data[i]) {
      for (k = 2 * i; k <= 1000000; k += i) {
        data[k] = 0;
      }
    }
  }
  for (i = 0; i <= 1000000; i++) {
    cnt[i + 1] = cnt[i] + data[i];
  }
  while (scanf("%d", &x) != EOF) {
    printf("%d\n", cnt[x + 1]);
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  const int num = 100000;
  vector<int> pnums(num);
  vector<bool> np(num);
  np[0] = np[1] = true;

  for (int i = 2; i < num; ++i) {
    pnums[i] = pnums[i - 1] + !np[i];

    if (!np[i]) {
      for (int j = i; j < num; j += i) {
        np[j] = true;
      }
    }
  }

  for (int n; cin >> n;)
    cout << pnums[n] << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  const int num = 1000000;
  vector<int> pnums(num);
  vector<bool> np(num);
  np[0] = np[1] = true;

  for (int i = 2; i < num; ++i) {
    pnums[i] = pnums[i - 1] + !np[i];

    if (!np[i]) {
      for (int j = i; j < num; j += i) {
        np[j] = true;
      }
    }
  }

  for (int n; cin >> n;)
    cout << pnums[n] << endl;

  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
const int MAX_V = 100;
int prime[MAX_V + 1];
int main() {
  int i, k;
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }

  int right;
  int sum[MAX_V + 1] = {0};
  sum[1] = 0;
  for (i = 1; i < MAX_V + 1; i++) {
    if (prime[i] == 1) {
      sum[i + 1] = sum[i] + 1;
    } else {
      sum[i + 1] = sum[i];
    }
  }
  while (scanf("%d", &right) != EOF) {
    printf("%d\n", sum[right + 1]);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
const int MAX_V = 1000000;
int prime[MAX_V + 1];
int main() {
  int i, k;
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }

  int right;
  int sum[MAX_V + 1] = {0};
  sum[1] = 0;
  for (i = 1; i < MAX_V + 1; i++) {
    if (prime[i] == 1) {
      sum[i + 1] = sum[i] + 1;
    } else {
      sum[i + 1] = sum[i];
    }
  }
  while (scanf("%d", &right) != EOF) {
    printf("%d\n", sum[right + 1]);
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <math.h>

using namespace std;

int prime[10000];
bool is_prime[1000000];

int sosuu(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 = i * 2; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

int main(void) {
  int n;
  while (cin >> n)
    cout << sosuu(n) << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <math.h>

using namespace std;

int prime[100000];
bool is_prime[1000000];

int sosuu(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 = i * 2; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

int main(void) {
  int n;
  while (cin >> n)
    cout << sosuu(n) << endl;
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

#define MAX_N 99999
#define MAX_D 30

bool prime_check(int n, int k = 2) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return prime_check(n, k + 1);
}

int main() {
  int n;

  bool isprime[MAX_N + 1];

  for (int i = 2; i < MAX_N + 1; i++) {
    isprime[i] = prime_check(i);
  }

  while (cin >> n) {
    int c = 0;

    for (int i = 2; i <= n; i++) {
      if (isprime[i] == true) {
        c++;
      }
    }

    cout << c << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

#define MAX_N 999999
#define MAX_D 30

bool prime_check(int n, int k = 2) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return prime_check(n, k + 1);
}

int main() {
  int n;

  bool isprime[MAX_N + 1];

  for (int i = 2; i < MAX_N + 1; i++) {
    isprime[i] = prime_check(i);
  }

  while (cin >> n) {
    int c = 0;

    for (int i = 2; i <= n; i++) {
      if (isprime[i] == true) {
        c++;
      }
    }

    cout << c << endl;
  }

  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    int ans = 0, math[1001000] = {0};
    math[1] = 1;
    for (int i = 2; i < n + 1; i++) {
      int bell = i;
      if (math[i] == 0) {
        ans++;
        math[i] = 1;
      }
      while (bell <= n) {
        bell += i;
        if (math[bell] == 0) {
          math[bell] = 1;
        }
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    int ans = 0, math[1001000] = {0};
    math[1] = 1;
    for (int i = 2; i < n + 1; i++) {
      int bell = i;
      if (math[i] == 0) {
        ans++;
        math[i] = 1;
      }
      while (bell + i <= n) {
        bell += i;
        if (math[bell] == 0) {
          math[bell] = 1;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
#define MAX 100

bool prime[MAX] = {};

void run() {
  for (int i = 2; i < MAX; i++) {
    prime[i] = true;
  }

  for (int i = 2; i < MAX; i++) {
    if (!prime[i])
      continue;
    for (int j = 2; j * i < MAX; j++) { // i*1 is prime number
      prime[i * j] = false;
    }
  }
  return;
}

int main() {

  run();

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

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
#define MAX 1000000

bool prime[MAX] = {};

void run() {
  for (int i = 2; i < MAX; i++) {
    prime[i] = true;
  }

  for (int i = 2; i < MAX; i++) {
    if (!prime[i])
      continue;
    for (int j = 2; j * i < MAX; j++) { // i*1 is prime number
      prime[i * j] = false;
    }
  }
  return;
}

int main() {

  run();

  int n;
  while (cin >> n) {
    int sum = 0;
    for (int i = 2; i <= n; i++) {
      if (prime[i])
        sum++;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
using namespace std;
int isPrime[1000001];
int main() {

  fill(isPrime, isPrime + 1000001, 1);
  isPrime[0] = isPrime[1] = 0;
  for (int i = 2; i * i < 1000001; i++) {
    if (isPrime[i])
      for (int j = i * 2; j < 1000001; j += i)
        isPrime[j] = 0;
  }
  int n;
  while (cin >> n, n) {
    int cnt = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime[i])
        cnt++;
    }
    cout << cnt << endl;
  }
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
int isPrime[1000001];
int main() {

  fill(isPrime, isPrime + 1000001, 1);
  isPrime[0] = isPrime[1] = 0;
  for (int i = 2; i * i < 1000001; i++) {
    if (isPrime[i])
      for (int j = i * 2; j < 1000001; j += i)
        isPrime[j] = 0;
  }
  int n;
  while (cin >> n) {
    int cnt = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime[i])
        cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

static const int MAX = 10;

int main() {
  static bool is_prime[MAX] = {false};
  static int npr[MAX];
  for (int i = 2; i < MAX; i++) {
    if (!is_prime[i]) {
      for (int j = 2; j * i < MAX; j++) {
        is_prime[i * j] = true;
      }
    }
    npr[i] = npr[i - 1] + !is_prime[i];
  }
  int n;
  while (cin >> n) {
    cout << npr[n] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

static const int MAX = 1000000;

int main() {
  static bool is_prime[MAX] = {false};
  static int npr[MAX];
  for (int i = 2; i < MAX; i++) {
    if (!is_prime[i]) {
      for (int j = 2; j * i < MAX; j++) {
        is_prime[i * j] = true;
      }
    }
    npr[i] = npr[i - 1] + !is_prime[i];
  }
  int n;
  while (cin >> n) {
    cout << npr[n] << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define LIM 999999
using namespace std;
long long int i, j, a, odd;
bool prime[LIM];
long long int arr[999999];
void sieve() {

  arr[a++] = 2;
  for (i = 3; i <= LIM; i += 2) {
    if (!prime[i]) {
      arr[a++] = i;
      if (i * i <= LIM) {
        odd = i;

        for (j = i * i; j <= LIM; j = i * odd) {
          prime[j] = true;
          odd += 2;
        }
      }
    }
    if (a == 999999)
      break;
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  sieve();
  long long int k, num, count = 0;
  k = 0;
  while (scanf("%lld", &num) != EOF) {
    k = 0;
    count = 0;

    for (int i = 1; i <= num; i++) {
      if (i == arr[k]) {
        count++;
        k++;
      }
    }
    printf("%lld\n", count);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define LIM 999999
using namespace std;
long long int i, j, a, odd;
bool prime[LIM];
long long int arr[999999];
void sieve() {

  arr[a++] = 2;
  for (i = 3; i <= LIM; i += 2) {
    if (!prime[i]) {
      arr[a++] = i;
      if (i * i <= LIM) {
        odd = i;

        for (j = i * i; j <= LIM; j = i * odd) {
          prime[j] = true;
          odd += 2;
        }
      }
    }
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  sieve();
  long long int k, num, count = 0;
  k = 0;
  while (scanf("%lld", &num) != EOF) {
    k = 0;
    count = 0;

    for (int i = 1; i <= num; i++) {
      if (i == arr[k]) {
        count++;
        k++;
      }
    }
    printf("%lld\n", count);
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) REP(i, 0, n)
#define mset(a, n) memset(a, n, sizeof(a))

using namespace std;

const int MAX_N = 100000000;
bool prime[MAX_N];
void eratos() {
  rep(i, MAX_N) prime[i] = true;
  prime[0] = false;
  prime[1] = false;
  REP(i, 2, sqrt(MAX_N)) {
    if (prime[i]) {
      for (int j = 0; i * (j + 2) < MAX_N; ++j) {
        prime[i * (j + 2)] = false;
      }
    }
  }
}

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

Fixed submission:
#include <bits/stdc++.h>

#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) REP(i, 0, n)
#define mset(a, n) memset(a, n, sizeof(a))

using namespace std;

const int MAX_N = 1000000;
bool prime[MAX_N];
void eratos() {
  rep(i, MAX_N) prime[i] = true;
  prime[0] = false;
  prime[1] = false;
  REP(i, 2, sqrt(MAX_N)) {
    if (prime[i]) {
      for (int j = 0; i * (j + 2) < MAX_N; ++j) {
        prime[i * (j + 2)] = false;
      }
    }
  }
}

int main() {
  eratos();
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 2; i <= n; ++i) {
      if (prime[i])
        ans++;
    }
    cout << ans << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>

const int N = 19;
int dp[N];
int isprime[N];

int main() {
  dp[1] = 0;
  for (int i = 2; i < N; i++) {
    if (!isprime[i])
      for (int j = 2 * i; j < N; j += i)
        isprime[j] = 1;
    dp[i] = dp[i - 1] + ((isprime[i]) ? 0 : 1);
  }

  int y;
  while (~scanf("%d", &y))
    printf("%d\n", dp[y]);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>

const int N = 1000009;
int dp[N];
int isprime[N];

int main() {
  dp[1] = 0;
  for (int i = 2; i < N; i++) {
    if (!isprime[i])
      for (int j = 2 * i; j < N; j += i)
        isprime[j] = 1;
    dp[i] = dp[i - 1] + ((isprime[i]) ? 0 : 1);
  }

  int y;
  while (~scanf("%d", &y))
    printf("%d\n", dp[y]);
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
std::vector<int> PRIMES;

void init_primes(int max) {
  if (max < 2)
    return;
  std::set<int> candidate;
  PRIMES.push_back(2);
  for (int i = 3; i < max; i += 2) {
    candidate.insert(i);
  }
  for (int i = 3; i < sqrt(max); i += 2) {
    std::set<int>::iterator it = candidate.find(i);
    if (it != candidate.end()) {
      PRIMES.push_back(i);
      int del_num = i;
      while (del_num < max) {
        it = candidate.find(del_num);
        if (it != candidate.end())
          candidate.erase(it);
        del_num += i;
      }
    }
  }
  for (std::set<int>::iterator it = candidate.begin(); it != candidate.end();
       ++it) {
    PRIMES.push_back(*it);
  }
}

int main() {
  const int max = 1000000;
  init_primes(max);
  int i;
  while (std::cin >> i) {
    std::vector<int>::iterator it = PRIMES.begin();
    int n = 0;
    while (*it <= i) {
      n++;
      it++;
    }
    std::cout << n << std::endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
std::vector<int> PRIMES;

void init_primes(int max) {
  if (max < 2)
    return;
  std::set<int> candidate;
  PRIMES.push_back(2);
  for (int i = 3; i < max; i += 2) {
    candidate.insert(i);
  }
  for (int i = 3; i < sqrt(max); i += 2) {
    std::set<int>::iterator it = candidate.find(i);
    if (it != candidate.end()) {
      PRIMES.push_back(i);
      int del_num = i;
      while (del_num < max) {
        it = candidate.find(del_num);
        if (it != candidate.end())
          candidate.erase(it);
        del_num += i;
      }
    }
  }
  for (std::set<int>::iterator it = candidate.begin(); it != candidate.end();
       ++it) {
    PRIMES.push_back(*it);
  }
}

int main() {
  const int max = 1000000;
  init_primes(max);
  int i;
  while (std::cin >> i) {
    std::vector<int>::iterator it = PRIMES.begin();
    int n = 0;
    while (*it <= i && it != PRIMES.end()) {
      n++;
      it++;
    }
    std::cout << n << std::endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

int sosuu(int n);

int main() {
  int n = 0;
  int num = 0;
  int a[30][2] = {0};
  int max = 0;
  while (scanf("%d", &n) != EOF) {
    if (max < n) {
      max = n;
    }
    a[num][0] = n;
    num++;
  }

  int b[max];
  int c[max];
  int so = 0;
  for (int i = 0; i <= max; i++) {
    b[i] = i;
  }
  b[1] = 0;

  for (int i = 2; i < sqrt(max); i++) {
    if (b[i] = !0) {
      for (int j = i + 1; j <= max; j++) {
        if (j % i == 0) {
          b[j] = 0;
        }
      }
    }
  }

  for (int i = 0; i <= max; i++) {
    if (b[i] != 0) {
      c[so] = i;
      so++;
    }
  }

  //    for(int j=0; j<num; j++){
  //        cout<<a[j][0]<<"\n";
  //    }
  //
  //    for(int j=0; j<=so; j++){
  //        cout<<c[j]<<"\n";
  //    }

  for (int i = 0; i < num; i++) {
    for (int j = 0; j < so; j++) {
      if (c[j] <= a[i][0]) {
        a[i][1]++;
      } else {
        j = so;
      }
    }
  }

  for (int j = 0; j < num; j++) {
    cout << a[j][1] << "\n";
  }
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

int sosuu(int n);

int main() {
  int n = 0;
  int num = 0;
  int a[30][2] = {0};
  int max = 0;
  while (scanf("%d", &n) != EOF) {
    if (max < n) {
      max = n;
    }
    a[num][0] = n;
    num++;
  }

  int b[max];
  int c[max];
  int so = 0;
  for (int i = 0; i <= max; i++) {
    b[i] = i;
  }
  b[1] = 0;

  for (int i = 2; i < sqrt(max); i++) {
    if (b[i] != 0) {
      for (int j = i + 1; j <= max; j++) {
        if (j % i == 0) {
          b[j] = 0;
        }
      }
    }
  }

  for (int i = 0; i <= max; i++) {
    if (b[i] != 0) {
      c[so] = i;
      so++;
    }
  }

  //    for(int j=0; j<num; j++){
  //        cout<<a[j][0]<<"\n";
  //    }
  //
  //    for(int j=0; j<=so; j++){
  //        cout<<c[j]<<"\n";
  //    }

  for (int i = 0; i < num; i++) {
    for (int j = 0; j < so; j++) {
      if (c[j] <= a[i][0]) {
        a[i][1]++;
      } else {
        j = so;
      }
    }
  }

  for (int j = 0; j < num; j++) {
    cout << a[j][1] << "\n";
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 2; i <= n; i++)

using namespace std;

int main(void) {
  for (int n, count = 0; cin >> n; cout << count << endl, count = 0) {
    vector<bool> numberList(n + 1);

    rep(i, n) numberList[i] = true;

    rep(i, n) for (int j = i; i * j <= n; j++) numberList[i * j] =
        !(numberList[i * j]);

    rep(i, n) if (numberList[i]) count++;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 2; i <= n; i++)

using namespace std;

int main(void) {
  for (int n, count = 0; cin >> n; cout << count << endl, count = 0) {
    vector<bool> numberList(n + 1);

    rep(i, n) numberList[i] = true;

    for (int i = 2; i * i <= n; i++)
      for (int j = i; i * j <= n; j++)
        if (numberList[i * j])
          numberList[i * j] = false;

    rep(i, n) if (numberList[i]) count++;
  }

  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#define N 1000000

int main() {
  int ipt;
  int root;
  int cnt = 0;
  int A[N];
  for (int i = 1; i <= N; i++) {
    A[i] = i;
  }
  for (int pos = 2; pos * pos <= N; pos++) {
    root = A[pos];
    if (!root)
      continue;
    for (int k = 2; k * root <= N; k++) {
      A[k * root] = 0;
    }
  }

  while (1) {
    scanf("%d", &ipt);
    for (int i = 2; i <= ipt; i++) {
      if (A[i] != 0)
        cnt++;
    }
    printf("%d\n", cnt);
    cnt = 0;
  }

  return 0;
}


Fixed submission:
#include <stdio.h>
#define N 1000000

int main() {
  int ipt;
  int root;
  int cnt = 0;
  int A[N];
  for (int i = 1; i <= N; i++) {
    A[i] = i;
  }
  for (int pos = 2; pos * pos <= N; pos++) {
    root = A[pos];
    if (!root)
      continue;
    for (int k = 2; k * root <= N; k++) {
      A[k * root] = 0;
    }
  }

  while (scanf("%d", &ipt) == 1) {
    for (int i = 2; i <= ipt; i++) {
      if (A[i] != 0)
        cnt++;
    }
    printf("%d\n", cnt);
    cnt = 0;
  }

  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

int prime(int n) {
  int i, j, r;
  n++;
  vector<int> primes(n);

  for (i = 2; i < n; ++i)
    primes[i] = i;
  for (i = 2; i * i < n; ++i) {
    if (primes[i])
      for (j = i * i; j < n; j += i)
        primes[j] = 0;
  }
  for (i = r = 0; i < n; i++)
    if (primes[i])
      r++;

  for (i = r; i < n; i++)
    cout << primes[i] << " ";
  cout << endl;
  return r;
}

int main() {
  int n;
  while (cin >> n)
    cout << prime(n) << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int prime(int n) {
  int i, j, r;
  n++;
  vector<int> primes(n);

  for (i = 2; i < n; ++i)
    primes[i] = i;
  for (i = 2; i * i < n; ++i) {
    if (primes[i])
      for (j = i * i; j < n; j += i)
        primes[j] = 0;
  }
  for (i = r = 0; i < n; i++)
    if (primes[i])
      r++;
  return r;
}

int main() {
  int n;
  while (cin >> n)
    cout << prime(n) << endl;
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>

using namespace std;
typedef long long Int;
#define REP(i, x) for (int i = 0; i < x; ++i)

typedef pair<int, int> P;

bool dp[10000100];

int main() {
  int N;
  while (cin >> N) {
    long ans(0);
    dp[0] = dp[1] = true;
    for (int i = 2; i <= N * N; ++i) {
      if (!dp[i]) {
        for (int j = 2 * i; j <= N; j += i)
          dp[j] = true;
      }
    }
    for (int i = 2; i <= N; ++i) {
      ans += !dp[i];
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>

using namespace std;
typedef long long Int;
#define REP(i, x) for (int i = 0; i < x; ++i)

typedef pair<int, int> P;

bool dp[10000100];

int main() {
  int N;
  while (cin >> N) {
    long ans(0);
    dp[0] = dp[1] = true;
    for (int i = 2; i * i <= N; ++i) {
      if (!dp[i]) {
        for (int j = 2 * i; j <= N; j += i)
          dp[j] = true;
      }
    }
    for (int i = 2; i <= N; ++i) {
      ans += !dp[i];
    }
    cout << ans << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

vi prime_count;
void sieve(int max) {
  int count = 0;
  vector<bool> table(max + 1, true);
  table[0] = false;
  prime_count.push_back(0);
  table[1] = false;
  prime_count.push_back(0);
  for (int i = 2; i <= max; i++) {
    if (table[i]) {
      count++;
      for (int j = 2; i * j <= max; j++) {
        table[i * j] = false;
      }
    }
    prime_count.push_back(count);
  }
}

int main() {
  int n;
  sieve(10000);
  while (cin >> n) {
    cout << prime_count[n] << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

vi prime_count;
void sieve(int max) {
  int count = 0;
  vector<bool> table(max + 1, true);
  table[0] = false;
  prime_count.push_back(0);
  table[1] = false;
  prime_count.push_back(0);
  for (int i = 2; i <= max; i++) {
    if (table[i]) {
      count++;
      for (int j = 2; i * j <= max; j++) {
        table[i * j] = false;
      }
    }
    prime_count.push_back(count);
  }
}

int main() {
  int n;
  sieve(1000000);
  while (cin >> n) {
    cout << prime_count[n] << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):

#include <iostream>
using namespace std;
#define MAX_N 999999 + 16

int prime[MAX_N];
bool is_prime[MAX_N + 1];
int sieve(int n) {
  int p = 0;
  fill(is_prime, is_prime + n + 1, 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 = i * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return p;
}
int main() {

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

Fixed submission:

#include <iostream>
using namespace std;
#define MAX_N 999999 + 16

int prime[MAX_N];
bool is_prime[MAX_N + 1];
int sieve(int n) {
  int p = 0;
  fill(is_prime, is_prime + n + 1, 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 n;
  while (cin >> n) {
    cout << sieve(n) << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#define MAX_N 1000000

int main() {
  bool p[MAX_N + 1]; // p[NUM] : NUM is Prime?
  int n;

  for (int i = 0; i <= MAX_N; i++)
    p[i] = true;

  for (int i = 1; i <= MAX_N; i++) {
    if (i == 1)
      p[i] = false;
    else {
      if (p[i]) {
        for (int j = 2; i * j <= MAX_N; j++) {
          p[i * j] = false;
        }
      }
    }
  }

  // for(int i = 0; i <= MAX_N; i++) if(p[i]) printf("%d\n",i);

  while (scanf("%d", &n)) {
    int count = 0;
    for (int i = 1; i <= n; i++) {
      if (p[i])
        count++;
    }
    printf("%d\n", count);
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#define MAX_N 1000000

int main() {
  bool p[MAX_N + 1]; // p[NUM] : NUM is Prime?
  int n;

  for (int i = 0; i <= MAX_N; i++)
    p[i] = true;

  for (int i = 1; i <= MAX_N; i++) {
    if (i == 1)
      p[i] = false;
    else {
      if (p[i]) {
        for (int j = 2; i * j <= MAX_N; j++) {
          p[i * j] = false;
        }
      }
    }
  }

  // for(int i = 0; i <= MAX_N; i++) if(p[i]) printf("%d\n",i);

  while (scanf("%d", &n) == 1) {
    int count = 0;
    for (int i = 1; i <= n; i++) {
      if (p[i])
        count++;
    }
    printf("%d\n", count);
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false

int main() {
  vector<int> prime;
  prime.push_back(2);
  for (long long int i = 3; i < 999999; i++) {
    double temp = sqrt(i);
    for (int j = 0;; j++) {
      if (temp < prime[j]) {
        prime.push_back(i);
        break;
      }
      if (i % prime[j] == 0) {
        break; // this is not prime
      }
    }
  }
  int n;
  int i;
  while (cin >> n) {
    for (i = 0; prime[i] <= n; i++)
      ;
    cout << i << endl;
  }
  return 0;
}

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

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false

int main() {
  vector<int> prime;
  prime.push_back(2);
  for (long long int i = 3; i < 999999; i++) {
    double temp = sqrt(i);
    for (int j = 0;; j++) {
      if (temp < prime[j]) {
        prime.push_back(i);
        break;
      }
      if (i % prime[j] == 0) {
        break; // this is not prime
      }
    }
  }
  int n;
  int i;
  while (cin >> n) {
    for (i = 0; i < prime.size() && prime[i] <= n; i++)
      ;
    cout << i << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int const M = 40;
int a, hantei[M];

int main() {
  hantei[0] = hantei[1] = 1;
  for (int i = 2; i < M; i++) {
    if (hantei[i] == 0) {
      for (int j = 2; j * i < M; j++) {
        if (j * i < M) {
          hantei[j * i] = 1;
        }
      }
    }
  }
  for (; cin >> a;) {
    int count = 0;
    for (int i = 0; i <= a; i++) {
      if (hantei[i] == 0) {
        count++;
      }
    }
    cout << count << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int const M = 1000010;
int a, hantei[M];

int main() {
  hantei[0] = hantei[1] = 1;
  for (int i = 2; i < M; i++) {
    if (hantei[i] == 0) {
      for (int j = 2; j * i < M; j++) {
        if (j * i < M) {
          hantei[j * i] = 1;
        }
      }
    }
  }
  for (; cin >> a;) {
    int count = 0;
    for (int i = 0; i <= a; i++) {
      if (hantei[i] == 0) {
        count++;
      }
    }
    cout << count << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    vector<bool> v(n + 1, true);
    for (int i = 2; i <= sqrt(n); i++) {
      if (v[i]) {
        for (int j = i + 1; j <= n; j++) {
          if (j % i == 0)
            v[j] = false;
        }
      }
    }
    for (int i = 2; i <= n; i++) {
      if (v[i] == true)
        cnt++;
    }
    cout << cnt << endl;
  }
}

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

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    vector<bool> v(n + 1, true);
    for (int i = 2; i <= sqrt(n); i++) {
      if (v[i]) {
        for (int j = 2; i * j <= n; j++) {
          v[i * j] = false;
        }
      }
    }
    for (int i = 2; i <= n; i++) {
      if (v[i] == true)
        cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>

typedef long long LL;
using namespace std;

const int X = 1000001;
bool Find[X];

void init() {
  for (int i = 0; i < X; i++) {
    Find[i] = false;
  }
  for (int i = 2; i <= sqrt(X); i++) {
    if (Find[i])
      continue;
    for (int j = i + 1; j < X; j++) {
      if (Find[j])
        continue;
      if (j % i == 0)
        Find[j] = true;
    }
  }
}

int main() {
  init();
  int n;
  while (cin >> n) {
    int answer = 0;
    for (int i = 2; i < n + 1; i++) {
      cout << Find[i] << endl;
      if (!Find[i])
        answer++;
    }
    cout << answer << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>

typedef long long LL;
using namespace std;

const int X = 1000001;
bool Find[X];

void init() {
  for (int i = 0; i < X; i++) {
    Find[i] = false;
  }
  for (int i = 2; i <= sqrt(X); i++) {
    if (Find[i])
      continue;
    for (int j = i + 1; j < X; j++) {
      if (Find[j])
        continue;
      if (j % i == 0)
        Find[j] = true;
    }
  }
}

int main() {
  init();
  int n;
  while (cin >> n) {
    int answer = 0;
    for (int i = 2; i < n + 1; i++) {
      if (!Find[i])
        answer++;
    }
    cout << answer << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int sum[1000010];
void init() {
  for (int i = 2; i <= 1000000; i++) {
    sum[i] = sum[i - 1] + 1;
    for (int j = 2; j < i; j++) {
      if (i % j == 0) {
        sum[i]--;
        break;
      }
    }
  }
}
int main() {
  init();
  int n;
  while (cin >> n && n) {
    cout << sum[n] << endl;
  }
}


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

int sum[1000010];
void init() {
  for (int i = 2; i <= 1000000; i++) {
    sum[i] = sum[i - 1] + 1;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        sum[i]--;
        break;
      }
    }
  }
}
int main() {
  init();
  int n;
  while (cin >> n && n) {
    cout << sum[n] << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

const int MAX_V = 10000;
int prime[MAX_V + 1];

void init() {
  int i, k, v;
  for (i = 2; i < MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }
}

int isprime(int n) { return prime[n]; }

int main() {
  init();
  int num;
  while (scanf("%d", &num) != EOF) {
    int count = 0;
    for (int i = 2; i <= num; i++) {
      if (isprime(i) == 1)
        count++;
    }
    printf("%d\n", count);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

const int MAX_V = 1000000;
int prime[MAX_V + 1];

void init() {
  int i, k, v;
  for (i = 2; i < MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }
}

int isprime(int n) { return prime[n]; }

int main() {
  init();
  int num;
  while (scanf("%d", &num) != EOF) {
    int count = 0;
    for (int i = 2; i <= num; i++) {
      if (isprime(i) == 1)
        count++;
    }
    printf("%d\n", count);
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>

int main() {
  int n;
  int a[1000];
  int b;
  int i, j, p;
  int c[30];
  p = 0;
  while (scanf("%d", &n) != EOF) {
    b = (int)pow((double)n, 0.5);
    c[p] = 0;
    a[0] = -1;
    for (i = 2; i <= b; i++) {
      for (j = 2; j <= n / i; j++) {
        a[i * j - 1] = -1;
      }
    }
    for (i = 1; i <= n; i++) {
      if (a[i - 1] != -1) {
        c[p]++;
      }
      a[i - 1] = 0;
    }
    p++;
  }
  for (i = 0; i < p; i++) {
    printf("%d\n", c[i]);
  }
  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>

int main() {
  int n;
  int a[1000000];
  int b;
  int i, j, p;
  int c[30];
  p = 0;
  while (scanf("%d", &n) != EOF) {
    b = (int)pow((double)n, 0.5);
    c[p] = 0;
    a[0] = -1;
    for (i = 2; i <= b; i++) {
      for (j = 2; j <= n / i; j++) {
        a[i * j - 1] = -1;
      }
    }
    for (i = 1; i <= n; i++) {
      if (a[i - 1] != -1) {
        c[p]++;
      }
      a[i - 1] = 0;
    }
    p++;
  }
  for (i = 0; i < p; i++) {
    printf("%d\n", c[i]);
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <map>
#include <string>
using namespace std;

bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}

int main() {
  int n;
  int *table = new int[1000000];
  while (~scanf("%d", &n)) {

    fill((int *)table, (int *)table + n + 1, 1);
    table[0] = 0;
    table[1] = 0;

    for (int i = 2; i <= n; i++) {
      if (table[i] && !is_prime(i)) {
        for (int j = i; j <= n; j += i) {
          table[j] = 0;
        }
      }
    }
    printf("%d\n", count(table, table + n + 1, 1));
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <map>
#include <string>
using namespace std;

bool is_prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}

int main() {
  int n;
  int *table = new int[1000000];
  while (~scanf("%d", &n)) {

    fill((int *)table, (int *)table + n + 1, 1);
    table[0] = 0;
    table[1] = 0;

    for (int i = 2; i <= n; i++) {
      if (table[i]) {
        for (int j = i + i; j <= n; j += i) {
          table[j] = 0;
        }
      }
    }
    printf("%d\n", count(table, table + n + 1, 1));
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#define MAX 100001
using namespace std;

int p[MAX];

int main() {
  int counter = 0;
  p[1] = 0;
  for (int i = 2; i < MAX; i++) {
    int sqrti = (int)sqrt(i);
    counter++;
    p[i] = counter;
    for (int j = 2; j <= sqrti; j++) {
      if (i % j == 0) {
        counter--;
        p[i] = counter;
        break;
      }
    }
  }
  int n;
  while (cin >> n) {
    cout << p[n] << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#define MAX 1000001
using namespace std;

int p[MAX];

int main() {
  int counter = 0;
  p[1] = 0;
  for (int i = 2; i < MAX; i++) {
    int sqrti = (int)sqrt(i);
    counter++;
    p[i] = counter;
    for (int j = 2; j <= sqrti; j++) {
      if (i % j == 0) {
        counter--;
        p[i] = counter;
        break;
      }
    }
  }
  int n;
  while (cin >> n) {
    cout << p[n] << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

const int C = (int)1e8;

bool isprime[C];
int prime[C];

void init() {
  fill(isprime, isprime + C, true);
  isprime[0] = isprime[1] = false;
  for (auto i = 2; i < C; i++) {
    if (isprime[i]) {
      for (auto j = 2; i * j < C; j++) {
        isprime[i * j] = false;
      }
    }
  }
  prime[0] = 0;
  for (auto i = 1; i < C; i++) {
    prime[i] = ((isprime[i]) ? prime[i - 1] + 1 : prime[i - 1]);
  }
}

int main() {
  init();
  int n;
  while (cin >> n) {
    cout << prime[n] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

const int C = (int)1e7 + 10;

bool isprime[C];
int prime[C];

void init() {
  fill(isprime, isprime + C, true);
  isprime[0] = isprime[1] = false;
  for (auto i = 2; i < C; i++) {
    if (isprime[i]) {
      for (auto j = 2; i * j < C; j++) {
        isprime[i * j] = false;
      }
    }
  }
  prime[0] = 0;
  for (auto i = 1; i < C; i++) {
    prime[i] = ((isprime[i]) ? prime[i - 1] + 1 : prime[i - 1]);
  }
}

int main() {
  init();
  int n;
  while (cin >> n) {
    cout << prime[n] << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  long long int n;
  while (cin >> n) {
    int m = 0;
    long long int prime[200000];
    bool is_prime[200000];
    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]) {
        m++;
        for (int j = i * 2; j <= n; j += i) {
          is_prime[j] = false;
        }
      }
    }
    cout << m << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  long long int n;
  while (cin >> n) {
    int m = 0;
    long long int prime[2000000];
    bool is_prime[2000000];
    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]) {
        m++;
        for (int j = i * 2; j <= n; j += i) {
          is_prime[j] = false;
        }
      }
    }
    cout << m << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#define MAX 100000
using namespace std;
int main() {
  bool p[MAX + 1];
  int n, count;
  for (int i = 2; i <= MAX; i++)
    p[i] = true;
  for (int i = 2; i <= sqrt(MAX); i++) {
    if (p[i] == true) {
      for (int j = 2 * i; j <= MAX; j++) {
        if (j % i == 0)
          p[j] = false;
      }
    }
  }
  while (cin >> n) {
    count = 0;
    for (int i = 0; i <= n; i++) {
      if (p[i] == true)
        count++;
    }
    cout << count << endl;
  }
}

Fixed submission:
#include <iostream>
#include <math.h>
#define MAX 1000000
using namespace std;
int main() {
  bool p[MAX + 1];
  int n, count;
  for (int i = 2; i <= MAX; i++)
    p[i] = true;
  for (int i = 2; i <= sqrt(MAX); i++) {
    if (p[i] == true) {
      for (int j = 2 * i; j <= MAX; j++) {
        if (j % i == 0)
          p[j] = false;
      }
    }
  }
  while (cin >> n) {
    count = 0;
    for (int i = 0; i <= n; i++) {
      if (p[i] == true)
        count++;
    }
    cout << count << endl;
  }
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <math.h>
#include <stdio.h>

int sos(int);

int pri[10000000];

int main() {
  int x;
  while (scanf("%d", &x) != EOF) {
    printf("%d\n", sos(x));
  }
  return 0;
}

int sos(int a) {
  int i, j, c = 0, b;
  for (j = 2; j <= a; j++) {
    b = 0;
    if (pri[j] == 3)
      continue;
    for (i = 2; i <= sqrt(j); i++) {
      if (j % i == 0) {
        pri[j] = 3;
        b = 1;
        break;
      }
    }
    if (b == 0) {
      pri[j] = 1;
      c++;
    }
  }
  return c;
}

Fixed submission:
#include <math.h>
#include <stdio.h>

int sos(int);

int pri[10000000];

int main() {
  int x;
  while (scanf("%d", &x) != EOF) {
    printf("%d\n", sos(x));
  }
  return 0;
}

int sos(int a) {
  int i, j, c = 0, b;
  for (j = 2; j <= a; j++) {
    b = 0;
    if (pri[j] == 3)
      continue;
    if (pri[j] == 1) {
      c++;
      continue;
    }
    for (i = 2; i <= sqrt(j); i++) {
      if (j % i == 0) {
        pri[j] = 3;
        b = 1;
        break;
      }
    }
    if (b == 0) {
      pri[j] = 1;
      c++;
    }
  }
  return c;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;

// エラストテネスの篩, o(n)程度
// n以下の素数の数を返す, prime[i]: i番目の素数, is_prime[i]: iが素数であるか
int sieve(int n, int prime[] = nullptr, bool is_prime[] = nullptr) {
  bool flag0 = false, flag1 = false;
  if (prime == nullptr) {
    flag0 = true;
    prime = new int[n];
  }
  if (is_prime == nullptr) {
    flag1 = true;
    is_prime = new bool[n + 1];
  }

  int p = 0;
  fill(is_prime, is_prime + n + 1, 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;
    }
  }

  if (flag0)
    delete prime;
  if (flag1)
    delete is_prime;

  return p;
}

const int MAX_N = (int)1e6;
int prime[MAX_N];
bool is_prime[MAX_N + 1];

int main(void) {
  int num = sieve(MAX_N, prime, is_prime);

  int n;
  while (scanf("%d", &n)) {
    if (n == EOF)
      break;
    cout << upper_bound(prime, prime + num, n) - prime << '\n';
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;

// エラストテネスの篩, o(n)程度
// n以下の素数の数を返す, prime[i]: i番目の素数, is_prime[i]: iが素数であるか
int sieve(int n, int prime[] = nullptr, bool is_prime[] = nullptr) {
  bool flag0 = false, flag1 = false;
  if (prime == nullptr) {
    flag0 = true;
    prime = new int[n];
  }
  if (is_prime == nullptr) {
    flag1 = true;
    is_prime = new bool[n + 1];
  }

  int p = 0;
  fill(is_prime, is_prime + n + 1, 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;
    }
  }

  if (flag0)
    delete prime;
  if (flag1)
    delete is_prime;

  return p;
}

const int MAX_N = (int)1e6;
int prime[MAX_N];
bool is_prime[MAX_N + 1];

int main(void) {
  int num = sieve(MAX_N, prime, is_prime);

  int n;
  while (scanf("%d", &n) != EOF) {
    cout << upper_bound(prime, prime + num, n) - prime << '\n';
  }

  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

#define N 100000

using namespace std;

int main() {
  int n;
  bool p[N];
  for (int i = 0; i <= N; i++)
    p[i] = 0;
  for (int i = 3; i <= N; i += 2)
    p[i] = 1;
  p[2] = 1;
  for (int i = 0; i < N; i++) {
    if (!p[i])
      continue;
    for (int j = 2 * i; j < N; j += i)
      p[j] = 0;
  }

  while (cin >> n) {
    int j = 0;
    for (int i = 0; i <= n; i++) {
      if (p[i])
        j++;
    }
    cout << j << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

#define N 1000000

using namespace std;

int main() {
  int n;
  bool p[N];
  for (int i = 0; i <= N; i++)
    p[i] = 0;
  for (int i = 3; i <= N; i += 2)
    p[i] = 1;
  p[2] = 1;
  for (int i = 0; i < N; i++) {
    if (!p[i])
      continue;
    for (int j = 2 * i; j < N; j += i)
      p[j] = 0;
  }

  while (cin >> n) {
    int j = 0;
    for (int i = 0; i <= n; i++) {
      if (p[i])
        j++;
    }
    cout << j << endl;
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
bool prime[1000000];
#define max 1000000
int main() {
  prime[1] = true;
  for (int i = 2; i < max; i++) {
    if (!prime[i]) {
      for (int j = i * i; j < max; j += i) {
        prime[j] = true;
      }
    }
  }
  int n;
  while (scanf("%d", &n) != EOF) {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (!prime[i])
        ans++;
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
bool prime[1000000];
#define max 1000000
int main() {
  prime[1] = true;
  for (int i = 2; i * i < max; i++) {
    if (!prime[i]) {
      for (int j = i * i; j < max; j += i) {
        prime[j] = true;
      }
    }
  }
  int n;
  while (scanf("%d", &n) != EOF) {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (!prime[i])
        ans++;
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
const int MAX_V = 999999;
int main() {
  int prime[MAX_V + 1] = {0};
  int sum[MAX_V + 2] = {0};
  int i, k, v, n;
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }
  for (i = 0; i <= MAX_V + 1; i++) {
    sum[i + 1] += sum[i] + prime[i];
  }

  while (scanf("%d", &n)) {
    if (n == 0)
      break;
    printf("%d\n", sum[n + 1]);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
const int MAX_V = 999999;
int main() {
  int prime[MAX_V + 1] = {0};
  int sum[MAX_V + 2] = {0};
  int i, k, v, n;
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }
  for (i = 0; i <= MAX_V + 1; i++) {
    sum[i + 1] += sum[i] + prime[i];
  }

  while (scanf("%d", &n) != EOF) {
    printf("%d\n", sum[n + 1]);
  }
  return 0;
}
Problem p00009 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int prime[40];
  bool is_prime[40];
  int i = 2, j = 2, n, count = 0;
  while (scanf("%d", &n) != EOF) {
    int p = 0;
    for (i = 0; i <= n; i++) {
      is_prime[i] = true;
    }
    is_prime[0] = is_prime[1] = false;
    for (i = 2; i <= n; i++) {
      if (is_prime[i]) {
        prime[p++] = i;
        for (j = 2 * i; j <= n; j += i)
          is_prime[j] = false;
      }
    }
    cout << p << endl;
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int prime[1000000];
  bool is_prime[1000000];
  int i = 2, j = 2, n, count = 0;
  while (scanf("%d", &n) != EOF) {
    int p = 0;
    for (i = 0; i <= n; i++) {
      is_prime[i] = true;
    }
    is_prime[0] = is_prime[1] = false;
    for (i = 2; i <= n; i++) {
      if (is_prime[i]) {
        prime[p++] = i;
        for (j = 2 * i; j <= n; j += i)
          is_prime[j] = false;
      }
    }
    cout << p << endl;
  }
  return 0;
}