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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
// ranker
using namespace std;

#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using pis = pair<int, string>;
using psi = pair<string, int>;
using D = double;

const int Max = 10000000;
bool prime[Max];
int main(void) {
  int in;
  for (int i = 0; i < Max; i++)
    prime[i] = true;
  prime[1] = false;
  for (int i = 2; i < Max; i++) {
    if (prime[i]) {
      for (int s = i * 2; s <= Max; s += i)
        prime[s] = false;
    }
  }
  while (cin >> in) {
    int cnt = 0;
    for (int i = 1; i <= in; i++) {
      if (prime[i])
        cnt++;
    }
    cout << cnt << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
// ranker
using namespace std;

#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using pis = pair<int, string>;
using psi = pair<string, int>;
using D = double;

int main() {
  int n, ans, cnt, prime;

  while (cin >> n) {
    prime = 1;
    if (n == 1)
      cout << 0 << endl;
    if (n == 2)
      cout << 1 << endl;
    if (n == 3)
      cout << 2 << endl;

    if (n >= 4) {
      for (int i = 3; i <= n; i++) {
        cnt = 0;
        for (int j = 3; j <= sqrt(i); j += 2) {
          if (i % j == 0)
            cnt++;
        }
        if (cnt == 0 && i % 2 != 0)
          prime++;
      }
      cout << prime << endl;
    }
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <vector>

using namespace std;

vector<int> ps;

int isprime(int n) {
  vector<int>::iterator it;
  for (it = ps.begin(); it != ps.end(); it++) {
    if (n % (*it) == 0)
      return 0;
    if (n <= (*it) * (*it))
      break;
  }
  ps.push_back(n);
  return 1;
}

int main() {
  int n;
  ps.push_back(2);
  while (scanf("%d", &n) != EOF) {

    int c = 0;
    int last = 2;
    for (vector<int>::iterator it = ps.begin(); it != ps.end(); it++) {
      if ((last = *it) <= n)
        c++;
    }

    for (int i = last + 1; i <= n; i++) {
      if (i & 1 && isprime(i))
        c++;
    }

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

Error: TLE

Fixed submission:

#include <stdio.h>
#include <vector>

using namespace std;

vector<int> ps;

int isprime(int n) {
  vector<int>::iterator it;
  for (it = ps.begin(); it != ps.end(); it++) {
    if (n % (*it) == 0)
      return 0;
    if (n <= (*it) * (*it))
      break;
  }
  ps.push_back(n);
  return 1;
}

int main() {
  int n;
  ps.push_back(2);
  while (scanf("%d", &n) != EOF) {

    int c = 0;

    for (int i = 3; i <= n; i++) {
      if (i & 1 && isprime(i))
        c++;
    }

    printf("%d\\n", c);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1e7;
int is_prime[MAXN], prime[10000];
int sieve(int n) {
  memset(is_prime, -1, sizeof(is_prime));
  int index = 0;
  prime[0] = prime[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (is_prime[i])
      prime[index++] = i;
    for (int j = 2 * i; j <= n; j += i)
      is_prime[j] = 0;
  }
  return index;
}
int main(void) {
  int n;
  while (scanf("%d", &n) != EOF) {
    printf("%d\\n", sieve(n));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1e7;
int is_prime[MAXN], prime[10000];
int main(void) {
  memset(is_prime, -1, sizeof(is_prime));
  int index = 0;
  prime[0] = prime[1] = 0;
  for (int i = 2; i < MAXN; i++) {
    if (is_prime[i])
      prime[index++] = i;
    for (int j = 2 * i; j < MAXN; j += i)
      is_prime[j] = 0;
  }
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < index; i++)
      if (prime[i] <= n && prime[i + 1] > n)
        printf("%d\\n", i + 1);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

/*http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0009&lang=jp*/

#include <iostream>

#define MAX 999999

using namespace std;

int main() {
  int n;
  static int num_prime[MAX + 1];
  static bool not_prime_flag[MAX + 1];

  for (int i = 2; i < MAX + 1; ++i) {
    if (!not_prime_flag[i]) {
      for (int j = 2; i * j < MAX + 1; ++j) {
        not_prime_flag[i * j] = true;
      }
    }
    num_prime[i] = num_prime[i - 1] + !not_prime_flag[i];
  }
  while (cin >> n) {
    cout << num_prime[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

/*http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0009&lang=jp*/

#include <iostream>

using namespace std;

int main() {
  int n;

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

    if (1 < n) {
      count++;
    }
    if (n % 2 == 0) {
      n--;
    }
    while (1 < n) {
      int prime_flag = 1;
      for (int j = 2; prime_flag && j < n; j++) {
        if (n % j == 0) {
          prime_flag = 0;
        }
        if (2 < j) {
          j++;
        }
      }
      count += prime_flag;
      n -= 2;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: 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 + i <= n) {
        bell += i;
        if (math[bell] == 0) {
          math[bell] = 1;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

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 <= n) {
        bell += i;
        if (math[bell] == 0) {
          math[bell] = 1;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ELEM(array) (sizeof(array) / sizeof *(array))
#define MAX_N 100
#define SAFE_FREE(ptr)                                                         \\
  if (ptr != NULL) {                                                           \\
    free(ptr);                                                                 \\
    ptr = NULL;                                                                \\
  }
typedef unsigned int UINT;
#define ll long long
typedef pair<int, int> P;

bool prime[1000005];

void solve() {
  REP(i, 1000005) prime[i] = true;
  prime[0] = prime[1] = false;
  for (int i = 2; i * i <= 1000000; i++) {
    if (prime[i] == true) {
      for (int j = 2; j * i <= 1000000; j++) {
        if (prime[i * j] == true)
          prime[i * j] = false;
      }
    }
  }
}

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

    int ans = 0;

    solve();
    REP(i, n + 1) {
      if (prime[i] == true) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define ELEM(array) (sizeof(array) / sizeof *(array))
#define MAX_N 100
#define SAFE_FREE(ptr)                                                         \\
  if (ptr != NULL) {                                                           \\
    free(ptr);                                                                 \\
    ptr = NULL;                                                                \\
  }
typedef unsigned int UINT;
#define ll long long
typedef pair<int, int> P;

bool prime[100005];

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

    int ans = 0;

    REP(i, 100005) prime[i] = true;
    prime[0] = prime[1] = false;
    FOR(i, 2, (int)sqrt(100005)) {
      if (prime[i] == true) {
        FOR(j, 2, (int)sqrt(100005)) {
          if (prime[i * j] == true)
            prime[i * j] = false;
        }
      }
    }
    REP(i, n + 1) {
      if (prime[i] == true) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int prime[1000000];
  fill(prime, prime + 1000000, 1);
  prime[0] = prime[1] = 0;
  for (int i = 2; i * i < 1000000; i++) {
    if (prime[i])
      for (int j = i * 2; j < 1000000; j += i)
        prime[j] = 0;
  }

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

Error: 0

Fixed submission:

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

int main() {
  int prime[100000];
  fill(prime, prime + 100000, 1);
  prime[0] = prime[1] = 0;
  for (int i = 2; i * i <= 100000; i++) {
    if (prime[i])
      for (int j = i * 2; j <= 100000; j += i)
        prime[j] = 0;
  }

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

#include <iostream>
using namespace std;

int main() {
  int isPrime[1000000];
  for (int i = 2; i < 1000000; i++)
    isPrime[i] = 1;
  isPrime[0] = isPrime[1] = 0;
  for (int i = 2; i * i < 1000000; i++) {
    if (isPrime[i])
      for (int j = i * 2; j < 1000000; 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;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int furui(int N) {
  int i;
  int count = 0;
  int array[1000000];
  array[0] = false;
  array[1] = false;

  for (i = 2; i <= N; i++) {
    array[i] = true;
  }

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

  for (i = 2; i <= N; i++) {
    if (array[i] == true)
      count++;
  }

  return count;
}

int main() {
  int cnt = 0, n;
  while (cnt < 30) {
    cnt++;
    cin >> n;
    cout << furui(n) << endl;
  }

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

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

int main() {
  bool p[MAX + 1];
  int n, ans;

  for (int i = 0; i <= MAX; i++) {
    p[i] = (i <= 1) ? false : true;
  }

  for (int i = 2; i <= sqrt((long double)MAX) + 1; i++) {
    if (p[i]) {
      for (int j = i * 2; j <= MAX; j += i) {
        p[j] = false;
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

typedef long long ll;

int main() {
  ll n;
  vector<ll> pl;
  back_insert_iterator<vector<ll>> it(pl);

  while (cin >> n) {
    // ú»
    pl.clear();
    // nª2ÈºÌÆ«Ì
    if (n < 2) {
      cout << 0 << endl;
      continue;
    }
    // â¿Ìì¬
    *it = 2;
    for (ll i = 3; i <= sqrt((long double)n); i++) {
      for (ll t = 0; t < pl.size(); t++) {
        if (i % pl[t] == 0)
          break;
        if (t == pl.size() - 1)
          *it = i;
      }
    }
    // â¿É©¯é
    int L = pl.size();
    for (ll i = sqrt((long double)n) + 1; i <= n; i++) {
      for (ll t = 0; t < L; t++) {
        if (i % pl[t] == 0)
          break;
        if (t == L - 1)
          *it = i;
      }
    }
    cout << pl.size() << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

bool is_prime(const unsigned n);

int main() {
  int n;

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

    for (int number = 2; number <= n; number++) {
      if (is_prime(number))
        ans++;
    }
    printf("%d\\n", ans);
  }

  return 0;
}

bool is_prime(const unsigned n) {
  switch (n) {
  case 0:
  case 1:
    return false;
  case 2:
  case 3:
    return true;
  }

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

  if (n % 6 != 1 && n % 6 != 5)
    return false;

  for (unsigned i = 5; i * i <= n; i += 6) {
    if (n % i == 0)
      return false;
    if (n % (i + 2) == 0)
      return false;
  }

  return true;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

bool is_prime(const unsigned n);

int main() {
  int n;

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

    for (int number = 2; number <= n; number++) {
      if (is_prime(number))
        ans++;
    }
    printf("%d\\n", ans);
  }

  return 0;
}

bool is_prime(const unsigned n) {
  if (n == 2)
    return true;

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

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

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

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n;
  int cont;
  while (scanf("%d", &n) != EOF) {
    int a[n];
    for (int i = 0; i < n; i++) {
      a[i] = 1; // 1 素数　０素数ではない
    }
    a[0] = 0;
    cont = 0;
    for (int i = 2; i <= sqrt(n); i++) {
      if (a[i - 1] == 1) {
        for (int j = 2; i * j <= n; j++) {
          a[i * j - 1] = 0;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (a[i] == 1)
        cont++;
    }
    cout << cont << endl;
  }
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n;
  int cont;
  while (scanf("%d", &n) != EOF) {
    int a[1000000] = {0};
    int cont = 0;
    a[1] = 1;
    for (int i = 2; i <= sqrt(n); i++) {
      if (a[i] == 0) {
        for (int j = 2; j <= n; j++) {
          a[i * j] = 1;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      if (a[i] == 0)
        cont++;
    }
    cout << cont << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define MAX_N 1000000
using namespace std;

int prime[MAX_N];
bool is_prime[MAX_N + 1];

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

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int ans, n;
  while (cin >> n) {
    for (int i = 2; i < n + 1; i++) {
      for (int j = 2; j < i + 1; j++) {
        if (i == j) {
          ans++;
          break;
        }
        if (i % j == 0) {
          break;
        }
      }
    }
    cout << ans << endl;
    ans = 0;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

int prime[MAX_N];
bool is_prime[MAX_N + 1];
int sieve(const 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;
}

Error: 0

Fixed submission:

// 艾氏&#31579;法
#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;
  // 其&#23454;可以&#20026;sqrt(n)
  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;
}
Problem p00009 (C++) — buggy submission, verdict: 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 = 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;
}

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

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n;
  bool a[1000000] = {false};
  for (int i = 2; i < 1000; i++) {
    bool t = false;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0)
        t = true;
    }
    if (i == 2)
      t = false;
    if (t == false) {
      for (int j = i * 2; j < 1000000; j += i)
        a[j] = true;
      a[i] = false;
    }
  }
  while (cin >> n) {
    int count = 0;
    for (int i = 0; i <= n; i++) {
      if (a[i] == false)
        count++;
    }
    cout << count - 2 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main() {
  int n, a[9];
  a[0] = 9592;
  a[1] = 17984;
  a[2] = 25997;
  a[3] = 33860;
  a[4] = 41538;
  a[5] = 49098;
  a[6] = 56543;
  a[7] = 63951;
  a[8] = 71274;
  while (cin >> n) {
    int count = 0;
    int i = 0;
    while (true) {
      if ((i + 1) * 100000 <= n)
        count = a[i];
      else
        break;
      i++;
    }
    if (n < 100000) {
      count = 1;
      for (int j = 3; j <= n; j++) {
        bool t = false;
        for (int k = 2; k < 1 + (double)sqrt((double)j); k++) {
          if (j % k == 0)
            t = true;
        }
        if (t == false)
          count++;
      }
    } else {
      for (int j = (i)*100000 + 1; j <= n; j++) {
        bool t = false;
        for (int k = 2; k < 1 + (double)sqrt((double)j); k++) {
          if (j % k == 0)
            t = true;
        }
        if (t == false)
          count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: 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 * j <= i; j++) {
      if (i % j == 0) {
        sum[i]--;
        break;
      }
    }
  }
}
int main() {
  init();
  int n;
  while (cin >> n && n) {
    cout << sum[n] << endl;
  }
}


Error: TLE

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 < 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, verdict: Time Limit Exceeded

#include <iostream>
#define N 1000000
using namespace std;
void PrimeNum(int n, bool Primeif[]) {
  for (int i = 2; i * i < n; i++) {
    if (Primeif[i] == true) {
      for (int k = 2; k * i < n; k++) {
        Primeif[k * i] = false;
      }
    }
  }
}
int main() {
  int n, num;
  bool Primeif[N + 1];
  Primeif[1] = false;
  for (int i = 2; i < N; i++) {
    Primeif[i] = true;
  }
  PrimeNum(N + 1, Primeif);

  while (cin >> n) {
    num = 0;
    for (int i = 0; i <= n; i++) {
      if (Primeif[i] == true) {
        num = num + 1;
      }
    }
    cout << num << endl;
  }
}


Error: TLE

Fixed submission:

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

#include <cmath>
#include <iostream>

const int LIMIT = 1000000;

void eratosthenes(int n, bool *primes);

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  bool primes[LIMIT];
  primes[0] = primes[1] = false;
  for (int i = 2; i < LIMIT; ++i)
    primes[i] = true;
  eratosthenes(LIMIT, primes);

  int num_of_primes[LIMIT] = {};
  for (int i = 1; i < LIMIT; ++i) {
    num_of_primes[i] = num_of_primes[i - 1];
    if (primes[i])
      ++num_of_primes[i];
  }

  int n;
  while (std::cin >> n) {
    std::cout << num_of_primes[n] << std::endl;
  }
}

void eratosthenes(int n, bool *primes) {
  double root_n = std::sqrt((double)n);
  for (int i = 2; i <= root_n; ++i)
    if (primes[i])
      for (int j = i * i; j <= n; j += i)
        primes[j] = false;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

bool eratosthenes(int n);

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n;
  while (std::cin >> n) {
    int cnt = 0;
    for (int i = 2; i <= n; ++i)
      if (eratosthenes(i))
        ++cnt;
    std::cout << cnt << std::endl;
  }
}

bool eratosthenes(int n) {
  if (n == 2)
    return true;
  if (n < 2 || n % 2 == 0)
    return false;
  int i = 3;
  double root_n = std::sqrt((double)n);
  while (i <= root_n) {
    if (n % i == 0)
      return false;
    i += 2;
  }
  return true;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int eratos(int n) {
  bool isprime[n + 1];
  for (int i = 0; i <= n; i++)
    isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i * i <= n; i++) {
    if (isprime[i]) {
      int j = i + i;
      while (j <= n) {
        isprime[j] = false;
        j = j + i;
      }
    }
  }
  int counter = 0;
  for (int i = 0; i <= n; i++) {
    if (isprime[i] == true)
      counter++;
  }
  return counter;
}

int main() {
  int n;
  while (cin >> n) {
    cout << eratos(n) << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  while (cin >> n) {
    int counter = 0;
    if (n != 1)
      counter++;
    for (int i = 3; i <= n; i += 2) {
      for (int j = 2; j * j <= i; j++) {
        if (i % j == 0)
          goto divisible;
      }
      counter++;
    divisible:;
    }
    cout << counter << endl;
  }
}