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

#include <iostream>
using namespace std;

int main() {
  int n = 0;
  int a[40] = {0};
  while (cin >> a[n++])
    ;
  n--;

  int maxn = 1000000;
  bool p[maxn];
  for (int i = 0; i < maxn; i++) {
    p[i] = 1;
  }
  for (int i = 2; i < maxn; i++) {
    for (int j = i + i; j < maxn; j += i) {
      p[j] = 0;
    }
  }

  for (int i = 0, cnt = 0; i < n; i++) {
    for (int j = 2; j <= a[i]; j++) {
      cnt += p[j];
    }
    cout << cnt << endl;
    cnt = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n = 0;
  int a[40] = {0};
  while (cin >> a[n++])
    ;
  n--;
  for (int i = 0; i < n; i++) {
    if (a[i] == 2)
      cout << "1" << endl;
    else if (a[i] == 3)
      cout << "2" << endl;
    else {
      int pcnt = 0, cnt = 0;
      for (int j = 2; j <= a[i]; j++) {
        for (int k = 1; k <= j; k++) {
          if (j % k == 0)
            cnt++;
        }
        if (cnt == 2)
          pcnt++;
        cnt = 0;
      }
      cout << pcnt << endl;
      pcnt = 0;
    }
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;
int main() {
  static bool cmp[1000000] = {true};
  static int ans[1000000];
  for (int i = 2; i < 1000000; i++) {
    if (!cmp[i]) {
      for (int j = 2; j * i < 1000000; j++) {
        cmp[i * j] = true;
      }
    }
    ans[i] = ans[i - 1] + !cmp[i];
  }
  for (int n; cin >> n; n++) {
    cout << ans[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = n; i > 1; i--) {
      if (i == 2) {
        ans++;
      } else {
        bool isPrime = true;
        for (int j = 2; j <= i / 2; j++) {
          if (i % j == 0) {
            isPrime = false;
            break;
          }
        }
        if (isPrime)
          ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

const int MAXN = 1000005;
int num[MAXN];

int main() {
  for (int i = 0; i < MAXN; ++i) {
    num[i] = isPrime(i);
  }
  for (int i = 1; i < MAXN; ++i) {
    num[i] += num[i - 1];
  }
  int n;
  while (cin >> n) {
    cout << num[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  int n;
  while (cin >> n) {
    int num = 0;
    for (int i = 2; i <= n; ++i)
      num += isPrime(i);
    cout << num << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

int main() {
  int n;

  while (std::cin >> n) {
    int checkLimit = sqrt((double)n);

    std::vector<bool> Prime(n + 1, true);

    Prime[0] = false;

    Prime[1] = false;

    for (int i = 2; i < checkLimit + 1; ++i) {
      if (!(Prime[i])) {
        continue;
      }

      for (int j = 2 * i; j < n + 1; j += i) {
        Prime[j] = false;
      }
    }

    std::cout << std::count(Prime.begin(), Prime.end(), true) << '\\n';
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int n;

  while (std::cin >> n) {
    std::vector<int> Sosuu(1);

    Sosuu[0] = 2;

    for (int i = 2; i < n; ++i) {
      size_t j = 0;

      for (; j < Sosuu.size(); ++j) {
        if (i % Sosuu[j] == 0) {
          break;
        }
      }
      if (j == Sosuu.size()) {
        Sosuu.push_back(i);
      }
    }

    std::cout << Sosuu.size() << '\\n';
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    vector<bool> a(n + 1, true);
    int cnt = n == 0 ? 0 : n - 1;

    // Sieve of Eratosthenes
    for (int i = 2; i * i <= n; ++i)
      if (a[i])
        for (int j = 0; i * (j + 2) <= n; ++j)
          if (a[i * (j + 2)])
            a[i * (j + 2)] = false, cnt--;

    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

  return true;
}

int main() {
  int n;

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

    if (n > 1)
      cnt++;
    for (int i = 3; i <= n; i += 2)
      cnt += is_prime(i);

    cout << cnt << endl;
  }

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

#include <iostream>
using namespace std;
// エラトステネスのふるい
int main() {
  int num = 1000000;
  int n, prime[num];
  while (cin >> n) {
    for (int i = 0; i < num; i++) {
      prime[i] = 0;
    }
    prime[0] = 1;
    prime[1] = 1;

    int count = 0;
    for (int i = 0; i <= n; i++) {
      if (prime[i] == 0) {
        count++;
        for (int j = i; j <= n; j += i) {
          prime[j] = 1;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
// エラトステネスのふるい
int main() {
  int num = 1000;
  int n, prime[num];
  while (cin >> n) {
    for (int i = 0; i < num; i++) {
      prime[i] = 0;
    }
    prime[0] = 1;
    prime[1] = 1;

    int count = 0;
    for (int i = 0; i <= n; i++) {
      if (prime[i] == 0) {
        count++;
        for (int j = i; j <= n; j += i) {
          prime[j] = 1;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

void prima() {
  bool isprima;
  int a, n, c = 0;
  while (cin >> a && c < 30) {
    n = 0;
    for (int i = 2; i <= a; i++) {
      isprima = true;
      for (int j = 2; j < i; j++) {
        if (i == 2) {
          isprima = true;
          break;
        } else if (i % j == 0) {
          isprima = false;
          break;
        }
      }
      if (isprima)
        n++;
    }
    cout << n << endl;
    c++;
  }
}

int main() { prima(); }
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

// 36
#include <iostream>

using namespace std;

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

Error: TLE

Fixed submission:

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

void setPrime(int *prime, int num) {
  vector<int> prime_num;
  prime_num.push_back(2);
  prime[2] = 1;
  for (int i = 3; i < num; i++) {
    bool isPrime = true;
    for (int j = 0; j < prime_num.size(); j++) {
      if (i % prime_num[j] == 0)
        isPrime = false;
    }
    if (isPrime) {
      prime_num.push_back(i);
    }
    prime[i] == prime_num.size();
  }
}

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

#include <iostream>
using namespace std;

bool is_prime[1000000];
int n_prime[1000000];
int main() {

  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 cnt = 0;
  for (int i = 0; i < 1000000; i++) {
    if (is_prime[i]) {
      cnt++;
    }
    n_prime[i] = cnt;
  }
  while (true) {
    int n;
    cin >> n;

    if (cin.eof())
      break;

    cout << n_prime[n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool is_prime[1000000];
int n_prime[1000000];
int main() {

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

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

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

  int n;
  cin >> n;

  cout << n_prime[n] << endl;

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

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

int main() {
  const size_t MAX = 1000000;

  bitset<MAX> primebox;
  primebox.set();
  primebox.reset(0);
  primebox.reset(1);

  for (int i = 2; i < MAX; i++) {
    if (!primebox.test(i))
      continue;

    for (int j = i * 2; j < MAX; j += i)
      primebox.reset(j);
  }

  size_t n;
  while (cin >> n) {
    int found;
    if (n < 2)
      found = 0;
    else if (n == 2)
      found = 1;
    else {
      found = 1;
      for (size_t i = 3; i <= n; i += 2)
        if (primebox.test(i))
          found++;
    }

    cout << found << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

class PrimeBox {
private:
  bitset<1000000> m_box;
  size_t m_size;

public:
  PrimeBox() : m_box(), m_size(2) {
    m_box.set();
    m_box.reset(0);
    m_box.reset(1);
  }

private:
  void expand(size_t size) {
    for (size_t i = m_size + 1; i <= size; i++) {
      for (size_t j = 2; j < i; j++) {
        if (!m_box.test(j))
          continue;

        if (i % j == 0) {
          m_box.reset(i);
          break;
        }
      }
    }
    m_size = size;
  }

public:
  bool isPrime(size_t number) {
    if (m_size < number)
      expand(number);

    return m_box.test(number);
  }
};

int main() {
  PrimeBox box;

  size_t n;
  while (cin >> n) {
    int found = 0;
    for (size_t i = 2; i <= n; i++) {
      if (box.isPrime(i))
        found++;
    }

    cout << found << endl;
  }

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

#include <stdio.h>
#define MAX 1000010

int is_prime[MAX];

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

int main() {
  int a, i;
  int sum[MAX];
  sieve();
  for (i = 0; i < MAX; i++)
    sum[i] = 0;
  for (i = 1; i < MAX; i++)
    sum[i] = sum[i - 1] + is_prime[i];
  while (scanf("%d", &a) != EOF) {
    printf("%d\\n", sum[a]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int isPrime(int n) {
  int i;
  if (n < 2)
    return 0;
  if (n == 2)
    return 1;
  if (n % 2 == 0)
    return 0;
  for (i = 3; i <= n / i; i += 2)
    if (n % i == 0)
      return 0;
  return 1;
}

int main() {
  int a;
  while (scanf("%d", &a) != EOF) {
    int i, ans = 0;
    for (i = 0; i <= a; i++) {
      if (isPrime(i))
        ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

#define debug(x) cerr << #x << ": " << x << '\\n'

#define fi first
#define se second

#define vi vector<int>
#define pb push_back
#define mp make_pair

#define pcnt __builtin_popcount

typedef long long ll;

const int inf = 100100100;
const int MOD = 998244353;
const double EPS = 1e-9;

bool s[1000000] = {};
int sosu[1000000] = {};
void erast() {
  memset(s, 1, sizeof(s));
  s[1] = false;
  s[0] = false;
  REP(i, 2, 1000) {
    if (s[i]) {
      REP(j, 2, 1000000) {
        if (i * j >= 1000000)
          break;
        s[i * j] = false;
      }
    }
  }
  REP(i, 2, 1000000) sosu[i] = sosu[i - 1] + s[i];
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  erast();
  int n;
  while (cin >> n) {
    cout << sosu[n] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

#define debug(x) cerr << #x << ": " << x << '\\n'

#define fi first
#define se second

#define vi vector<int>
#define pb push_back
#define mp make_pair

#define pcnt __builtin_popcount

typedef long long ll;

const int inf = 100100100;
const int MOD = 998244353;
const double EPS = 1e-9;

bool s[1000000] = {};
int sosu[1000000] = {};
void erast() {
  memset(s, 1, sizeof(s));
  s[1] = false;
  s[0] = false;
  REP(i, 2, 1000) {
    if (s[i]) {
      REP(j, 2, 1000000) {
        if (i * j >= 1000000)
          break;
        s[i * j] = false;
      }
    }
  }
  REP(i, 2, 1000000) sosu[i] = sosu[i - 1] + s[i];
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  erast();
  int n;
  while (cin >> n, n) {
    cout << sosu[n] << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
using namespace std;
const int N = 1000000;
int IsPrime[N];
int main() {

  IsPrime[0] = 1;
  IsPrime[1] = 1;
  for (int i = 2; i <= N; i++) {
    if (IsPrime[i] == 0) {
      for (int j = 2 * i; j <= N; j += i)
        IsPrime[j] = 1;
    }
  }
  int max;
  while (~scanf("%d", &max)) {
    int count = 0;
    for (int i = 1; i < max + 1; i++)
      if (IsPrime[i] == 0)
        count++;
    printf("%d\\n", count);
  }
}


Error: 0

Fixed submission:

#include <cstdio>
using namespace std;

int Eratosthenes(int N) {

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

  delete[] IsPrime;
  return count;
}

int main(void) {
  int N[30] = {};
  int count = 0;
  int i = 0;
  while (1) {
    if (scanf("%d", &N[i]) != 1)
      break;
    i++;
  }
  i = 0;
  while (N[i] > 0) {
    count = Eratosthenes(N[i]);
    printf("%d\\n", count);
    i++;
  }

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

#include <iostream>
using namespace std;
int main() {
  int a[1000000] = {0, 0, 1, 2}, n, k = 0, count = 2;
  for (int i = 4; i < 1000000; i++) {
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        k++;
        break;
      }
    }
    if (k == 0)
      count++;
    a[i] = count;
    k = 0;
  }
  while (cin >> n)
    cout << a[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int a[999999] = {0, 0, 1, 2}, n, k = 0, count = 2;
  for (int i = 4; i < 999999; i++) {
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0)
        k++;
    }
    if (k == 1)
      count++;
    a[i] = count;
    k = 0;
  }
  while (cin >> n)
    cout << a[n] << endl;
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
typedef pair<ll, ll> cp2;
typedef pair<ll, cp2> cp3;
#define fi first
#define se second
#define sec se.fi
#define thr se.se
const ll mod = 1000000007;
//              123456789

///////////////////////////////////////////////
//?????§??¬?´???°??¨????°???¬?????°
// O(1)?????????
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////

#define NUM_MAX 1000010
bool flag[NUM_MAX];
vector<int> prm;

int N;
int ans = 0;
int K;

void eratosthenes() {
  if (N < 2)
    return;
  fill(flag, flag + NUM_MAX, 0);
  int idx = 2;
  while (idx <= K) {
    while (flag[idx])
      idx++;
    if (idx > N)
      break;
    // prm.push_back( idx );
    for (int i = idx; i <= N; i += idx)
      flag[i] = 1;
    ans++;
  }
  while (idx < N) {
    idx++;
    if (!flag[idx]) {
      // prm.push_back( idx );
      ans++;
    }
  }
}

int main() {

  while (scanf("%d", &N) != EOF) {
    K = sqrt(N);
    K++;

    eratosthenes();

    cout << ans << endl;
    /*
    REP( i, prm.size() ){
            cout<<prm[i]<<endl;
    }*/
    // prm.clear();
    ans = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
typedef pair<ll, ll> cp2;
typedef pair<ll, cp2> cp3;
#define fi first
#define se second
#define sec se.fi
#define thr se.se
const ll mod = 1000000007;
//              123456789

///////////////////////////////////////////////
//?????§??¬?´???°??¨????°???¬?????°
// O(1)?????????
///////////////////////////////////////////////

////////////////////////////////////////////////
////////////////////////////////////////////////

#define NUM_MAX 10000000
bool flag[NUM_MAX];
vector<int> prm;

int N;
int ans = 0;
int K;

void eratosthenes() {
  if (N < 2)
    return;
  fill(flag, flag + NUM_MAX, 0);
  int idx = 2;
  while (idx <= K) {
    while (flag[idx])
      idx++;
    if (idx > N)
      break;
    prm.push_back(idx);
    for (int i = idx; i <= N; i += idx)
      flag[i] = 1;
    ans++;
  }
  while (idx < N) {
    idx++;
    if (!flag[idx]) {
      prm.push_back(idx);
      ans++;
    }
  }
}

int main() {

  while (cin >> N, N != EOF) {
    K = sqrt(N);
    K++;

    eratosthenes();

    cout << ans << endl;
    /*
    REP( i, prm.size() ){
            cout<<prm[i]<<endl;
    }*/
    prm.clear();
    ans = 0;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int count_prime(int n) {
  int c = 0;
  bool ps[n + 1];

  ps[0] = false;
  ps[1] = false;

  for (int i = 2; i <= n; ++i)
    ps[i] = true;

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

  for (int i = 1; i <= n; ++i) {
    if (ps[i])
      ++c;
  }
  return c;
}

int main() {
  int n;
  int c;

  while (cin >> n) {
    c = count_prime(n);
    cout << c << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

bool isPrime(int n) {

  if (n == 1)
    return false;
  if (n == 2)
    return true;

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

int main() {
  int n;

  while (cin >> n) {
    int c = 0;
    for (int i = 1; i <= n; ++i) {
      if (isPrime(i))
        ++c;
    }
    cout << c << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int count = 0, n;
  bitset<1000001> prime;

  prime[0] = 1;
  prime[1] = 1;
  for (int i = 2; i <= 1000; i++)
    if (prime[i] == 0)
      for (int j = i + 1; j <= 1000000; j++)
        if (j % i == 0)
          prime[j] = 1;

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

Error: TLE

Fixed submission:

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

int main() {
  int count = 0, n;
  bitset<1000000> prime;
  prime.reset();

  while (cin >> n) {
    prime[0] = 1;
    prime[1] = 1;
    for (int i = 2; i <= sqrt(n); i++)
      if (prime[i] == 0)
        for (int j = i + 1; j <= n; j++)
          if (j % i == 0)
            prime[j] = 1;
    for (int i = 2; i <= n; i++)
      if (prime[i] == 0)
        count++;
    cout << count << endl;
    count = 0;
    prime.reset();
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int Prime[1000000];

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

  Prime[0] = 0;
  Prime[1] = 0;

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

  int n, cnt;
  while (cin >> n) {
    cnt = 0;
    for (int i = 0; i <= n; ++i) {
      cnt += Prime[i];
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  int n;
  int count;
  int prime[999999];
  int max = 0;

  while (cin >> n) {
    if (n > max) {

      count = 1;
      prime[0] = 2;
      for (int i = 3; i <= n; i += 2) {
        int isPrime = true;

        for (int j = 0; j < count; ++j) {
          if (i % prime[j] == 0) {
            isPrime = false;
            break;
          }
        }
        if (isPrime) {
          prime[count] = i;
          count++;
        }
      }
      max = n;
    } else {
      count = 0;
      for (int i = 0; i < 999999; ++i) {
        if (prime[i] > n)
          break;
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#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;
}

Error: 0

Fixed submission:

#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;
}