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

#include <array>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>

int main() {
  std::array<char, 1000000> is_prime_table = {0, 0, 1};
  for (int i = 3; i <= is_prime_table.size(); i += 2) {
    if ([&i]() -> bool {
          for (int j = 3; j <= std::sqrt(i); j += 2)
            if (i % j == 0)
              return false;
          return true;
        }())
      is_prime_table[i] = 1;
  }

  std::string line;
  while (std::getline(std::cin, line)) {
    int const num = std::atoi(line.c_str());
    int result = num > 1 ? 1 : 0; // 2

    for (int i = 3; i <= num; i += 2)
      if (is_prime_table[i] == 1)
        ++result;

    std::cout << result << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>

int main() {
  std::string line;
  while (std::getline(std::cin, line)) {
    int const num = std::atoi(line.c_str());
    int result = num > 1 ? 1 : 0; // 2

    for (int i = 3; i <= num; i += 2) {
      if ([&i]() -> bool {
            for (int j = 3; j <= std::sqrt(i); j += 2)
              if (i % j == 0)
                return false;
            return true;
          }())
        ++result;
    }
    std::cout << result << std::endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

#define MAX_NUM (1000000)

int main(void) {
  static bool searchList[MAX_NUM];
  static bool isPrime[MAX_NUM];
  static int primeNum[MAX_NUM];

  double maxSq = sqrt((double)(MAX_NUM - 1));

  for (int i = 2; i <= maxSq + 1; i++) {
    if (!searchList[i]) {
      isPrime[i] = true;
      for (int j = 2; j < MAX_NUM; j++) {
        if (j % i == 0) {
          searchList[j] = true;
        }
      }
    }
  }

  for (int i = maxSq + 2; i < MAX_NUM; i++) {
    if (!searchList[i]) {
      isPrime[i] = true;
    }
  }

  for (int i = 2; i < MAX_NUM; i++) {
    primeNum[i] = isPrime[i] ? primeNum[i - 1] + 1 : primeNum[i - 1];
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstring>
#include <iostream>

#define MAX_NUM (1000000)

static int getPrimeNum(int maxVal);

int main(void) {
  int n;

  while (std::cin >> n) {
    std::cout << getPrimeNum(n) << std::endl;
  }

  return 0;
}

static int getPrimeNum(int maxVal) {
  static int searchList[MAX_NUM] = {0};
  static int primeList[MAX_NUM] = {0};

  memset(searchList, 0, sizeof(int) * MAX_NUM);
  memset(primeList, 0, sizeof(int) * MAX_NUM);

  double sqMaxVal = sqrt((double)maxVal) + 1;

  for (int i = 2; i <= sqMaxVal; i++) {
    if (searchList[i] == 0) {
      searchList[i] = 1;
      primeList[i] = 1;
      for (int j = i + 1; j <= maxVal; j++) {
        if (j % i == 0) {
          searchList[j] = 1;
        }
      }
    }
  }

  for (int i = 2; i <= maxVal; i++) {
    if (searchList[i] == 0) {
      primeList[i] = 1;
    }
  }

  int count = 0;
  for (int i = 2; i <= maxVal; i++) {
    if (primeList[i] == 1) {
      count++;
    }
  }

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

#include <iostream>
using namespace std;

int isPrime(int n);

int dp[1000000] = {};

int main() {
  int n;
  for (int i = 2; i <= 1000000; i++) {
    if (isPrime(i))
      dp[i] = dp[i - 1] + 1;
    else
      dp[i] = dp[i - 1];
  }

  while (cin >> n) {
    cout << dp[n] << endl;
  }
}

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int isPrime(int n);

int main() {
  int n, out;
  while (cin >> n) {
    out = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime(i))
        out++;
    }
    cout << out << endl;
  }
}

int isPrime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return 0;
  }
  return 1;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int array[1000000];
  int n, count = 0;
  for (int i = 0; i < 1000000; i++) {
    array[i] = 1;
  }
  array[0] = 0;
  array[1] = 0;
  for (int i = 2; i < 1000000; i++) {
    if (array[i] != 0) {
      for (int j = i * 2; j < 1000000; j += i) {
        array[j] = 0;
      }
    }
  }
  while (cin >> n) {
    for (int i = 0; i <= n; i++) {
      count += array[i];
    }
    cout << count << endl;
    count = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int n, count = 0;
  double l, m;
  while (cin >> n) {
    for (int i = 2; i <= n; i++) {
      count++;
      int j;
      l = sqrt(i * 1.0);
      m = l;
      for (j = 2; j <= m; j++) {
        if (i % j == 0) {
          count--;
          break;
        }
      }
    }
    cout << count << endl;
    count = 0;
  }

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

#include <cmath>
#include <iostream>
using namespace std;
#define N 1000001
bool prime[N];
int cnt[N];
int main() {
  for (int i = 0; i < N; i++)
    prime[i] = true;
  int n = sqrt(N);
  prime[0] = prime[1] = false;
  for (int i = 2; i <= n; i++) {
    if (prime[i]) {
      for (int j = i + i; j <= N; j += i)
        prime[j] = false;
    }
  }
  cnt[0] = cnt[1] = 0;
  for (int i = 2; i < N; i++) {
    if (prime[i])
      cnt[i] = cnt[i - 1] + 1;
    else
      cnt[i] = cnt[i - 1];
  }
  int x;
  while (cin >> x) {
    cout << cnt[x] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#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 x;
  while (cin >> x) {
    int cnt = 0;
    for (int i = 2; i <= x; i++) {
      if (isprime(i))
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

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

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  int i, k, x, cnt[1000001], data[1000000];
  cnt[0] = 0;
  data[0] = 0;
  data[1] = 0;
  for (i = 2; i <= 1000000; i++) {
    data[i] = 1;
  }
  for (i = 2; i * i <= 1000000; i++) {
    if (data[i]) {
      for (k = 2 * i; k <= 1000000; k += i) {
        data[k] = 0;
      }
    }
  }
  for (i = 0; i <= 1000000; i++) {
    cnt[i + 1] = cnt[i] + data[i];
  }
  while (1) {
    scanf("%d", &x);
    if (x == 0)
      break;
    printf("%d\\n", cnt[x]);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

int eratos(int n) {
  int cnt = 0;
  vector<bool> is_prime(n + 1, true);
  is_prime[0] = is_prime[1] = false;
  for (auto i = 2; i <= n; i++) {
    if (is_prime[i]) {
      ++cnt;
      for (auto j = 2 * i; j <= n; j += i)
        is_prime[j] = false;
    }
  }
  return cnt;
}

int main() {
  for (int n; cin >> n; cout << eratos(n) << endl)
    ;
  return 0;
}

Error: 0

Fixed submission:

#include <forward_list>
#include <iostream>
#include <numeric>
using namespace std;

int eratos(forward_list<int> xs) {
  if (xs.empty())
    return 0;
  int head = xs.front();
  xs.pop_front();
  xs.remove_if([&](int n) { return !(n % head); });
  return 1 + eratos(xs);
}

int main() {
  int n;
  while (cin >> n) {
    forward_list<int> ls(n - 1);
    iota(begin(ls), end(ls), 2);
    cout << eratos(ls) << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

int main() {
  int n;
  int prime[1000000] = {};
  for (int i = 2; i <= 1000000; i++) {
    if (IsPrime(i))
      prime[i] = prime[i - 1] + 1;
    else
      prime[i] = prime[i - 1];
  }

  while (cin >> n)
    cout << prime[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int main(void) {
  int n;
  while (cin >> n) {
    int count = 1;

    for (int i = 3; i <= n; i = i + 2) {
      if (IsPrime(i))
        count++;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <math.h>

using namespace std;

int A[1000000];
int Eratosthenes(int n) {
  int mama = 0;
  for (int i = 0; i < n; i++) {
    A[i] = 1;
  }
  for (int i = 2; i < sqrt(n); i++) {
    if (A[i]) {
      for (int j = 0; i * (j + 2) < n; j++) {
        A[i * (j + 2)] = 0;
      }
    }
  }

  for (int i = 2; i < n; i++) {
    if (A[i]) {
      mama++;
    }
  }
  return mama;
}

int main() {
  int n;

  while ((scanf("%d", &n)) != -1) {
    printf("%d\\n", Eratosthenes(n + 1));
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int A[1000000];
int Eratosthenes(int n) {
  int mama = 0;
  for (int i = 0; i < n; i++) {
    A[i] = 1;
  }
  for (int i = 2; i < sqrt(n); i++) {
    if (A[i]) {
      for (int j = 0; i * (j + 2) < n; j++) {
        A[i * (j + 2)] = 0;
      }
    }
  }

  for (int i = 2; i < n; i++) {
    if (A[i]) {
      mama++;
    }
  }
  return mama;
}

int main() {
  int n;
  cin >> n;
  while (n != -1) {
    cout << Eratosthenes(n) << endl;
    cin >> n;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

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

  int i, j;
  data[0] = data[1] = 0;
  for (i = 2; i < 1000000; i++) {
    data[i] = 1;
  }

  for (i = 0; i < 1000000; i++) {
    if (data[i] == 1) {
      for (j = i + i; j < 1000000; j = j + i) {
        data[j] = 0;
      }
    }
  }

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

Error: 0

Fixed submission:

#include <stdio.h>

int main() {
  int n, i, j;
  bool c[100000];
  int data[100000];
  int count;
  int premaxin = 1;

  for (i = 0; i < 100000; i++) {
    c[i] = 1;
    data[i] = 0;
  }
  c[0] = c[1] = 0;

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

    if (premaxin < n) {
      for (i = 0; i <= n; i++) {
        if (c[i] == 1) {
          if (premaxin > i) {
            j = premaxin;
          } else {
            j = i + 1;
          }
          for (; j <= n; j++) {
            if (j % i == 0) {
              c[j] = 0;
            }
          }
        }
      }

      count = data[premaxin];
      for (i = premaxin + 1; i <= n; i++) {

        if (c[i] == 1) {
          count++;
        }
        data[i] = count;
      }
    }

    printf("%d\\n", data[n]);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 1000010

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

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

Error: 0

Fixed submission:

#include <stdio.h>
#define MAX 1000 // 1000010

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

  int n;
  while ((scanf("%d", &n)) != EOF) {
    printf("%d\\n", prime_under_num[n]);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>

using namespace std;

void set_prime_number(bool *array, int num) {

  fill(array, array + num, true);
  array[0] = array[1] = false;
  for (int i = 2; i < num; i++) {
    if (!array[i])
      continue;
    for (int j = i * 2; j < num; j += i) {
      array[j] = false;
    }
  }
}

int main() {
  bool prime_number[1000000];
  int num;
  int nump = 0;
  int cnt = 0;

  set_prime_number(prime_number, 1000000);
  while (scanf("%d", &num) != EOF) {
    for (int i = 0; i <= num; i++) {
      if (prime_number[i]) {
        cnt++;
      }
    }
    printf("%d\\n", cnt);
    cnt = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>

using namespace std;

void set_prime_number(bool *array, int num) {

  fill(array, array + num, true);
  array[0] = array[1] = false;
  for (int i = 2; i < num; i++) {
    if (!array[i])
      continue;
    for (int j = i * 2; j < num; j += i) {
      array[j] = false;
    }
  }
}

int main() {
  bool prime_number[999999];
  int num;
  int nump = 0;
  int j = 0;
  int cnt = 0;

  while (scanf("%d", &num) != EOF) {
    set_prime_number(prime_number, num);
    for (int i = 0; i < num; i++) {
      if (prime_number[i])
        cnt++;
    }
    printf("%d ", cnt);
    cnt = 0;
  }

  while (1) {
    printf("aaaa");
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

#define MAX 1200000
using namespace std;
int prim[1200100];
int prima[1200100];
int main() {
  int n, i, x = 0;
  for (i = 2; i <= MAX; i++) {
    prim[i] = i;
  }
  for (i = 2; i <= MAX; i++) {
    if (prim[i] != 0) {
      prima[x] = prim[i];
      x += 1;
      int j = i;
      while (j <= MAX) {
        prim[j] = 0;
        j += i;
      }
    }
  }
  while (scanf("%d", &n) != EOF) {
    int a = 0;
    while (prima[a] <= n) {
      a++;
    }
    printf("%d\\n", a);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

using namespace std;
int prim[1000100];
int prima[1000100];

int main() {
  int n, i, x = 0;
  for (i = 2; i <= 1000000; i++) {
    prim[i] = i;
  }
  for (i = 2; i <= 1000000; i++) {
    if (prim[i] != 0) {
      prima[x] = prim[i];
      x += 1;
      int j = i;
      while (j <= 1000000) {
        prim[j] = 0;
        j += i;
      }
    }
  }
  while (scanf("%ld", &n) != EOF) {
    int a = 0;
    while (prima[a] <= n) {
      a++;
    }
    printf("%ld\\n", a);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, cnt;
  bool prime[1000000] = {false, false};

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

  for (int i = 2; i <= sqrt(1000000); i++) {
    if (prime[i]) {
      for (int j = 2 * i; j <= 1000000; j++) {
        if (j % i == 0) {
          prime[j] = false;
        }
      }
    }
  }

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

    for (int i = 2; i <= n; i++) {
      if (prime[i]) {
        cnt++;
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, cnt;
  bool prime[1000000];
  prime[0] = prime[1] = false;

  for (int i = 2; i < 100000; i++) {
    if (i % 2 == 0) {
      prime[i] = false;
    } else {
      prime[i] = true;
    }
  }

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

    for (int i = 2; i <= sqrt(n); i++) {
      if (prime[i]) {
        for (int j = 2 * i; j <= n; j++) {
          if (j % i == 0) {
            prime[j] = false;
          }
        }
      }
    }

    for (int i = 2; i <= n; i++) {
      if (prime[i]) {
        cnt++;
      }
    }

    cout << cnt << endl;
  }

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

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

bool isprime(int number, std::vector<int> const &primeset) {
  int maxprime = static_cast<int>(std::sqrt(static_cast<float>(number)));

  typedef std::vector<int>::const_iterator Iter;
  Iter primesetlimit =
      std::upper_bound(primeset.begin(), primeset.end(), maxprime);
  for (Iter p = primeset.begin(); p != primesetlimit; ++p) {
    if (number % *p == 0) {
      return false;
    }
  }
  return true;
}

int resolve(int number) {
  if (number < 2) {
    return 0;
  }
  if (number == 2) {
    return 1;
  }
  std::vector<int> primesetExceptTwo;
  for (int i = 3; i <= number; i += 2) {
    if (isprime(i, primesetExceptTwo)) {
      primesetExceptTwo.push_back(i);
    }
  }
  return static_cast<int>(primesetExceptTwo.size()) + 1;
}

int main() {
  int input;
  while (std::cin >> input) {
    std::cout << resolve(input) << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

bool isprime(int number, const std::vector<int> &primeset) {
  for (std::vector<int>::const_iterator p = primeset.begin();
       p != primeset.end(); ++p) {
    if (number % *p == 0) {
      return false;
    }
  }
  return true;
}

int resolve(int number) {
  std::vector<int> primeset;
  for (int i = 2; i <= number; ++i) {
    if (isprime(i, primeset)) {
      primeset.push_back(i);
    }
  }
  return static_cast<int>(primeset.size());
}

int main() {
  int input;
  while (std::cin >> input) {
    std::cout << resolve(input) << std::endl;
  }

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

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

bool check(int in) {
  for (int i = 3; i <= sqrt(in); i += 2) {
    if (i != 5 && i % 5 == 0)
      continue;
    if (in % i == 0)
      return false;
  }
  return true;
}

int PrimeNumber(int in, int start) {
  int count = 0;
  if (in >= 2 && start == 3)
    count++;
  for (int i = start; i <= in; i += 2) {
    if (check(i))
      count++;
  }

  return count;
}

int main() {
  int in;
  int count250000 = PrimeNumber(250000, 3);
  int count500000 = count250000 + PrimeNumber(500000, 250001);
  int count750000 = count500000 + PrimeNumber(750000, 500001);

  while (cin >> in, !cin.eof()) {
    if (in <= 250000)
      cout << PrimeNumber(in, 3) << endl;
    else if (in <= 500000)
      cout << count250000 + PrimeNumber(in, 250001) << endl;
    else if (in <= 750000)
      cout << count500000 + PrimeNumber(in, 500001) << endl;
    else
      cout << count750000 + PrimeNumber(in, 750001) << endl;
  }
}

Error: TLE

Fixed submission:

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

bool check(int in) {
  for (int i = 3; i <= sqrt(in); i += 2) {
    if (i != 3 && i % 3 == 0)
      continue;
    if (i != 5 && i % 5 == 0)
      continue;
    if (i != 7 && i % 7 == 0)
      continue;
    if (in % i == 0)
      return false;
  }
  return true;
}

int PrimeNumber(int in, int start) {
  int count = 0;
  if (in > 2)
    count++;
  for (int i = start; i <= in; i += 2) {
    if (check(i))
      count++;
  }

  return count;
}

int main() {
  int in, count500000 = PrimeNumber(500000, 3);

  while (cin >> in, !cin.eof()) {
    if (in <= 500000)
      cout << PrimeNumber(in, 3) << endl;
    else
      cout << count500000 + PrimeNumber(in, 500001) << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  vector<int> ns;
  int temp, max_n = 0;
  while (cin >> temp) {
    ns.push_back(temp);
    max_n = max(temp, max_n);
  }
  max_n++;

  vector<bool> is_prime(max_n, true);
  vector<int> primes;
  for (ll i = 2; i < max_n; i++) {
    if (is_prime[i]) {
      primes.push_back(i);
      for (ll j = i * i; j < max_n; j += i) {
        is_prime[j] = false;
      }
    }
  }

  for (int n : ns) {
    int ans = upper_bound(primes.begin(), primes.end(), n) - primes.begin();
    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

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

const int MAX_N = 1000000;

int main() {
  vector<bool> is_prime(MAX_N, true);
  vector<int> primes;
  for (int i = 2; i < MAX_N; i++) {
    if (is_prime[i]) {
      primes.push_back(i);
      for (int j = i * i; j < MAX_N; j += i) {
        is_prime[j] = false;
      }
    }
  }

  int n;
  while (cin >> n) {
    int ans = upper_bound(primes.begin(), primes.end(), n) - primes.begin();
    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int INF = (int)((unsigned int)-1 >> 1);

int main() {
  vector<int> prime;
  prime.push_back(2);

  for (int i = 3; i <= 999999; i += 2) {
    bool is_prime = true;
    for (int j = 0; j < prime.size(); j++) {
      if (prime[j] * prime[j] > i)
        break;
      if (i % prime[j] == 0) {
        is_prime = false;
        break;
      }
    }
    if (is_prime) {
      prime.push_back(i);
    }
  }

  int n;
  while (cin >> n) {
    int ans = 0;
    for (int j = 0; j < prime.size(); j++) {
      if (prime[j] > n)
        break;
      ans++;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int INF = (int)((unsigned int)-1 >> 1);

int main() {
  int n;
  while (cin >> n) {
    vector<int> prime;
    prime.push_back(2);

    for (int i = 3; i <= n; i += 2) {
      bool is_prime = true;
      for (int j = 0; j < prime.size(); j++) {
        if (i % prime[j] == 0) {
          is_prime = false;
          break;
        }
      }
      if (is_prime) {
        prime.push_back(i);
      }
    }

    cout << prime.size() << endl;
  }

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

#include <iostream>
using namespace std;

int main() {

  int judge[1000000];
  for (int k = 0; k < 1000000; k++)
    judge[k] = 1;
  int n, sum;

  while (cin >> n) {
    if (n == 0 || n == 1) {
      sum = 0;
    } else {
      sum = 1;
      for (int i = 3; i < n + 1; i += 2) {
        if (judge[i] == 1) {
          sum++;
          for (int j = 2; j <= n / i; j++)
            judge[i * j] = 0;
        }
      }
    }

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  // 変数の宣言

  int i, n, j, sum, judge = 1;

  while (cin >> n) {
    sum = 1;
    for (i = 3; i < n + 1; i += 2) {
      for (j = 3; j <= sqrt(i); j += 2) {
        if (i % j == 0) {
          judge = 0;
          break;
        }
      }
      if (judge == 1) {
        sum++;
      }
      judge = 1;
    }

    cout << sum << endl;
  }

  return 0;
}