alexjercan/bugnet huggingface dataset:
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int b[1000000];
  for (int k = 0; k < 1000001; k++) {
    b[k] = 0;
  }
  b[2] = 1;
  b[3] = 1;
  b[4] = 0;
  b[5] = 1;
  b[6] = 0;
  b[7] = 1;

  for (int p = 11; p < 1000001; p += 2) {
    int i, a = 0;
    i = 3;
    while (i <= sqrt(p)) {
      if (p % i == 0) {
        break;
      }
      i = i + 2;
    }
    if (i > sqrt(p)) {
      a = 1;
    }
    b[p] = a;
  }
  int c[1000000];
  for (int l = 0; l < 1000001; l++) {
    c[l + 1] = c[l] + b[l + 1];
  }
  int r;
  while (cin >> r) {
    cout << c[r] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int b[1000];
  for (int k = 0; k < 1001; k++) {
    b[k] = 0;
  }
  for (int p = 2; p < 1001; p++) {
    int i, a = 0;
    i = 2;
    while (i < p) {
      if (p % i == 0) {
        break;
      }
      i = i + 1;
    }
    if (i == p) {
      a = 1;
    }
    b[p] = a;
  }
  int c[1000];
  for (int l = 0; l < 1001; l++) {
    c[l + 1] = c[l] + b[l + 1];
  }
  int r;
  while (cin >> r) {
    cout << c[r] << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

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

int sosuu(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 = i * 2; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

int main(void) {
  int n;
  while (cin >> n)
    cout << sosuu(n) << endl;
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

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

int sosuu(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 = i * 2; j <= n; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return p;
}

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

// Prime Number
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  const int MAX = 1000000;
  bool isPrime[MAX];
  for (int i = 0; i < MAX; i++) {
    isPrime[i] = true;
  }

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

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

  return 0;
}

Error: TLE

Fixed submission:

// Prime Number
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int count = 0;
    for (int i = 2; i <= n; i++) {
      bool flag = true;
      for (int j = 2; j <= i / 2; j++) {
        if (i % j == 0) {
          flag = false;
          break;
        }
      }
      if (flag == true) {
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
bool F[1000000] = {};

int main() {

  for (int i = 2; i < 1000000; i++)
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        F[i] = false;
        break;
      } else
        F[i] = true;
    }
  F[2] = true;
  F[3] = true;
  int N;
  while (cin >> N) {
    int c = 0;
    for (int i = 0; i <= N; i++) {
      if (F[i] == true) {
        c++; // cout<<i<<endl;
      }
    }

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

Error: -11

Fixed submission:

#include <stdio.h>
int num[1000001] = {};
int main() {
  int p, n, m, cou;
  m = 0;
  cou = 0;
  p = 3;

  for (int i = 2; i <= 10000001; i++)
    num[i] = 1;
  for (int i = 2; i <= 1001; i++) {
    for (int k = 2; k * k <= i; k++) {
      if (i % k == 0) {
        num[i] = 0;
      }
    }
  }
  while (scanf("%d", &n) != EOF) {
    for (int i = 2; i <= n; i++)
      cou += num[i];

    if (n == 2)
      cou = 1;
    if (n == 1)
      cou = 0;
    // else cou++;
    printf("%d\\n", cou);
    cou = 0;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <string.h>
#include <time.h> // for clock()
using namespace std;

/*
1???2 ?????? n ?????§?????´??°???????????????
2??????????????£???????????°????????§???????°?????????????????????? p
??¨?????????????????????????????????????????°?????? p ??¨????????? p ??\\?????? p
????????°?????¨????¶???????????????? 3???2??????????????°?????????????????????p
??? ???n ????¶??????????????????????????????????????????£?????????????´???°???
*/
// ??¨????????????????????????
int SieveOfEratosthenes(int num) {

  if (num < 2) {
    return 0;
  }
  // num???????´???°?????°
  int count = 0;
  bool fl[1000001];
  memset(fl, true, sizeof(fl));
  for (int i = 2; i <= sqrt(num); i++) {
    if (fl[i]) {
      for (int j = 2; j * i <= num; j++) {
        fl[i * j] = false;
      }
    }
  }
  for (int i = 2; i <= num; i++) {
    if (fl[i]) {
      count++;
    }
  }
  return count;
}

int main() {

  int num = 0;
  while (cin >> num) {
    // clock_t start = clock();    // ??????????????????
    cout << SieveOfEratosthenes(num) << endl;
    // clock_t end = clock();     // ????????????
    // std::cout << "duration = " << (double)(end - start) / CLOCKS_PER_SEC <<
    // "sec.\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <time.h> // for clock()
#include <vector>
using namespace std;

/*
1???2 ?????? n ?????§?????´??°???????????????
2??????????????£???????????°????????§???????°?????????????????????? p
??¨?????????????????????????????????????????°?????? p ??¨????????? p ??\\?????? p
????????°?????¨????¶???????????????? 3???2??????????????°?????????????????????p
??? ???n ????¶??????????????????????????????????????????£?????????????´???°???
*/
// ??¨????????????????????????
int SieveOfEratosthenes(int num) {

  if (num < 2) {
    return 0;
  }
  // num???????´???°?????°
  int count = 0;
  int i = 2;
  while (i <= num) {
    if (i == 2) {
      count++;
      i++;
      continue;
    } else if (i % 2 == 0) {
      // count++;
      i++;
      continue;
    } else {
      // Route Num?????´??°??¨??????????????????
      int routeN_int = (int)sqrt(i);
      int j = 3;
      while (1) {
        if (j > routeN_int) {
          count++;
          break;
        }
        if (i % j == 0) {
          break;
        }
        j += 2;
      }
    }
    i++;
  }

  return count;
}

int main() {

  int num = 0;
  while (cin >> num) {
    // clock_t start = clock();    // ??????????????????
    cout << SieveOfEratosthenes(num) << endl;
    // clock_t end = clock();     // ????????????
    // std::cout << "duration = " << (double)(end - start) / CLOCKS_PER_SEC <<
    // "sec.\\n";
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int p[1000000], size;

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

void mkPrime(int n) {
  size = 0;
  p[size++] = 2;
  for (int i = 3; i <= n; i += 2) {
    if (isPrime(i)) {
      p[size++] = i;
    }
  }
}

int main(void) {
  mkPrime(1000020);

  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; p[i] <= n; i++) {
      ans++;
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int main(void) {
  int n;
  int ans = 0;

  while (cin >> n) {
    for (int i = 2; i <= n; i++) {
      if (isPrime(i))
        ans++;
    }
    printf("%d\\n", ans);
  }

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

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

int main() {
  bool IsPrime[1000010] = {};
  for (int i = 2; i < 999999; i++) {
    IsPrime[i] = true;
  }
  for (int i = 2; i * i <= 999999; i++) {
    if (IsPrime[i]) {
      for (int j = i * 2; j <= 999999; j += i) {
        IsPrime[j] = false;
      }
    }
  }

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

Error: TLE

Fixed submission:

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

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

int main() {
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if (IsPrime(i))
        ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
int isprime(int x) {
  int i;
  if (x == 2) {
    return 1;
  }
  if (x < 2 || x % 2 == 0) {
    return 0;
  }
  i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0) {
      return 0;
    }
    i = i + 2;
  }
  return 1;
}
int main() {
  int l[999999] = {0};
  int r = 0, n;
  for (int i = 0; i < 999999; i++) {
    if (isprime(i + 1)) {
      r++;
    }
    l[i] = r;
  }
  while (scanf("%d", &n) != EOF) {
    printf("%d\\n", l[n - 1]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
int main() {
  int n, c, i, j, k;
  while (scanf("%d", &n) != EOF) {
    c = 1;
    for (j = 3; j <= n; j += 2) {
      k = 1;
      i = 3;
      while (i <= sqrt(j)) {
        if (j % i == 0) {
          k = 0;
          break;
        }
        i = i + 2;
      }
      if (k) {
        c++;
      }
    }
    printf("%d\\n", c);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

vi prime_count;
void sieve(int max) {
  int count = 0;
  vector<bool> table(max + 1, true);
  table[0] = false;
  prime_count.push_back(0);
  table[1] = false;
  prime_count.push_back(0);
  for (int i = 2; i <= max; i++) {
    if (table[i]) {
      count++;
      for (int j = 2; i * j <= max; j++) {
        table[i * j] = false;
      }
    }
    prime_count.push_back(count);
  }
}

int main() {
  int n;
  sieve(1000000);
  while (cin >> n) {
    cout << prime_count[n] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

vi prime_count;
void sieve(int max) {
  int count = 0;
  vector<bool> table(max + 1, true);
  table[0] = false;
  prime_count.push_back(0);
  table[1] = false;
  prime_count.push_back(0);
  for (int i = 2; i <= max; i++) {
    if (table[i]) {
      count++;
      for (int j = 2; i * j <= max; j++) {
        table[i * j] = false;
      }
    }
    prime_count.push_back(count);
  }
}

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

#include <algorithm> // sort などのアルゴリズム
#include <ctype.h>   // C の文字種用
#include <iostream>  // C++の入出力
#include <stdio.h>   // C の入出力
#include <string.h>  // C の文字列用
#include <string>    // C++の文字列
using namespace std;
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 <math.h>
#include <stdio.h>
int sosuu(int x) {
  int n, m, sosuu;
  sosuu = x - 1;
  for (n = 2; n <= x;
       n++) { // nは素数を調べる。入力された数字まで１からスライドしていく。
    for (
        m = 2; m < n;
        m++) { // mはnが素数かどうか調べる。素数かどうかをnより小さい数字で数えていく。2からなのは2から素数だから。1はふくめない。
      if (n % m == 0) { // 素数じゃなかったら、つまり割り切れてしまったら。
        sosuu--;
      }

      if (n % m == 0)
        break;
    }
  }
  if (x == 1 || x == 0)
    sosuu = 0;
  return sosuu;
}

int main(void) {
  int s, result;
  while (scanf("%d", &s) != EOF) {
    result = sosuu(s);
    printf("%d\\n", result);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n;
int ans[1000000];

int main() {
  for (int i = 2; i <= 999999; i++) {
    ans[i] = ans[i - 1];
    for (int j = 2; j <= sqrt(i); j++) {
      if (i % j == 0)
        goto f;
    }
    ans[i]++;
  f:;
  }
  while (cin >> n) {
    cout << ans[n] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int n, k, kk;
  while (cin >> n) {
    kk = 0;
    for (int j = 3; j <= n; j += 2) {
      k = 0;
      for (int i = 3; i <= sqrt(j); i += 2) {
        if (j % i == 0)
          k++;
      }
      if (k == 1)
        kk++;
    }
    if (n == 1)
      cout << "0" << endl;
    else if (n == 2)
      cout << "1" << endl;
    else
      cout << kk + 1 << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
Write a program which reads an integer n (n &#8804; 999999) and prints the
number of prime numbers which are less than or equal to n. A prime number is a
natural number which has exactly two distinct natural number divisors: 1 and
itself. For example, the first four prime numbers are: 2, 3, 5, 7.
*/

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

using namespace std;

/*
http://community.appliplanet.co.jp/community/beginner

[|Cg]
+nªfÅÈ¢ÈçÌ½ûªmæè¬³¢àÌÅèØêéÍ¸B
+\\ßQÌ{ðO·éB
+ªå«­ÈéÈçÎGXgeXÌâ¿ðÂ©¤©ÀÍbool[]ðg¤B
*/
void solve(int n) {
  bool prime[n + 1];
  for (int i = 0; i < n + 1; i++)
    prime[i] = true;
  prime[0] = false;
  prime[1] = false;
  int m = (int)sqrt(n); // nÌ½ûª

  for (int i = 2; i <= m; i++) {
    if (prime[i]) {
      for (int k = i * i; k <= n; k += i)
        prime[k] = false;
    }
  }

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

  cout << count << endl;
}

int main() {
  int n;
  while (cin >> n) {
    solve(n);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

void solve(int n) {
  int prime[n];
  int j = 0;
  for (int i = 2; i <= n; i++) {
    prime[j] = i;
    j++;
  }
  int p;
  for (int i = 0; i < n; i++) {
    p = prime[i];
    if (p == -1)
      continue;
    for (int k = i + 1; k < n; k++) {
      if (prime[k] % p == 0)
        prime[k] = -1;
    }
  }
  int count = 0;
  for (int i = 0; i < n; i++)
    if (prime[i] != -1)
      count++;
  cout << count << endl;
}

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

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, !cin.eof()) {
    vector<int> prime;
    prime.push_back(
        2); // ôðÈ­½ß2ÍÅÉüêÄ¨­
    bool flag;
    for (int x = 3; x <= n; x += 2) {
      flag = true;
      for (int y = 0; prime[y] * prime[y] <= x; y++) {
        if (!(x % prime[y])) {
          flag = !flag;
          break;
        }
      }
      if (flag)
        prime.push_back(x);
    }
    int r = (n <= 1) ? (0) : (prime.size());
    cout << r << endl;
    prime.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  bool f;
  while (1) {
    vector<int> array;
    cin >> n;
    if (cin.eof())
      return 0;
    for (int i = 2; i <= n; i++) {
      f = true;
      for (int j = 0; j < array.size(); j++)
        if (!(i % array[j])) {
          f = false;
          continue;
        }
      if (f == true)
        array.push_back(i);
    }
    cout << array.size() << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
const int MAX_N = 1000000;
int main() {
  bool sosu[MAX_N] = {}; // false:sosu
  for (int i = 2; i < MAX_N; i++) {
    if (!sosu[i]) {
      for (int j = 2; j * i < MAX_N; j++) {
        sosu[j * i] = true;
      }
    }
  }
  for (int n; cin >> n;) {
    int ans = 0;
    for (int i = 2; i <= n; i++) {
      if (!sosu[i])
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define L long long int
int main() {
  int n, ans, i, j;
  while (cin >> n) {
    ans = 0;
    for (i = 2; i <= n; i++) { // search for 2 to n
      for (j = 2; j <= i; j++)
        if (i % j == 0)
          break;
      if (j == i)
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define N_MAX 999999

int prime[(N_MAX + 1) / 2];
int memo[N_MAX + 1];

int mkprime(void);

int main(void) {
  int n;

  prime[0] = 2;
  memo[1] = 0;
  memo[2] = 1;

  mkprime();

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

  return 0;
}

int mkprime(void) {
  int i, j, endindex = 1;

  for (i = 3; i < N_MAX + 1; i++) {
    for (j = 0; prime[j] * prime[j] <= i; j++) {
      if (i % prime[j] == 0) {
        goto tag;
      }
    }

    prime[endindex] = i;
    endindex++;

  tag:
    memo[i] = endindex;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#define N_MAX 999999

int prime[(N_MAX + 1) / 2];
int memo[N_MAX + 1];

int mkprime(void);

int main(void) {
  int n, i, result;

  prime[0] = 2;

  mkprime();

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

  return 0;
}

int mkprime(void) {
  int i, j, endindex = 1;

  for (i = 3; i < N_MAX + 1; i++) {
    if (i % 2 == 1) {
      for (j = 0; j < endindex; j++) {
        if (i % prime[j] == 0) {
          break;
        }
      }
    }

    if (prime[j] == 0) {
      prime[endindex] = i;
      endindex++;
    }

    memo[i] = endindex;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int n, prime[1000000], ans[1000000];

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

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

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

vector<int> PrimeNumber;
int p = 0;

int f(int x) {
  if (x == 2) {
    PrimeNumber.push_back(x);
    return 1;
  }
  for (int i = 0; i < PrimeNumber.size(); i++) {
    if (x % PrimeNumber[i] == 0)
      return 0;
  }
  PrimeNumber.push_back(x);
  return 1;
}

int main() {
  long int n;
  int ans = 0;
  while (1) {
    cin >> n;
    ans = 0;
    PrimeNumber.clear();
    for (int i = 2; i <= n; i++) {
      ans += f(i);
    }
    cout << ans << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int n, i, j, sosu[999999], ans = 0;
  for (i = 0; i < 999999; i++)
    sosu[i] = 1;
  sosu[0] = 0;
  for (i = 2; i <= 999999; i++) {
    if (sosu[i - 1] == 1) {
      for (j = i * 2; j <= 999999; j += i) {
        sosu[j - 1] = 0;
      }
    }
  }
  while (cin >> n) {
    ans = 0;
    for (i = 0; i < n; i++) {
      ans += sosu[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

void make_sosu(int ans[]) {
  int n, i, j, ind, cnt = 0;
  ans[0] = 2;
  ans[1] = 3;
  ans[2] = 5;
  ans[3] = 7;
  n = 9;
  ind = 4;
  while (n <= 99999) {
    cout << "checing::" << n << endl;
    cnt = 0;
    for (i = 3; i < n; i++) {
      if (n % i == 0) {
        cnt++;
        break;
      }
    }
    if (cnt == 0) {
      ans[ind] = n;
      ind++;
    }
    n += 2;
  }
}

int main(void) {
  int n, i, tmp, ans, sosu[100000] = {0};
  make_sosu(sosu);
  while (cin >> n) {
    if (n == 1)
      cout << "0" << endl;
    else {
      for (i = 0; i < n; i++) {
        if (n < sosu[i]) {
          cout << i << endl;
          break;
        }
      }
    }
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  int i, j, k;
  int era;

  while (cin >> n) {
    int count = 0;
    int S[n + 1];
    for (i = 2; i < n + 1; i++) {
      S[i] = i;
    }

    for (i = 2; i < ((int)sqrt(n) + 1); i++) {
      if (S[i] != 0) {
        era = S[i];
        for (j = 2; (j * era) < (n + 1); j++) {
          S[j * era] = 0;
        }
      }
    }

    for (i = 2; i < n + 1; i++) {
      if (S[i] != 0) {
        count++;
      }
    }

    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {

  int i, j, k;
  int n;

  while (cin >> n) {

    int count = 0;

    if (n > 2) {
      count++;
      for (i = 3; i <= n; i += 2) {
        for (j = 3; j <= (int)sqrt(i); j += 2) {
          k = i % j;
          if (k == 0)
            break;
        }
        if (j > (int)sqrt(i))
          count++;
      }
    }
    cout << count << endl;
  }

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

#include <stdio.h>
#include <string.h>
int main(void) {
  int n, i, c, k, prime[999999] = {0}, t;
  for (i = 2; 999999 >= i; i++) {
    if (!prime[i])
      for (c = i * 2; 999999 >= c; c += i)
        prime[c] = 1;
  }
  while (scanf("%d", &n) != EOF) {
    i = 0;
    for (c = 2; n >= c; c++) {
      if (!prime[c])
        i++;
    }
    printf("%d\\n", i);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  int n, i, c, k, t;
  while (scanf("%d", &n) != EOF) {
    i = 0;
    for (c = 2; n >= c; c++) {
      t = 0;
      for (k = 2; c / 2 >= k; k++) {
        if (c % k == 0) {
          t = 1;
          break;
        }
      }
      if (t == 0)
        i++;
    }

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

  return 0;
}