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

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

#define rep(i, n) for (int i = 2; i <= n; i++)
#define rep2(i, n) for (int i = 0; i < n; i++)
bool isprime(int n) {
  rep(i, sqrt(n)) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int main() {
  int n, ans;
  bool table[1000000];
  rep2(i, 1000000) table[i] = true;
  rep(i, 1000000) {
    if (table[i] && isprime(i)) {
      rep(j, 999999 / i) table[i * j] = false;
    }
  }
  while (cin >> n) {
    ans = 0;
    rep(i, n) if (table[i]) ans++;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 2; i <= n; i++)
bool isprime(long long int n) {
  rep(i, sqrt(n)) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int main() {
  long long int n, ans;
  while (cin >> n) {
    ans = 0;
    rep(i, n) if (isprime(i)) ans++;
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int primes[1000010];
bool isPrime(int p) {
  bool flag = true;
  for (int i = 2; i * i <= p; i++) {
    if (p % i == 0) {
      flag = false;
      break;
    }
  }
  return flag;
}
int main() {
  int n;
  for (int i = 0; i <= 999999; i++)
    primes[i] = 0;
  for (int i = 2; i <= 999999; i++)
    if (isPrime(i))
      primes[i] = 1;
  for (int i = 0; i <= 999999; i++)
    primes[i + 1] += primes[i];
  while (cin >> n)
    cout << primes[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool isPrime(int p) {
  bool flag = true;
  for (int i = 2; i * i <= p; i++) {
    if (p % i == 0) {
      flag = false;
      break;
    }
  }
  return flag;
}
int main() {
  int n;
  while (cin >> n) {
    int count = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime(i))
        count++;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  const int numPrimes = 1000000;
  bool primes[numPrimes];
  for (int i = 0; i != numPrimes; ++i) {
    primes[i] = true;
  }

  for (int i = 2; i != sqrt(numPrimes); ++i) {
    for (int j = 2; i * j < numPrimes; ++j) {
      primes[i * j] = false;
    }
  }

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

Error: 0

Fixed submission:

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

int main() {
  vector<int> primes;
  primes.push_back(2);
  primes.push_back(3);
  for (int n = 4; n != 999; ++n) {
    bool isPrime = true;
    for (int i = 0; i != primes.size(); ++i) {
      if (n % primes[i] == 0) {
        isPrime = false;
      }
    }
    if (isPrime) {
      primes.push_back(n);
    }
  }

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

  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: 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;

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

Error: 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;

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

#include <cstdio>

int Eratos[1000000];

int main() {
  Eratos[0] = Eratos[1] = 0;
  for (int i = 2; i < 1000000; i++) {
    Eratos[i] = 1;
  }
  for (int i = 2; i < 1000; i++) {
    if (Eratos[i]) {
      for (int j = 2 * i; j < 1000000; j += i) {
        Eratos[j] = 0;
      }
    }
  }
  int n, res;
  res = 1;
  for (int i = 2; i < 1000000; i++) {
    if (Eratos[i]) {
      Eratos[i] = res;
      res++;
    }
  }
  while (scanf("%d", &n) != EOF) {
    res = 0;
    while (!Eratos[n]) {
      n--;
      if (n == 0)
        break;
    }
    if (n)
      printf("%d\\n", Eratos[n]);
    else
      printf("0\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

int Eratos[1000000];

int main() {
  Eratos[0] = Eratos[1] = 0;
  for (int i = 2; i < 1000000; i++) {
    Eratos[i] = 1;
  }
  for (int i = 2; i < 1000; i++) {
    if (Eratos[i]) {
      for (int j = 2 * i; j < 1000000; j += i) {
        Eratos[j] = 0;
      }
    }
  }
  int n, res;
  res = 1;
  for (int i = 2; i < 1000000; i++) {
    if (Eratos[i]) {
      Eratos[i] = res;
      res++;
    }
  }
  while (scanf("%d", &n)) {
    res = 0;
    while (!Eratos[n]) {
      n--;
    }
    printf("%d\\n", Eratos[n]);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int prime(int n) {
  if (n < 2) {
    return 0;
  } else if (n == 2) {
    return 1;
  }

  if (n % 2 == 0) {
    return 0;
  }

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

  return 1;
}

int main(int argc, char **argv) {
  int n, c, i;
  int a[10] = {0, 9592, 17984, 25997, 33860, 41538, 49098, 56543, 63951, 71274};
  while (cin >> n) {
    i = 0;
    c = 0;
    for (int j = 900000; j >= 100000; j -= 100000) {
      if (j < n) {
        i = j + 1;
        c = a[j / 100000];
        break;
      }
    }

    for (; i <= n; i++) {
      if (prime(i) == 1) {
        c++;
      }
    }

    cout << c << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int prime(int n) {
  if (n < 2) {
    return 0;
  } else if (n == 2) {
    return 1;
  }

  if (n % 2 == 0) {
    return 0;
  }

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

  return 1;
}

int main(int argc, char **argv) {
  int n, c;

  while (cin >> n) {
    c = 0;
    for (int i = 2; i <= n; i++) {
      if (prime(i) == 1) {
        c++;
      }
    }

    cout << c << endl;
  }

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

#include <cmath>
#include <iostream>
using namespace std;
int prime(int);
int main() {
  int a, sum;
  int b[999999];
  b[0] = 0;
  for (int i = 1; i < 999999; i++) {
    b[i] = prime(i + 1);
  }
  while (cin >> a) {
    sum = 0;
    for (int i = 1; i < a; i++) {
      sum += b[i];
    }
    cout << sum << endl;
  }
}
int prime(int x) {
  if (x == 1)
    return 0;
  if (x == 2)
    return 1;
  for (int i = 2; i <= sqrt(x) + 1; i++) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int prime(int);
int main() {
  int a, n;
  n = 0;
  while (cin >> a) {
    for (int i = 2; i < a; i++) {
      if (prime(i) == 0) {
        n++;
      }
    }
    cout << n << endl;
  }
}

int prime(int x) {
  if (x == 2 || x == 3 || x == 5 || x == 7)
    return 0;
  if (x % 2 == 0 || x % 3 == 0)
    return 1;
  for (int i = 5; i <= sqrt(x) + 1; i += 6) {
    if (x % i == 0 || x % (i + 2) == 0)
      return 1;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int n;

  while (1) {
    cin >> n;
    if (cin.eof()) {
      break;
    }
    int m = (int)sqrt(n);

    vector<bool> h(n + 1, true);
    h[0] = false;
    h[1] = false;

    for (int i = 2; i <= m; i++) {
      if (h.at(i)) {
        for (int s = 2; s * i < h.size(); s++) {
          if (h.at(s * i))
            h[s * i] = false;
        }
      }
    }

    int ans = 0;
    for (auto f : h) {
      if (f)
        ans++;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>

using namespace std;

int main() {
  int n;

  while (!cin.eof()) {
    cin >> n;
    list<int> search_list;
    list<int> prime_number;
    while (n >= 2) {
      if (n % 2 != 0)
        search_list.push_front(n);
      n--;
    }
    if (!search_list.empty())
      prime_number.push_front(2);
    while (!search_list.empty()) {
      int m = search_list.front();
      prime_number.push_back(m);
      search_list.pop_front();
      list<int>::iterator it = search_list.begin();
      while (it != search_list.end()) {
        if ((*it % m) == 0) {
          it = search_list.erase(it);
          continue;
        }
        it++;
      }
    }
    cout << prime_number.size() << endl;
    prime_number.clear();
    search_list.clear();
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
bool b[1000000];

int main(void) {
  int a, n, i;
  int count = 0;
  int f = 1;

  for (i = 3; i < 1000000; i += 2) {
    f = 1;
    for (a = 2; a * a <= i; a++) {
      if (i % a == 0) {
        f = 0;
        break;
      }
    }
    if (f == 1) {
      b[i] = 1;
    }
  }
  b[2] = 1;

  while (scanf("%d", &n) != EOF) {
    for (i = 0; i <= n; i++) {
      if (b[i] == 1)
        count++;
    }

    printf("%d\\n", count);
    count = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
bool b[4000000];

int main(void) {
  int a, n, i;
  int count = 0;
  int f = 1;

  for (i = 3; i < 4000000; i += 2) {
    f = 1;
    for (a = 2; a * a <= i; a++) {
      if (i % a == 0) {
        f = 0;
        break;
      }
    }
    if (f == 1) {
      b[i] = 1;
    }
  }
  b[2] = 1;

  while (scanf("%d", &n) != EOF) {
    for (i = 0; i < n; i++) {
      if (b[i] == 1)
        count++;
    }

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

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

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

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
bool prime(int);
int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    if (n >= 2)
      cnt++;
    for (int i = 3; i <= n; i += 2) {
      if (prime(i)) {
        cnt++;
      }
    }
    cout << cnt << endl;
  }
  return 0;
  cin >> n;
}

bool prime(int n) {
  for (int i = 3; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <stdio.h>

using namespace std;

#define REP(a, i, n) for (int i = a; i <= n; ++i)

const int MAX_N = 1e6 - 1;
bool memo[MAX_N + 1];

bool isPrime(int n) {
  if (memo[n])
    return true;

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

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

  memo[n] = true;
  return true;
}

//  n よりも小さい素数の数は何個存在するか
int main() {

  REP(0, i, MAX_N) { memo[i] = false; }

  int n = 0; // 999,999
  while (scanf("%d", &n) != EOF) {
    int cnt = 0;
    REP(1, i, n) {
      if (isPrime(i))
        cnt++;
    }

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

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <stdio.h>

using namespace std;

#define REP(a, i, n) for (int i = a; i <= n; ++i)

bool isPrime(int n) {
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  if (n % 2 == 0)
    return false;
  if (n == 3)
    return true;
  if (n % 3 == 0)
    return false;

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

//  n よりも小さい素数の数は何個存在するか
int main() {

  int n = 0; // 999,999
  while (scanf("%d", &n) != EOF) {
    int cnt = 0;
    REP(1, i, n) {
      if (isPrime(i))
        cnt++;
    }

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

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

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

int main() {
  vector<bool> hantei(1000000, true);
  vector<int> sosuu(1000000);
  int s = 0;
  for (int a = 2; a < 1000000; a++) {
    if (hantei[a]) {
      s++;
      for (int b = a * 2; b < 1000000; b += a)
        hantei[b] = false;
    }
    sosuu[a] = s;
  }
  int c;
  while (cin >> c) {
    cout << sosuu[c] << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  vector<bool> hantei(1000000, true);
  vector<int> sosuu(1000000);
  int s = 0;
  for (int a = 2; a < 1000000; a++) {
    if (hantei[a]) {
      s++;
      for (int b = a * 2; b < 1000000; b += a)
        hantei[b] = false;
    }
    sosuu[a] = s;
  }
  int c;
  while (cin >> c, c) {
    cout << sosuu[c] << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

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

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

  for (int i = 1; i < (int)sqrt(1000000); i++) {
    if (prime[i] == true)
      for (int j = i + 1; (i + 1) * j <= 1000000; j++)
        prime[(i + 1) * j - 1] = false;
  }

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  long cnt;
  bool flag;

  while (cin >> n) {
    if (n < 2) {
      cout << "0\\n";
      continue;
    } else if (n == 2) {
      cout << "1\\n";
      continue;
    } else if (n == 3) {
      cout << "2\\n";
      continue;
    }

    cnt = 2;
    for (int i = 5; i <= n; i += 2) {
      flag = false;
      for (int j = 3; j * j <= n; j += 2) {
        if (!(i % j)) {
          flag = true;
          break;
        }
      }
      if (!flag)
        cnt++;
    }
    cout << cnt << endl;
  }

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

#include <iostream>
using namespace std;

int main() {
  bool is_prime[1000000];
  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 n[1000000];
  int count = 0;
  for (int i = 0; i < 1000000; i++) {
    if (is_prime[i])
      count++;
    n[i] = count;
  }
  while (true) {
    int x;
    cin >> x;
    if (cin.eof())
      break;
    cout << n[x] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

bool isPrime(int x) {
  if (x == 2) {
    return true;
  }
  if (x < 2 || x % 2 == 0) {
    return false;
  }
  int i = 3;
  while (i * i <= x) {
    if (x % i == 0) {
      return false;
    }
    i += 2;
  }
  /*for (int i=3; i*i<=x; i+=2) {
   if (x%i==0) {
   return false;
   }
   }*/
  return true;
}
int main() {
  int n;
  int cnt;
  while (cin >> n) {
    cnt = 0;
    for (int i = 0; i <= n; i++) {
      if (isPrime(i)) {
        cnt++;
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int SUUZI[1000000];

int a = 0, b, d, f, n, c = 0;
double buffer;

int main() {

  for (d = 0; d < 1000000; d++) {
    SUUZI[d] = 1;
  };

  for (a = 3; a < 1000000; a++) {
    buffer = a;
    for (b = 2; 1; b++) {
      if (a % b == 0) {
        SUUZI[a] = 0;
        break;
      };
      if (b > sqrt(buffer)) {
        SUUZI[a] = 1;
        break;
      };
    };
  };

  while (cin >> n) {
    c = 0;
    for (d = 0; d <= n; d++) {
      c += SUUZI[d];
    };

    cout << c - 2 << endl;
  };
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n = 0, m = 0, o = 0, f = 0, c = 0;

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

    for (o = 2; o <= n; o++) {
      f = 0;
      for (m = 2; m < o / 2; m++) {
        if (o % m == 0) {
          f++;
        }
      };
      if (f == 0) {
        c++;
      };
    };
    cout << c << endl;
  };
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 999999999
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define MOD(x) (x % (mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

bool debug = false;
/*---------------------------------------------------*/

bool prime[1000000];
int ans[1000000];

void is_prime() {
  rep(i, 1000000) prime[i] = true;
  prime[0] = prime[1] = false;
  REP(i, 2, 1000000) {
    if (prime[i] == true) {
      for (int j = i + i; j < 1000000; j += i)
        prime[j] = false;
    }
  }
  if (debug)
    rep(i, 1000) if (prime[i]) cout << i << endl;
}

void calc() {
  ans[0] = 0;
  REP(i, 1, 1000000) { ans[i] = ans[i - 1] + prime[i]; }
}

int main() {
  int n;
  is_prime();
  calc();
  while (cin >> n) {
    cout << ans[n] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 999999999
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define MOD(x) (x % (mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

bool debug = false;
/*---------------------------------------------------*/

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

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

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int n, ct = 0;
bool p[1000001];

int main() {
  for (int i = 0; i < 1000001; i++)
    p[i] = 1;
  for (int i = 2; i < 1000; i++) {
    if (p[i] == 1) {
      for (int j = 0; i * (j + 2) < 1000000; j++) {
        p[i * (j + 2)] = 0;
      }
    }
  }
  while (cin >> n) {
    for (int i = 2; i < n + 1; i++)
      if (p[i] == 1)
        ct++;
    cout << ct << endl;
    ct = 0;
  }
}

Error: TLE

Fixed submission:

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

int count(int n);

int main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n) {
      break;
    }
    cout << count(n) << endl;
  }
  return 0;
}

int count(int n) {
  int ct = 0;
  bool *prime = new bool[n];
  // bool prime[999999];
  for (int i = 0; i <= n; i++) {
    prime[i] = 0;
  }
  prime[2] = 1;
  for (int i = 3; i <= n; i = i + 2) {
    prime[i] = 1;
  }
  for (int i = 3; i <= n; i = i + 2) {
    if (prime[i] == 0)
      continue;
    for (int j = i * 2; j <= n; j = j + i) {
      prime[j] = 0;
    }
  }
  for (int i = 3; i <= n; i = i + 2) {
    if (prime[i] == 1)
      ct++;
  }
  ct++; // 2を数えていない分
  return ct;
  delete[] prime;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits //stdc++.h>
using namespace std;
int a[1000500];
int main() {
  for (int i = 2; i * i < 1000500; i++) {
    for (int j = i * 2; j < 1000500; j += i) {
      a[j] = 1;
    }
  }
  int b;
  while (cin >> b) {
    int c = 0;
    for (int i = 2; i <= b; i++) {
      if (a[i] == 0)
        c++;
    }
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits //stdc++.h>
using namespace std;
int main() {
  int a[1000000];
  a[0] = 1;
  a[1] = 1;
  for (int i = 2; i < 1000000;) {
    if (a[i] == 0) {
      for (int j = i; j < 1000000; j += i) {
        a[j] = 1;
      }
    }
  }
  int b;
  while (cin >> b) {
    int c = 0;
    for (int i = 0; i < b; i++) {
      if (a[i] > b)
        break;
      else {
        if (a[i] == 1)
          c++;
      }
    }
    cout << c << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  char p[1000000];
  for (int i = 2; i < 1000000; i++)
    p[i] = 1;
  for (int i = 2; i < 1000; i++) {
    if (p[i] == 0)
      continue;
    for (int j = i + 1; j < 1000000; j++) {
      if (p[j] == 0)
        continue;
      if (j % i == 0)
        p[j] = 0;
    }
  }

  int n, c;
  while (scanf("%d", &n) != EOF) {
    c = 0;
    for (int i = 0; i <= n; i++)
      if (p[i] == 1)
        c++;
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {

  int p[168], l = 0, n;

  for (int i = 2; i < 1000; i++) {
    int k = 0;
    for (int j = 2; j <= sqrt(i); j++)
      if (i % j == 0 && i / j != 1)
        k = 1;
    if (k == 0) {
      p[l] = i;
      l++;
    }
  }

  while (cin >> n) {
    l = 0;
    for (int i = 2; i <= n; i++) {
      int k = 0;
      for (int j = 0; j < 168; j++)
        if (i % p[j] == 0 && i / p[j] != 1)
          k = 1;
      if (k == 0)
        l++;
    }
    cout << l << endl;
  }

  return 0;
}