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

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, k, sum, check;
  int t = 1;
  vector<int> prime;
  prime.push_back(0);
  prime.push_back(0);
  for (int i = 2; i < 1000000; i++) {
    prime.push_back(1);
  }
  while (t < 1000000) {
    for (int j = t + 1; j <= 1000000; j++) {
      if (prime[j] == 1) {
        t = j;
        for (k = 2 * j; k <= 1000000; k += j) {
          prime[k] = 0;
        }
        break;
      } else if (j == 1000000) {
        t = 1000000;
      }
    }
  }
  while (cin >> n) {
    sum = 0;
    for (check = 1; check <= n; check++) {
      if (prime[check] == 1) {
        sum += 1;
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, u, v, sum;
  double t;
  while (cin >> n) {
    sum = 0;
    for (int i = 1; i <= n; i++) {
      v = 0;
      t = sqrt(i) / 2;
      if (i > 7 && (i % 2 == 0 || i % 3 == 0 || i % 5 == 0)) {
        sum += 0;
      } else {
        for (int j = 0; j <= t; j++) {
          u = 2 * j + 1;
          if (i % u == 0) {
            v += 1;
          }
        }
      }
      while (v == 1) {
        sum += 1;
        break;
      }
    }
    if (n > 3) {
      cout << sum - 2 << endl;
    } else if (n == 2 || n == 3) {
      cout << n - 1 << endl;
    } else if (n == 1) {
      cout << 0 << endl;
    }
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 1000000
int p[MAX];
void f() {
  int n = 0;
  for (int i = 2; i < MAX; i++) {
    if (p[i] == 0) {
      p[i] = ++n;
      for (int j = 2; j * i < MAX; j++)
        p[j * i] = -1;
    } else
      p[i] = n;
  }
}
int main() {
  int n;
  f();
  while (cin >> n) {
    cout << p[n] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 100000
int p[MAX + 1];
void f() {
  int n = 0;
  for (int i = 2; i <= MAX; i++) {
    if (p[i] == 0) {
      p[i] = ++n;
      for (int j = 2; j * i <= MAX; j++)
        p[j * i] = -1;
    } else
      p[i] = n;
  }
}
int main() {
  int n;
  f();
  while (cin >> n) {
    cout << p[n] << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

bool isPrime(unsigned int n) {
  unsigned int count = 0;
  unsigned int max;
  count = 0;
  max = n;

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

int main() {
  unsigned int input;
  unsigned int count;
  unsigned int cum[500000]; // for odd numbers greater than or equal to 1

  count = 1;  // For 2
  cum[0] = 0; // For 1
  cum[1] = 2; // For 3
  for (int i = 2; i < 500000; i++) {
    if (isPrime(2 * i + 1)) {
      cum[i] = cum[i - 1] + 1;
      count++;
    } else {
      cum[i] = cum[i - 1];
    }
  }

  while (cin >> input) {
    count = 0;
    if (input == 2)
      cout << "1" << endl;
    else
      cout << cum[(int)(input - 0.5) / 2] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool isPrime(unsigned int n) {
  unsigned int count = 0;
  count = 0;

  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;

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

int main() {
  unsigned int input;
  unsigned int count;

  while (cin >> input) {
    count = 0;
    unsigned int i = 2;
    for (int i = 2; i <= input; i++) {
      if (isPrime(i))
        count++;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#define MAX_N 1000000

using namespace std;

bool IsPrime[MAX_N];
int NofP[MAX_N];
void q9() {
  memset(IsPrime, 1, sizeof(IsPrime));
  IsPrime[0] = false;
  IsPrime[1] = false;
  for (int i = 0; i < sqrt(MAX_N); i++) {
    if (!IsPrime[i])
      continue;
    for (int j = i * 2; j < MAX_N; j += i) {
      IsPrime[j] = false;
    }
  }

  NofP[0] = 0;
  for (int i = 1; i < MAX_N; i++) {
    NofP[i] = NofP[i - 1] + IsPrime[i];
  }

  int n;
  for (; cin >> n;) {
    cout << NofP[n] << endl;
  }
}

int main() {
  q9();
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#define MAX_N 100000

using namespace std;
void q9() {
  bool IsPrime[MAX_N];
  memset(IsPrime, 1, sizeof(IsPrime));
  IsPrime[0] = false;
  IsPrime[1] = false;
  for (int i = 0; i < sqrt(MAX_N); i++) {
    if (!IsPrime[i])
      continue;
    for (int j = i * 2; j < MAX_N; j += i) {
      IsPrime[j] = false;
    }
  }

  int NofP[MAX_N];
  NofP[0] = 0;
  for (int i = 1; i < MAX_N; i++) {
    NofP[i] = NofP[i - 1] + IsPrime[i];
  }

  int n;
  for (; cin >> n;) {
    cout << NofP[n] << endl;
  }
}

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

#include <iostream>
using namespace std;

bool isprime(int n) {
  if (n < 2)
    return false;

  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  bool memo[1000000] = {false};
  for (int i = 0; i < 1000000; i++) {
    memo[i] = isprime(i);
  }
  while (cin >> n) {
    int output = 0;
    for (int i = 0; i <= n; i++) {
      if (memo[i])
        output++;
    }
    cout << output << endl;
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  int n, m;
  while (scanf("%d", &n) != EOF) {
    m = 0;
    for (int x = 2; x <= n; x++) {
      for (int y = x - 1; y; y--) {
        if (y == 1) {
          m++;
        } else if (x % y == 0) {
          break;
        }
      }
    }
    printf("%d\\n", m);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int prime[1000000];
signed main(void) {
  REP(i, 1000000) prime[i] = 1;
  prime[0] = prime[1] = 0;
  for (int i = 2; i * i <= 1000000; i++) {
    if (prime[i]) {
      for (int j = 2 * i; j <= 1000000; j += i) {
        prime[j] = 0;
      }
    }
  }
  // REP(i, 10) cout << prime[i] << " "; cout << endl;
  FOR(i, 1, 1000000) {
    prime[i] += prime[i - 1];
    // cout << prime[i] << endl;
  }
  int n;
  while (cin >> n) {
    cout << prime[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

signed main(void) {
  int n;
  while (cin >> n) {
    int ret = 0;
    for (int i = 2; i <= n; ++i) {
      bool flag = true;
      for (int j = 2; j * j <= i; ++j) {
        if (i % j == 0)
          flag = false;
      }
      if (flag) {
        ret++;
      }
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
using namespace std;
int main() {
  unsigned long long int i;
  int n, p, cnt, fg;
  while (cin >> n) {
    i = 3;
    cnt = 1;
    fg = 0;

    if ((int)log10(n) == 3) {
      cnt = 168;
      i = 1001;
    } else if ((int)log10(n) == 4) {
      cnt = 1229;
      i = 10001;
    } else if ((int)log10(n) == 5) {
      cnt = 9592;
      i = 100001;
    }

    if (200000 < n) {
      cnt = 17984;
      i = 200001;
    }
    if (300000 < n) {
      cnt = 25997;
      i = 300001;
    }
    if (400000 < n) {
      cnt = 33860;
      i = 400001;
    }
    if (500000 < n) {
      cnt = 41538;
      i = 500001;
    }
    if (600000 < n) {
      cnt = 49098;
      i = 600001;
    }
    if (700000 < n) {
      cnt = 56543;
      i = 700001;
    }
    if (800000 < n) {
      cnt = 63951;
      i = 800001;
    }
    if (900000 < n) {
      cnt = 71274;
      i = 900001;
    }

    for (; i <= n; i += 2) {
      for (int j = 3; j <= (int)sqrt(i); j += 2) {
        if (i % j == 0) {
          fg = 1;
          break;
        }
      }
      if (fg)
        fg = 0;
      else
        cnt++;
    }
    if (n == 1)
      cnt--;
    cout << cnt << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
using namespace std;
int main() {
  unsigned long long int i;
  int n, p, cnt, fg;
  while (cin >> n) {
    i = 3;
    cnt = 1;
    fg = 0;

    if ((int)log10(n) == 3) {
      cnt = 168;
      i = 1001;
    } else if ((int)log10(n) == 4) {
      cnt = 1229;
      i = 10001;
    } else if ((int)log10(n) == 5) {
      cnt = 9592;
      i = 100001;
    }
    if (110000 < n) {
      cnt = 10453;
      i = 110001;
    }
    if (120000 < n) {
      cnt = 11301;
      i = 120001;
    }
    if (130000 < n) {
      cnt = 12159;
      i = 130001;
    }
    if (140000 < n) {
      cnt = 13010;
      i = 140001;
    }
    if (150000 < n) {
      cnt = 13848;
      i = 150001;
    }
    if (160000 < n) {
      cnt = 14683;
      i = 160001;
    }
    if (170000 < n) {
      cnt = 15493;
      i = 170001;
    }
    if (180000 < n) {
      cnt = 16342;
      i = 180001;
    }
    if (190000 < n) {
      cnt = 17170;
      i = 190001;
    }

    for (; i <= n; i += 2) {
      for (int j = 3; j <= (int)sqrt(i); j += 2) {
        if (i % j == 0) {
          fg = 1;
          break;
        }
      }
      if (fg)
        fg = 0;
      else
        cnt++;
    }
    if (n == 1)
      cnt--;
    cout << cnt << endl;
  }

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

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

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, p[10003];
  for (int i = 0; i <= 10000; ++i)
    p[i] = 1;
  p[0] = 0;
  p[1] = 0;
  for (int i = 2; i <= 10000, i * i < 10000; ++i) {
    for (int j = 2; j <= 10000; ++j) {
      if (p[i] == 0)
        break;
      if (j % i == 0 && i != j)
        p[j] = 0;
    }
  }
  while (cin >> n) {
    int sum = 0;
    for (int i = 0; i <= n; ++i)
      if (p[i] == 1)
        ++sum;
    cout << sum << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define N 1000002
bool is_prime(int n) {
  if (n <= 1)
    return false;
  if (n == 2)
    return true;
  if (!(n & 1))
    return false;
  for (int x = 3; x <= sqrt(n); x += 2)
    if (!(n % x))
      return false;
  return true;
}

int main() {
  int n;
  int primes[N];
  primes[0] = primes[1] = 0;
  int cnt = 1;
  primes[2] = cnt;
  for (int x = 3; x <= N; ++x) {
    if (is_prime(x)) {
      ++cnt;
    }
    primes[x] = cnt;
  }
  while (scanf("%d", &n) == 1) {
    cout << primes[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

bool is_prime(int n) {
  if (n <= 1)
    return false;
  if (n == 2)
    return true;
  if (!(n & 1))
    return false;
  for (int x = 3; x <= sqrt(n); ++x)
    if (!(n % x))
      return false;
  return true;
}

int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    int tot = 0;
    if (n >= 2)
      ++tot;
    for (int x = 3; x <= n; x += 2) {
      if (is_prime(x))
        ++tot;
    }
    cout << tot << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define N 1000000

int main(void) {
  bool a[N];
  int n, count;
  rep(i, N) a[i] = true;
  a[0] = a[1] = false;
  for (int i = 0; i * i <= N; i++) {
    if (a[i])
      for (int j = i * i; j < N; j += i)
        a[j] = false;
  }

  while (cin >> n) {
    count = 0;

    rep(i, n + 1) {
      if (a[i])
        count++;
    }
    cout << count << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

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

const int N = 100000;

int main(void) {
  bool a[N];
  int prime[N], n, count = 0;
  rep(i, N) a[i] = true;
  a[0] = a[1] = false;
  for (int i = 2; i * i <= N; i++) {
    if (a[i]) {
      prime[i] = prime[i - 1] + 1;
      for (int j = i * i; j < N; j += i)
        a[j] = false;
    } else
      prime[i] = prime[i - 1];
  }

  while (cin >> n) {
    rep(i, n + 1) {
      if (a[i])
        count++;
    }
    cout << count << endl;
  }

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

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

int main() {
  // bool num[MAX];
  pair<bool, int> num[MAX];
  int input = 0;
  while (cin >> input) {
    int count = 0;
    for (int i = 2; i < MAX; i++) {
      num[i].first = true;
      num[i].second = i;
    }
    for (int i = 2; i < input + 1; i++) {
      if (num[i].first) {
        // cout << num[i].second <<",";
        count++;
        for (int j = i + i; j < MAX; j += i) {
          num[j].first = false;
        }
      }
    }
    cout << count << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  // bool num[MAX];
  pair<bool, int> num[MAX];
  int input = 0;
  while (cin >> input) {
    int count = 0;
    for (int i = 2; i < MAX; i++) {
      num[i].first = true;
      num[i].second = i;
    }
    for (int i = 2; i < input + 1; i++) {
      if (num[i].first) {
        // cout << num[i].second <<",";
        count++;
        for (int j = i + i; j < MAX; j += i) {
          num[j].first = false;
        }
      }
    }
    cout << count << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int main(void) {

  int sosuu[1000000], kazu, n, i, j;

  while (scanf("%d", &n) != EOF) {

    kazu = n - 1;

    for (i = 2; i <= n; i++) {
      sosuu[i] = 1;
    }
    sosuu[0] = 0;
    sosuu[1] = 0;

    for (i = 2; i <= n; i++) {
      if (sosuu[i] == 1) {
        for (j = i * 2; j <= n; j += i) {
          if (sosuu[j] == 1) {
            kazu--;
            sosuu[j] = 0;
          }
        }
      }
    }

    printf("%d\\n", kazu);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

int main(void) {

  int sosuu[100000], kazu, n, i, j;

  while (scanf("%d", &n) != EOF) {
    kazu = n - 1;
    for (i = 2; i <= n; i++) {
      sosuu[i] = 1;
    }
    for (i = 2; i <= n; i++) {
      if (sosuu[i] == 1) {
        for (j = i * 2; j <= n; j += i) {
          if (sosuu[j] == 1) {
            kazu--;
            sosuu[j] = 0;
          }
        }
      }
    }
    printf("%d\\n", kazu);
  }

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

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

int main() {
  int n, prime[1000001], key, lf = 0;
  prime[0] = 2;
  key = 1;
  for (int i = 3; i <= 1000100; i += 2) {
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0) {
        lf = 1;
        break;
      }
    }
    if (lf == 1) {
      lf = 0;
    } else {
      prime[key] = i;
      key++;
    }
  }

  while (cin >> n) {
    int cou = 0;
    for (int i = 0;; i++) {
      if (prime[i] <= n) {
        cou++;
      } else
        break;
    }

    cout << cou << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, prime[1000001], key, lf = 0;
  prime[0] = 2;
  key = 1;
  for (int i = 3; i <= 2000000; i += 2) {
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0) {
        lf = 1;
        break;
      }
    }
    if (lf == 1) {
      lf = 0;
    } else {
      prime[key] = i;
      key++;
    }
  }

  while (cin >> n) {
    int cou = 0;
    for (int i = 0;; i++) {
      if (prime[i] <= n) {
        cou++;
      } else
        break;
    }

    cout << cou << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int f[1000001];
int main() {
  fill_n(f, 1000001, 1);
  f[0] = f[1] = 0;

  for (int i = 2; i < 1000001; i++) {
    f[i] += f[i - 1];
    if (!f[i])
      continue;
    for (int j = i + i; j < 1000001; j += i)
      f[j] = 0;
  }

  int N;

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

  return 0;
}

Error: TLE

Fixed submission:

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

int f[1000001];
int main() {
  fill_n(f, 1000001, 1);
  f[0] = f[1] = 0;

  for (int i = 2; i < 1000001; i++) {
    f[i] += f[i - 1];
    if (!f[i])
      continue;
    for (int j = i + i; j < 1000001; j += i)
      f[j] = 0;
  }

  int N;

  while (scanf("%d", &N), &N) {
    printf("%d\\n", f[N]);
  }

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

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

Error: 0

Fixed submission:

#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;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int i, j, n;
  static bool prime[1000001];
  for (i = 3; i <= 1000000; i += 2) {
    prime[i] = true;
  }
  prime[2] = true;

  for (i = 3; i <= 1000; i += 2) {
    if (prime[i] == 1) {
      for (j = 3; j <= 1000000 / i; j += 2) {
        prime[i * j] = false;
      }
    }
  }
  while (cin >> n && n) {
    int count = 0;
    for (i = 2; i <= n; i++) {
      if (prime[i] == true)
        count++;
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

int main() {
  using namespace std;
  while (1) {
    int n, i, j = 2, count = 0, limit;
    cin >> n;
    if (cin.eof() || n == 0)
      break;
    int prime[n + 1];
    for (i = 2; i <= n; i++)
      prime[i] = 1;
    limit = sqrt(n);
    for (i = 2; i <= limit; i++) {
      if (prime[i] == 0)
        continue;
      else {
        for (j = 2 * i; j <= n; j++) {
          if (j % i == 0)
            prime[j] = 0;
        }
      }
    }
    for (i = 2; i <= n; i++) {
      if (prime[i] == 1)
        count += 1;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <stdio.h>

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

int main(void) {
  int list[1000000] = {0, 1};
  int i, data, cnt;
  for (i = 3; i <= 999999; i++) {
    if (i % 2 == 0) {
      list[i - 1] = 0;
    } else {
      list[i - 1] = prime(i);
    }
  }
  while (std::cin >> data) {
    cnt = 0;
    for (i = 0; i < data; i++) {
      if (list[i] == 1) {
        cnt++;
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>

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

int main(void) {
  int data, cnt = 0, i;
  while (std::cin >> data) {
    if (data > 1)
      cnt = 1;
    for (i = 3; i <= data; i++) {
      if (prime(i)) {
        cnt++;
      }
    }
    std::cout << cnt << std::endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
using namespace std;
bool n[1000000] = {0};
bool prime(int n) {
  for (int i = 2; i <= sqrt((double)n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int main() {
  for (int k = 2; k <= 999999; k++) {
    if (prime(k))
      n[k] = true;
  }

  int a, sum = 0;
  while (cin >> a) {
    for (int j = 2; j <= a; j++) {
      if (n[j])
        sum++;
    }
    cout << sum << endl;
    sum = 0;
  }
}

Error: TLE

Fixed submission:

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

#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];
  }
  int n;
  while (cin >> n) {
    cout << npr[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, i, j, a = 0, b = 0;
  while (cin >> n) {
    if (n >= 9) {
      for (i = 9; i <= n; i++) {
        a = 0;
        for (j = 3; j < i; j++) {
          if (i % j == 0) {
            a++;
            b += a;
            break;
          }
          j++;
        }
        i++;
      }
    }
    if (n == 2)
      cout << "1" << endl;
    else if (n == 1)
      cout << "0" << endl;
    else if (n == 3)
      cout << "2" << endl;
    else if (n == 5)
      cout << "3" << endl;
    else if (n == 7)
      cout << "4" << endl;
    else if (n % 2 == 0)
      cout << n / 2 - b << endl;
    else
      cout << (n + 1) / 2 - b << endl;
    b = 0;
  }
  return 0;
}