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

#include <iostream>
using namespace std;

int main() {

  int i, j, num;
  int *pT;

  while (cin >> num) {
    int count = 0;
    pT = new int[num];

    for (i = 0; i < num; i++) {
      pT[i] = 0;
    }

    pT[0] = 1;

    for (i = 2; i <= (num / 2); i++) {
      for (j = 2; i * j <= num; j++) {
        if (pT[i * j - 1] == 0) {
          pT[i * j - 1] = 1;
        }
      }
    }

    for (i = 0; i < num; i++) {
      if (pT[i] == 0) {
        ++count;
      }
    }

    delete[] pT;

    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int prime(int num) {

  if (num < 2) {
    return 0;
  } else if (num == 2) {
    return 1;
  } else if (num % 2 == 0) {
    return 0;
  }

  for (int s = 3; pow(s, 2) <= num; s += 2) {
    if (num % s == 0) {
      return 0;
    }
  }

  return 1;
}

int main() {
  int num;

  while (cin >> num) {
    int count = 0;
    for (int i = 0; i <= num; i++) {
      if (prime(i) == 1) {
        count++;
      }
    }

    cout << count << endl;
  }

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

#include <stdio.h>
const int MAX_V = 1000000;
int prime[MAX_V + 1], sum[MAX_V + 2] = {0};
int main() {
  int n;
  for (int i = 2; i <= MAX_V; i++) {
    prime[i] = 1;
  }
  for (int i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (int k = 2 * i; k <= MAX_V; k += i)
        prime[k] = 0;
    }
  }
  for (int i = 2; i < MAX_V; i++) {
    sum[i] += sum[i - 1] + prime[i];
  }
  while (scanf("%d", &n) != EOF) {
    printf("%d\\n", sum[n]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
const int MAX_V = 100000;
int prime[MAX_V + 1], sum[MAX_V + 2] = {0};
int main() {
  int n;
  for (int i = 2; i <= MAX_V; i++) {
    prime[i] = 1;
  }
  for (int i = 2; i * i <= MAX_V; i++) {
    if (prime[i]) {
      for (int k = 2 * i; k <= MAX_V; k += i)
        prime[k] = 0;
    }
  }
  for (int i = 2; i < MAX_V; i++) {
    sum[i] += sum[i - 1] + prime[i];
  }
  while (1) {
    scanf("%d", &n);
    printf("%d\\n", sum[n]);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Memory Limit Exceeded


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000
using namespace std;
typedef long long int ll;

int main() {
  vector<int> era(10000000, 0);
  for (int i = 1; i < era.size(); i++) {
    era[i] = 1;
  }
  for (int i = 2; i < era.size(); i++) {
    if (era[i] != 0) {
      int q = i + i;
      while (q < era.size()) {
        era[q] = 0;
        q += i;
      }
    }
  }
  int n;
  while (cin >> n) {
    int ans = -1;
    for (int i = 1; i <= n; i++) {
      if (era[i]) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000
using namespace std;
typedef long long int ll;

int main() {
  vector<ll> era(10000000, 0);
  for (int i = 1; i < era.size(); i++) {
    era[i] = i;
  }
  for (int i = 2; i < era.size(); i++) {
    if (era[i] != 0) {
      ll q = i + era[i];
      while (q < era.size()) {
        era[q] = 0;
        q += era[i];
      }
    }
  }
  int n;
  while (cin >> n) {
    int ans = -1;
    for (int i = 1; i <= n; i++) {
      if (era[i] == i) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

int ans;
int N;
bool prime[1000200];

void SE();

int main() {
  memset(prime, false, sizeof(prime));
  SE();
  while (scanf("%d", &N) == 1) {
    ans = 0;
    if (N >= 2) {
      ans++;
    }
    for (int i = 3; i <= N; i += 2) {
      if (prime[i] == false) {
        ans++;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

void SE() {
  prime[0] = prime[1] = true;
  for (int i = 3; i < 1004; i += 2) {
    if (prime[i] == false) {
      for (int j = i * i; j < 1000200; j += i * 2) {
        prime[j] = true;
      }
    }
  }
}

Error: WA

Fixed submission:

#include <stdio.h>

int ans;
int N;
bool prime[100020];

void SE();

int main() {
  SE();
  while (scanf("%d", &N) == 1) {
    ans = 0;
    if (N > 2) {
      ans++;
    }
    for (int i = 3; i < N; i += 2) {
      if (prime[i] == false) {
        ans++;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

void SE() {
  prime[0] = prime[1] = true;
  for (int i = 3; i < 1004; i += 2) {
    if (prime[i] == false) {
      for (int j = i * i; j < 100020; j += i * 2) {
        prime[j] = true;
      }
    }
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

bool sieve[1000000];
void makesieve();

int main() {
  makesieve();
  while (1) {
    int n, ans = 0;
    std::cin >> n;
    if (std::cin.eof())
      break;
    for (int i = 0; i <= n; i++) {
      ans += sieve[i] ? 1 : 0;
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

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

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

bool isprime(int n);

int main() {
  while (1) {
    int n, count = 0;
    std::cin >> n;
    if (std::cin.eof())
      break;
    for (int i = 2; i < n; i++) {
      count += isprime(i) ? 1 : 0;
    }
    std::cout << count << std::endl;
  }
  return 0;
}

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
typedef long long int ll;

using namespace std;
bool priN[1000000] = {};
bool pri[1000000] = {};

bool isPrime(int n) {
  if (priN[n] == true)
    return pri[n];
  for (int i = 3; i <= sqrt((double)n); i++) {
    if (n % i == 0) {
      priN[n] = true;
      pri[n] = false;
      return false;
    }
  }
  priN[n] = true;
  pri[n] = true;
  return true;
}

int main() {
  while (1) {
    int n;
    if (!(cin >> n))
      break;
    ll sum = 1;
    if (n < 2)
      sum--;
    for (int i = 3; i <= n; i += 2) {
      if (isPrime(i))
        sum++;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
typedef long long int ll;

using namespace std;

bool isPrime(int n) {
  for (int i = 3; i <= sqrt((double)n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

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

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

int prime[1000000];

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

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int prime[1000000] = {1};

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

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

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

bool is_SPRP(uint32_t n, uint32_t a) {
  uint32_t d = n - 1, s = 0;
  while ((d & 1) == 0) {
    ++s;
    d >>= 1;
  }
  uint64_t cur = 1, pw = d;
  while (pw) {
    if (pw & 1) {
      cur = (cur * a) % n;
    }
    a = ((uint64_t)a * a) % n;
    pw >>= 1;
  }
  if (cur == 1) {
    return true;
  }
  for (uint32_t r = 0; r < s; r++) {
    if (cur == n - 1) {
      return true;
    }
    cur = (cur * cur) % n;
  }
  return false;
}

bool is_prime(uint32_t x) {
  if (x <= 1)
    return false;
  if (x < 1ULL << 32) {
    uint32_t bases[] = {2, 7, 61};
    for (uint32_t i = 0; i < sizeof(bases) / sizeof(bases[0]); ++i) {
      if (x != bases[i] && !is_SPRP(x, bases[i])) {
        return false;
      }
    }
    return true;
  }
}

int p[1000000];

int main() {
  int n;
  for (int i = 0; i < 1000000; ++i) {
    p[i] = is_prime(i);
  }
  for (int i = 1; i < 1000000; ++i) {
    p[i] += p[i - 1];
  }
  while (cin >> n) {
    cout << p[n] << endl;
  }
}

Error: TLE

Fixed submission:

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

bool is_SPRP(uint32_t n, uint32_t a) {
  uint32_t d = n - 1, s = 0;
  while ((d & 1) == 0) {
    ++s;
    d >>= 1;
  }
  uint64_t cur = 1, pw = d;
  while (pw) {
    if (pw & 1) {
      cur = (cur * a) % n;
    }
    a = ((uint64_t)a * a) % n;
    pw >>= 1;
  }
  if (cur == 1) {
    return true;
  }
  for (uint32_t r = 0; r < s; r++) {
    if (cur == n - 1) {
      return true;
    }
    cur = (cur * cur) % n;
  }
  return false;
}

bool is_prime(uint32_t x) {
  if (x <= 1)
    return false;
  if (x < 1ULL << 32) {
    uint32_t bases[] = {2, 7, 61};
    for (uint32_t i = 0; i < sizeof(bases) / sizeof(bases[0]); ++i) {
      if (x != bases[i] && !is_SPRP(x, bases[i])) {
        return false;
      }
    }
    return true;
  }
}

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

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    int ans = 0;
    int d[999999 + 100] = {};
    for (int i = 2; i * i <= n; i++) {
      if (d[i] == 0) {
        for (int j = 2; j * i <= n; j++) {
          d[j * i]++;
        }
      }
    }
    d[1] = 1;
    for (int i = 1; i <= n; i++) {
      if (d[i] == 0) {
        ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    int ans = n - 1;
    int d[999999 + 100] = {};
    for (int i = 2; i <= n; i++) {
      for (int j = 3; j <= n; j++) {
        if (d[j] == 0 && j % i == 0 && j != i) {
          d[j] = 1;
          ans--;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool is_prime(int n, int r);

int main(void) {
  using namespace std;
  int x;
  int count;
  while (cin >> x) {
    count = 0;
    double r = pow(x, 0.5);
    for (int i = 2; i <= x; i++) {
      if (is_prime(i, r))
        count++;
    }
    cout << count << endl;
  }
  return 0;
}

bool is_prime(int n, int r) {
  static int flag = 0;
  static int list[78498];
  static int count = 0;
  if (flag == 1) {
    for (int i = 0; i < count; i++) {
      if (list[i] > r) {
        return true;
      }
      if (n % list[i] == 0) {
        if (n == list[i]) {

          return true;
        }

        return false;
      }
    }
  }
  flag = 1;
  list[count] = n;
  count++;

  return true;
}

Error: TLE

Fixed submission:

#include <iostream>

bool prime(int n);

int main(void) {
  using namespace std;
  int x;
  int count;
  while (cin >> x) {
    count = 0;
    for (int i = 2; i <= x; i++) {
      if (prime(i))
        count++;
    }
    cout << count << endl;
  }
  return 0;
}

bool prime(int n) {
  static int flag = 0;
  static int count = 0;
  static int list[78498];

  if (flag == 1) {
    for (int j = 0; j < count; j++) {
      if (n % list[j] == 0) {
        if (n == list[j]) {
          return true;
        }

        return false;
      }
    }
  }

  flag = 1;
  list[count] = n;
  count++;
  return true;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  vector<int> prime;
  bool num[1000001];
  for (int i = 0; i < 1000001; i++) {
    num[i] = true;
  }
  num[0] = false;
  num[1] = false;

  for (int i = 2; i <= 1000; i++) {
    for (int j = i * 2; j <= 1000000; j += i) {
      if (!num[j]) {
        continue;
      }
      num[j] = false;
    }
  }

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

  return (0);
}

Error: TLE

Fixed submission:

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

int main() {
  int n, temp, counter;
  temp = 0;
  counter = 0;
  while (cin >> n) {
    vector<int> num(n);
    num[0], num[1] = 1;
    for (int i = 0; i < n; i++) {
      if (num[i] == 0) {
        for (int j = 0; temp < n; j++) {
          temp = i * j;
          num[temp] = 1;
          counter++;
        }
      }
    }
    cout << counter << endl;
  }
  return (0);
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;

int const N = 1000010;
int ans[N];

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

int main(int argc, char **argv) {
  ans[0] = ans[1] = 0;
  for (int i = 2; i < N; ++i) {
    if (isprime(i))
      ans[i] = ans[i - 1] + 1;
    else
      ans[i] = ans[i - 1];
  }
  int n;
  while (~scanf("%d\\n", &n)) {
    printf("%d\\n", ans[n]);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main(void) {
  int i, j, cnt, n;
  while (scanf("%d", &n) != EOF) {
    cnt = 0;
    for (i = 2; i <= n; i++) {
      for (j = 2; j <= i; j++) {
        if (i == j) {
          cnt++;
        }
        if (i % j == 0) {
          break;
        }
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

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

using namespace std;

int a[1000010];

bool p(int n) {
  int m;
  for (int i = 2; i <= n; i++) {
    if (n % i == 0) {
      m = i;
      break;
    }
  }
  if (m == n)
    return true;
  else
    return false;
}

int main() {

  int n;
  while (1) {

    cin >> n;

    if (cin.fail())
      break;

    int f = floor(sqrt(1.0 * n));

    for (int i = 2; i <= n; i++)
      a[i] = 1;

    for (int i = 2; i <= f; i++) {
      if (a[i] != 0) {
        if (p(i)) {
          for (int j = 2 * i; j <= n; j += i)
            a[j] = 0;
        }
      }
    }

    int x = 0;
    for (int i = 2; i <= n; i++) {
      if (a[i] != 0)
        x++;
    }

    cout << x << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

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

using namespace std;

int a[1000000];

bool p(int n) {
  int m;
  for (int i = 2; i <= n; i++) {
    if (n % i == 0) {
      m = i;
      break;
    }
  }
  if (m == n)
    return true;
  else
    return false;
}

int main() {

  int n;
  while (1) {

    cin >> n;

    if (cin.fail())
      break;

    int f = floor(sqrt(1.0 * n));
    int c = floor(sqrt(1.0 * n) + 1);

    for (int j = c; j <= n; j++)
      a[j] = j;

    int x = 0;
    for (int i = 2; i <= f; i++) {

      if (p(i)) {
        x++;
        for (int j = c; j <= n; j++) {
          if (a[j] % i == 0 && a[j] != 0)
            a[j] = 0;
        }
      }
    }
    int y = 0;
    for (int i = c; i <= n; i++) {
      if (a[i] != 0)
        y++;
    }

    cout << x + y << endl;
  }

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

#include <iostream>

int n;
bool *isPrime;

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

int main() {
  while (std::cin >> n) {
    isPrime = new bool[n + 1];
    std::cout << solve() << std::endl;
    delete[] isPrime;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

bool isPrime(int a) {
  int x = std::sqrt((double)a);
  for (; x > 1; x -= 2) {
    if (a % x == 0)
      return false;
  }
  return true;
}

int countPrime(int a) {
  int x = a, ans = 1;
  if (x % 2 == 0)
    x--;
  for (; x > 1; x -= 2) {
    if (isPrime(x))
      ans++;
  }
  return ans;
}

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

#include <iostream>

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

using namespace std;

int main(void) {
  const int N = (int)1e6;
  bool num[N];
  rep(i, N) num[i] = true;
  for (int i = 2; i * i < N; i++)
    if (num[i])
      for (int j = i; i * j < N; j++)
        num[i * j] = false;

  for (int n, count = 0; cin >> n; cout << count << endl, count = 0)
    rep(i, n + 1) if (num[i]) count++;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

bool judge(int);

int main() {
  const int MAX_VALUE = (int)1e7;
  vector<bool> primeNums(MAX_VALUE);

  for (int i = 1; i < MAX_VALUE; i++)
    primeNums[i] = judge(i);

  for (int n, count = 0; cin >> n; cout << count << endl, count = 0)
    for (int i = 1; i <= n; i++)
      if (primeNums[i])
        count++;

  return 0;
}

bool judge(int n) {
  if (n == 2 || n == 3)
    return true;
  if (n == 1 || n % 2 == 0)
    return false;
  for (int i = 3; i * i <= n; i += 2)
    if (n % i == 0)
      return false;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

bool isPrime(int num);

int main() {
  int count = 0;
  /*int m = 2, n = 0, num;

  cin >> num;

  int *data = new int[num + 1];
  int *result = new int[num + 1];

  data[0] = 0;
  data[1] = 0;

  do
  {
          for (i = 2 * m; i < num + 1; i += m)
          {

          }
  }
  while
  {

  }*/
  const int N = 9999999;
  int n;
  std::vector<bool> isPrime(N + 1, true); // ?´???°????????°?????¨??¨ON???

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

        for (int k = i + i; k <= n; k += i)
          isPrime[k] =
              false; // ?´???°????????°????´???°????????°?????¨???OFF???
      }
    }

    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

bool isPrime(int num);

int main() {
  int input, count = 0;

  while (cin >> input) {
    count = 0;

    for (int i = 2; i <= input; i++) {
      if (isPrime(i))

        count++;
    }
    cout << count << endl;
  }
}

bool isPrime(int num) {
  if (num < 2)
    return false;
  else if (num == 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);

  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0)
      return false;
  }

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

#include <algorithm>
#include <bits/stdc++.h>
#include <list>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

void pr(int n) {
  int ans = 0;
  vector<bool> prime;
  for (int i = 0; i <= n; i++) {
    prime.push_back(true);
  }
  prime[0] = prime[1] = false;

  for (int i = 2; i <= n; i++) {
    if (prime[i]) {
      for (int j = 2; j * i <= n; j++) {
        prime[i * j] = false;
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (prime[i]) {
      ans++;
    }
  }
  printf("%d\\n", ans);
}

int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    pr(n);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <list>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

bool pr(int p) {
  int x = 2;
  while (x * x <= p) {
    if (p % x == 0) {
      return false;
    }
    x++;
  }
  return true;
}

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

#include <iostream>
#include <memory.h>
using namespace std;
int main() {
  bool prime[1000010];
  memset(prime, 1, 1000010);
  prime[0] = prime[1] = 0;
  int i, j, n, sum;
  for (i = 2; i < 1000000; i++) {
    if (prime[i]) {
      for (j = 2; i * j < 1000000; j++) {
        prime[i * j] = false;
      }
    }
  }
  while (cin >> n) {
    sum = 0;
    for (i = 2; i <= n; i++) {
      sum += prime[i];
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <iostream>
#include <stdlib.h>
using namespace std;
#define thefor(i, end) for (i = 0; i < end; i++)
int main() {
  int n, con, a, i, j;
  while (cin >> n) {
    con = 1;
    for (i = 3; i <= n; i++) {
      for (j = 2; j < i; j++) {
        if (i % j == 0) {
          break;
        }
      }
      if (j == i) {
        con++;
      }
    }
    cout << con << endl;
  }
}