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

#include <iostream>
using namespace std;

int main() {
  // your code goes here
  int n;
  while (cin >> n) {
    int count = 0;
    for (int i = 9; i >= 0; i--) {
      for (int j = 9; j >= 0; j--) {
        for (int k = 9; k >= 0; k--) {
          for (int l = 9; l >= 0; l--) {
            if (i + j + k + l == n)
              count++;
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  // your code goes here
  int n;
  while (cin >> n) {
    int count = 0;
    if (n > 36) {
      cout << 0 << endl;
    } else {
      for (int i = 9; i >= 0; i--) {
        for (int j = i; j >= 0; j--) {
          for (int k = j; k >= 0; j--) {
            for (int l = k; l >= 0; l--) {
              if (i + j + k + l == n)
                count++;
            }
          }
        }
      }
      cout << count << endl;
    }
  }
  return 0;
}
Problem p00008 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    for (int i = 0; i <= 9; ++i)
      for (int j = 0; j <= 9; ++j)
        for (int k = 0; k <= 9; ++k)
          for (int l = 0; l <= 9; ++l)
            if (i + j + k + l == n)
              ++cnt;
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <iostream>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    for (int i = 0; i <= 9; ++i)
      for (int j = 0; i <= 9; ++j)
        for (int k = 0; k <= 9; ++k)
          for (int l = 0; l <= 9; ++l)
            if (i + j + k + l == n)
              ++cnt;
    cout << cnt << endl;
  }
  return 0;
}
Problem p00008 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int cnt;
  int num;
  while (cin >> num) {
    cnt = 0;
    for (int a = 0; a < 10; a++) {
      for (int b = 0; b < 10; b++) {
        for (int c = 0; c < 10; c++) {
          for (int d = 0; d < 10; d++) {
            if (a + b + c + d == num) {
              cnt++;
            }
          }
        }
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int cnt;
  while (1) {
    int num;
    cin >> num;
    cnt = 0;
    for (int a = 0; a < 10; a++) {
      for (int b = 0; b < 10; b++) {
        for (int c = 0; c < 10; c++) {
          for (int d = 0; d < 10; d++) {
            if (a + b + c + d == num) {
              cnt++;
            }
          }
        }
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00008 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  while (cin >> n && n != EOF) {
    int ans = 0;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
          for (int l = 0; l < 10; l++) {
            if (i + j + k + l == n)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n, ans = 0;
    cin >> n;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
          for (int l = 0; l < 10; l++) {
            if (i + j + k + l == n)
              ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }

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

#include <iostream>
using namespace std;

int main() {
  int n, val;
  while (cin >> n) {
    val = 0;
    for (int i = 0; i <= 9; i++) {
      for (int j = 0; j <= 9; j++) {
        for (int k = 0; k <= 9; k++) {
          for (int l = 0; l <= 9; l++) {
            if (i + j + k + l == n) {
              val += 1;
            }
          }
        }
      }
    }
    cout << val << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, val;
  while (1) {
    val = 0;
    cin >> n;
    for (int i = 0; i <= 9; i++) {
      for (int j = 0; j <= 9; j++) {
        for (int k = 0; k <= 9; k++) {
          for (int l = 0; l <= 9; l++) {
            if (i + j + k + l == n) {
              val += 1;
            }
          }
        }
      }
    }
    cout << val << endl;
  }
  return 0;
}
Problem p00008 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main(void) {
  int n;
  int a, b, c, d;
  int com;

  while (cin >> n) {
    com = 0;
    if (n == EOF)
      break;

    for (a = 0; a < 10; a++) {
      for (b = 0; b < 10; b++) {
        for (c = 0; c < 10; c++) {
          for (d = 0; d < 10; d++) {
            if (a + b + c + d == n)
              com++;
          }
        }
      }
    }
    cout << com << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  int n;
  int a, b, c, d;
  int com;

  while (1) {
    cin >> n;
    if (n == EOF)
      break;
    com = 0;

    for (a = 0; a < 10; a++) {
      for (b = 0; b < 10; b++) {
        for (c = 0; c < 10; c++) {
          for (d = 0; d < 10; d++) {
            if (n == a + b + c + d)
              com++;
          }
        }
      }
    }
    cout << com << endl;
    ;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#include <sstream>
typedef long double ld;
int a;
int isprime(int aa) {
  if (aa == 1)
    return 0;
  for (int i = 2; i * i <= aa; ++i) {
    if (aa % i == 0) {
      return 0;
    }
  }
  return 1;
}

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

#include <iostream>

#define N 1000000

using namespace std;

int main() {
  bool prime[N];
  prime[0] = prime[1] = false;

  // initialize
  for (int i = 2; i < N; i++)
    prime[i] = true;
  // Sieve of Eratosthenes
  for (int i = 2; i <= N / 2; i++) {
    if (prime[i]) {
      for (int j = 2; i * j <= N; j++) {
        prime[i * j] = false;
      }
    }
  }

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

  int input;
  while (true) {
    cin >> input;
    if (cin.eof()) {
      break;
    }
    cout << countPrime[input] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

#define N 1000000

using namespace std;

int main() {
  int num = 0, y = 0, n;
  while (cin >> n) {
    num = 0;
    for (int i = 2; i <= n; i++) {
      y = 0;
      for (int j = 2; j < i; j++) {
        if (i % j == 0) {
          y++;
        }
      }
      if (y == 0) {
        num++;
      }
    }
    cout << num << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MAX 1000000
int a[MAX];

int main() {
  int n;
  a[0] = a[1] = 0;
  a[2] = 1;
  a[3] = 2;
  for (int i = 4; i < MAX; i++) {
    bool ok = true;
    for (int j = 2; j < sqrt(i) + 0.1; j++) {
      if (i % j == 0) {
        a[i] = a[i - 1];
        ok = false;
        break;
      }
    }
    if (ok)
      a[i] = a[i - 1] + 1;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX 10000000
int a[MAX];

int main() {
  int n;
  a[0] = a[1] = 0;
  a[2] = 1;
  a[3] = 2;
  for (int i = 4; i < MAX; i++) {
    bool ok = true;
    for (int j = 2; j < sqrt(i) + 0.1; j++) {
      if (i % j == 0) {
        a[i] = a[i - 1];
        ok = false;
        break;
      }
    }
    if (ok)
      a[i] = a[i - 1] + 1;
  }

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

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

#define _USE_MATH_DEFINES
#define llint long long
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int pn = 0;
int p[1000000];
int s[1000000];

int main() {
  s[1] = 0;
  int i = 2;
  do {
    bool flg = true;
    for (int j = 0; j < pn; j++) {
      if (p[j] * p[j] > i)
        break;
      if (i % p[j] == 0) {
        flg = false;
        break;
      }
    }
    if (flg)
      p[pn++] = i;
    s[i] = pn;
  } while ((++i) <= 999999);

  int n;
  while (cin >> n) {
    cout << s[n] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#define llint long long
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int pn = 0;
int p[1000000];
void getsosu(int Max) {
  for (int i = 2; i <= Max; i++) {
    bool flg = true;
    for (int j = 0; j < pn; j++) {
      if (p[j] * p[j] > i)
        break;
      if (i % p[j] == 0) {
        flg = false;
        break;
      }
    }
    if (flg)
      p[pn++] = i;
  }
}

int main() {
  getsosu(999999);

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

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

int main() {
  int max = 1000000;
  vector<int> prime(max, 1);
  // ツエツδ可トツスツテツネツスツづ個づ督づゥツつ「

  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 = i + i; j <= max; j += i) {
        prime[j] = 0;
      }
    }
  }
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      if (prime[i] == 1) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int maxy = (int)sqrt(100000);
  int prime[100000];
  for (int i = 0; i < 100000; i++) {
    prime[i] = 1;
  }
  prime[0] = prime[1] = 0;
  for (int i = 2; i <= maxy; i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j <= maxy; j += i) {
        prime[j] = 0;
      }
    }
  }
  int n;
  while (cin >> n) {
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      if (prime[i] == 1) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int a[1000000] = {};

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int isPrime(int a) {
  int i;
  int isTrue;
  for (i = 3, isTrue = 1; i < a; i += 2) {
    if (!(a % i)) {
      isTrue = 0;
      break;
    }
  }
  return a == 2 || isTrue && a % 2;
}

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
const int INF = 100000000;
const double EPS = 0.0000001;
typedef pair<int, int> P;

int n;
//           999999
bool isPrime[1000003];

void solve() {
  int ans = 0;
  rep(i, n + 1) if (isPrime[i]) ans++;
  cout << ans << endl;
}

void init() {
  rep(i, 1000003) isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;

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

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

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
const int INF = 100000000;
const double EPS = 0.0000001;
typedef pair<int, int> P;

int n;

bool isPrime[100003];

void solve() {
  int ans = 0;
  rep(i, n + 1) if (isPrime[i]) ans++;
  cout << ans << endl;
}

void init() {
  rep(i, 100003) isPrime[i] = true;
  isPrime[0] = isPrime[1] = false;

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

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

#include <iostream>
using namespace std;

int list[1000000] = {};

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

  int n;
  while (cin >> n) {

    if (list[n] != 0) {
      cout << list[n] << endl;
      continue;
    }

    int cnt = 0;
    for (int i = 2; i <= n; ++i) {
      bool check = true;
      for (int j = 2; i >= j * j; ++j) {
        if (i % j == 0) {
          check = false;
          break;
        }
      }
      if (check)
        cnt++;

      if (list[i] == 0) {
        list[i] += list[i - 1] + check;
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  int n;
  while (cin >> n) {

    int cnt = 0;
    for (int i = 2; i <= n; ++i) {
      bool check = true;
      for (int j = 2; i >= j * j; ++j) {
        if (i % j == 0) {
          check = false;
          break;
        }
      }
      if (check)
        cnt++;
    }

    cout << cnt << endl;
  }

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

#include <cmath>
#include <iostream>
#define N 999999
using namespace std;

int arr[N];

void Eratosthenes() {
  for (int i = 0; i < N; i++) {
    arr[i] = 1;
  }
  for (int i = 2; i < sqrt(N); i++) {
    if (arr[i]) {
      for (int j = 0; i * (j + 2) < N; j++) {
        arr[i * (j + 2)] = 0;
      }
    }
  }
  return;
}

int main() {
  int i, j = 0, n, pn;
  while (cin >> n) {
    pn = 0;
    Eratosthenes();
    for (i = 2; i <= n; i++) {
      if (arr[i] == 1)
        pn++;
    }
    cout << pn << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int i, j, n, pn, cn = 0;

  while (cin >> n) {
    pn = 1;
    for (i = 3; i <= n; i++) {
      for (j = 2; j < i; j++) {
        if (i % j == 0)
          goto skip;
      }
      pn++;
    skip:;
    }
    if (n == 0 || n == 1) {
      cout << 0 << endl;
    } else {
      cout << pn << endl;
    }
    cn++;
    if (cn >= 30)
      break;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define NUM 1000000
int main() {
  long int n;
  int p[NUM], i, j, k, a, b;
  for (i = 0; i < NUM; i++) {
    p[i] = 0;
  }
  for (i = 2; i < NUM; i++) {
    if (p[i] == 0) {
      p[i] = 2;
      for (j = 2; j <= (NUM / i); j++) {
        p[i * j] = 1;
      }
    }
  }
  while (cin >> n) {
    a = 0;
    for (i = 2; i <= n; i++) {
      if (p[i] == 2)
        a++;
    }
    cout << a << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int p;
  while (cin >> p) {
    int n[100001], i, j, a = 0;
    for (i = 0; i < 100001; i++) {
      n[i] = 0;
    }
    n[0] = 1;
    n[1] = 1;
    for (i = 2; i < 100000; i++) {
      if (n[i] == 0) {
        j = 2;
        while (i * j < 100000) {
          n[i * j] = 1;
          j++;
        }
      }
    }
    for (i = 1; i < p; i++) {
      if (n[i] == 0) {
        a++;
      }
    }
    cout << a << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main() {
  int n;
  int *prime;
  int i, j;
  while (cin >> n) {
    // init
    prime = new int[n + 1];
    for (i = 0; i <= n; i++)
      prime[i] = 0;

    // 素数判定
    for (i = 2; i * i <= n; i++) {
      if (prime[i] == 0) {
        prime[i] = 1;                  // iは素数
        for (j = 2; i * j <= n; j++) { // 倍数ふるい落とし
          prime[i * j] = -1;           // i*jは素数じゃない
        }
      }
    }
    for (i = i; i <= n; i++) {
      if (prime[i] == 0)
        prime[i] = 1;
    }

    // output
    int ans = 0;
    for (i = 2; i <= n; i++) {
      if (prime[i] == 1) {
        ans++;
        // cout << "prime:" << i << endl;;
      }
    }
    cout << ans << endl;

    delete[] prime;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, N;
  int *prime;
  int i, j;
  while (cin >> N) {
    n = N + 1;
    // init
    prime = new int[n];
    for (i = 0; i <= n; i++)
      prime[i] = 0;

    // 素数判定
    for (i = 2; i * i <= n; i++) {
      if (prime[i] == 0) {
        prime[i] = 1;                  // iは素数
        for (j = 2; i * j <= n; j++) { // 倍数ふるい落とし
          prime[i * j] = -1;           // i*jは素数じゃない
        }
      }
    }
    for (i = i; i <= n; i++) {
      if (prime[i] == 0)
        prime[i] = 1;
    }

    // output
    int ans = 0;
    for (i = 2; i <= n; i++) {
      if (prime[i] == 1) {
        ans++;
        // cout << "prime:" << i << endl;;
      }
    }
    cout << ans << endl;

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

#include <cmath>
#include <iostream>

using namespace std;

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

  return true;
}

int main() {
  // --- variable define --- //

  int n, c;

  // --- prime check ( 2 to 999999 ) --- //

  bool isprime[1000000];

  for (int i = 1; i < 1000000; i++) {
    if (prime_check(i) == true) {
      isprime[i] = true;
    } else {
      isprime[i] = false;
    }
  }

  // --- main --- //

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

    for (int i = 2; i <= n; i++) {
      if (isprime[i] == true) {
        c++;
      }
    }

    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

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

  return true;
}

int main() {
  // --- variable define --- //

  int n, c;

  // --- prime check ( 2 to 999 ) --- //

  bool isprime[1000];

  for (int i = 1; i < 1000; i++) {
    if (prime_check(i) == true) {
      isprime[i] = true;
    } else {
      isprime[i] = false;
    }
  }

  // --- main --- //

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

    for (int i = 2; i <= n; i++) {
      if (i <= 999) {
        if (isprime[i] = true) {
          c += 1;
        } else {
          c += 0;
        }
      } else {
        for (int j = 2; j <= sqrt(i); j++) {
          if (n % i == 0) {
            c += 0;
            goto Exit;
          }
        }

        c += 1;
      }
    Exit:;
    }

    cout << c << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

#define MAX_N 999999
#define MAX_D 30

bool prime_check(int n, int k = 2) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return prime_check(n, k + 1);
}

int main() {
  int n;

  bool isprime[MAX_N + 1];

  for (int i = 2; i < MAX_N + 1; i++) {
    isprime[i] = prime_check(i);
  }

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

    for (int i = 2; i <= n; i++) {
      if (isprime[i] == true) {
        c++;
      }
    }

    cout << c << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define MAX_N 99999
#define MAX_D 30

bool prime_check(int n, int k = 2) {
  if (n < k * k) {
    return true;
  }

  if (n % k == 0) {
    return false;
  }

  return prime_check(n, k + 1);
}

int main() {
  int n;

  bool isprime[MAX_N + 1];

  for (int i = 2; i < MAX_N + 1; i++) {
    isprime[i] = prime_check(i);
  }

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

    for (int i = 2; i <= n; i++) {
      if (isprime[i] == true) {
        c++;
      }
    }

    cout << c << endl;
  }

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

///
// File:  0009.cpp
// Author: ymiyamoto
//
// Created on Wed Nov  8 23:17:22 2017
//

#include <cmath>
#include <cstdint>
#include <iostream>

using namespace std;

static int32_t primes[1000000];

bool is_prime(uint32_t val) {
  if (primes[val] == 1)
    return true;
  if (primes[val] == -1)
    return false;

  for (uint32_t i = 2; i <= sqrt(val); i++) {
    if (val % i == 0) {
      primes[val] = -1;
      return false;
    }
  }

  primes[val] = 1;
  return true;
}

int32_t main() {
  while (true) {
    uint64_t n;
    cin >> n;
    if (cin.eof())
      break;

    uint32_t count = 0;
    for (uint32_t i = 2; i <= n; i++) {
      if (is_prime(i))
        count++;
    }
    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

///
// File:  0009.cpp
// Author: ymiyamoto
//
// Created on Wed Nov  8 23:17:22 2017
//

#include <cmath>
#include <cstdint>
#include <iostream>

using namespace std;

bool is_prime(uint32_t val) {
  for (uint32_t i = 2; i <= sqrt(val); i++) {
    if (val % i == 0)
      return false;
  }
  return true;
}

int32_t main() {
  while (true) {
    uint64_t n;
    cin >> n;
    if (cin.eof())
      break;

    uint32_t count = 0;
    for (uint32_t i = 2; i <= n; i++) {
      if (is_prime(i))
        count++;
    }
    cout << count << endl;
  }

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

#include <stdio.h>

int main() {
  const int MAX_V = 1000000;
  int prime[MAX_V + 1];
  int i, k, v;
  int ans;
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }
  while (scanf("%d", &v) != EOF) {
    int count = 0;
    for (k = 2; k <= v; k++) {
      ans = prime[k];
      if (ans == 1) {
        count++;
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>

int main() {
  const int MAX_V = 100000;
  int prime[MAX_V + 1];
  int i, k, v;
  int e, f, ans;
  for (i = 2; i <= MAX_V; i++) {
    prime[i] = 1;
  }
  for (i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (k = 2 * i; k <= MAX_V; k += i) {
        prime[k] = 0;
      }
    }
  }
  for (e = 0; e < 30; e++) {
    int count = 0;
    scanf("%d", &v);
    for (f = 2; f <= v; f++) {
      ans = prime[f];
      if (ans == 1) {
        count++;
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}