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

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

int prime(int number) {
  int s_num = sqrt(number);
  if (number == 2) {
    return 1;
  } else if (number % 2 == 0) {
    return 0;
  }
  for (int i = 3; i <= s_num; i += 2) {
    if (number % i == 0) {
      return 0;
    }
  }
  return 1;
}

int main(void) {
  int number;
  int count = 0;
  int max = 1;

  std::vector<int> v(1000000, 0);

  while (std::cin >> number) {
    if (number > max) {
      for (int i = max + 1; i <= number; i++) {
        if (prime(i) == 1) {
          count++;
        }
        v[i - 1] = count;
        max = number;
      }
    }
    std::cout << v[number - 1] << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

int prime(int number) {
  int s_num = sqrt(number);
  if (number == 2) {
    return 1;
  } else if (number % 2 == 0) {
    return 0;
  }
  for (int i = 3; i <= s_num; i += 2) {
    if (number % i == 0) {
      return 0;
    }
  }
  return 1;
}

int main(void) {
  int number;
  int count;

  while (std::cin >> number) {
    count = 0;
    for (int i = 2; i <= number; i++) {
      if (prime(i) == 1) {
        count++;
      }
    }
    std::cout << count << std::endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stdio.h>
#define DB(x)
int prime[80000];
int GetPrime(int num);

int main() {
  int count = GetPrime(1000000);
  DB(printf("%d\\n", count));
  int n, ans[100], cor = 0;
  int i;
  while (scanf("%d", &n) != EOF) {
    for (i = 0; n >= prime[i] && i < count; i++)
      ;
    ans[cor] = i;
    cor++;
  }
  for (i = 0; i < cor; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

int GetPrime(int num) {
  int i, j, count = 0;
  int sqnum = sqrt((double)num);
  for (i = 2; i <= num; i++) {
    for (j = 0; j < count; j++) {
      if (prime[j] > sqnum)
        goto COUNT;
      if (i % prime[j] == 0)
        break;
    }
    if (j < count)
      continue;
  COUNT:
    prime[count] = i;
    count++;
  }
  return count;
}

Error: WA

Fixed submission:

#include <math.h>
#include <stdio.h>

int pnum[350000];
int main() {
  int n, data[30] = {0};
  int pcorsole = 0, dcorsole = 0;
  int i, j;
  int root;
  while (scanf("%d", &n) != EOF) {
    for (i = 2; i <= n; i++) {
      root = (int)sqrt((double)i);
      for (j = 0; j < pcorsole && pnum[j] < root; j++) {
        if (i % pnum[j] == 0)
          break;
      }
      if (j == pcorsole || pnum[j] >= root) {
        pnum[pcorsole] = i;
        pcorsole++;
        data[dcorsole]++;
      }
    }
    dcorsole++;
  }
  for (i = 0; i <= dcorsole; i++) {
    printf("%d\\n", data[i]);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    bool prime[1000000];
    for (int l = 2; l <= n; l++) {
      prime[l] = true;
    }
    for (int i = 2; i <= n; i++) {
      if (prime[i] == true) {
        for (int j = i * 2; j <= n; j += i) {
          prime[j] = false;
        }
      }
    }
    int count = 0;
    for (int k = 2; k <= n; k++) {
      if (prime[k] == true)
        count++;
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    bool prime[1000000];
    for (int l = 2; l <= n; l++) {
      prime[l] = true;
    }
    /* prime[0]=false;
       prime[1]=false; */
    for (int i = 2; i <= n; i++) {
      if (prime[i] == true) {
        for (int j = i + 1; j <= n; j++) {
          if (j % i == 0)
            prime[j] = false;
        }
      }
    }
    int count = 0;
    for (int k = 2; k <= n; k++) {
      if (prime[k] == true)
        count++;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define MAX 1000000
using namespace std;

class prime {
  int sum;
  char p[MAX];

public:
  prime();
  int num(int n);
};

prime::prime() {
  p[0] = 0;
  p[1] = 0;
  p[2] = 1;
  for (int i = 3; i < MAX; i += 2)
    p[i] = 1;
  for (int i = 4; i < MAX; i += 2)
    p[i] = 0;

  int c = 3;
  while (c < 1000) {
    for (int i = c + 2; i < MAX; i += 2) {
      if (i % c == 0)
        p[i] = 0;
    }
    while (p[++c] == 0)
      ;
  }
}

int prime::num(int n) {
  sum = 0;
  for (int i = 0; i <= n; i++)
    sum += p[i];
  return sum;
}

int main() {
  int n;
  prime pr;
  while (cin >> n) {
    cout << pr.num(n) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

class prime {
  int cnt;
  vector<int> p;

public:
  prime();
  int num(int n);
};

prime::prime() { p.clear(); }

int prime::num(int n) {
  if (n < 2)
    return 0;
  if (n == 2)
    return 1;
  p.push_back(2);
  int c;
  cnt = 1;
  for (int i = 3; i <= n; i += 2) {
    c = 0;
    for (int j = 0; j < (int)p.size(); j++) {
      if (i % p[j] == 0) {
        c++;
        break;
      }
    }
    if (c == 0) {
      p.push_back(i);
      cnt++;
    }
  }
  return cnt;
}

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

#include <iostream>
using namespace std;
const int Max = 10000000;
bool prime[Max];
int main() {
  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 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  while (cin) {
    int n;
    cin >> n;

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

#include <math.h>
#include <stdio.h>

int main(void) {
  // 入力変数
  int n = 0;
  // 素数判定flag
  bool isPrime = true;
  // 素数カウント変数
  int count = 0;
  // 素数格納配列
  int prime[100000];
  // ループの最高回数
  int imax = 0;

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

    // 2は特別扱い
    if (n >= 2) {
      prime[0] = 2;
      count++;
    }

    // 2の倍数はいらない
    for (int j = 3; j <= n; j += 2) {
      // 素数かそうでないかの判断をするflag
      isPrime = true;

      // 平方根を求める
      imax = sqrt(double(j));

      // ここでエラトステネスのふるいで使った平方根の考え方を利用
      for (int i = 0; prime[i] <= imax; i++) {
        if (j % prime[i] == 0) {
          isPrime = false;
          break;
        }
      }

      // 素数だった場合
      if (isPrime == true) {
        prime[count++] = j;
      }
    }

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

  return 0;
}

Error: TLE

Fixed submission:

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



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

/*
int main(void)
{
        int n;
        vector<int> vecPrime;

        while (cin >> n)
        {
                // vectorの破棄
                vecPrime.clear();

                // nが2以上の時,2のみ追加
                if (n >= 2)
                {
                        vecPrime.push_back(2);
                }

                // 奇数のみ探索
                for (int i = 3; i <= n; i += 2)
                {
                        // 割り切れたらtrueにする
                        bool flagPrime = false;

                        // 奇数しかループさせないため、j = 1からでよい
                        int size = vecPrime.size();
                        for (int j = 1; j < size && vecPrime[j] <
sqrt((double)i); j++)
                        {
                                // 割り切れたら素数でない
                                if (i % vecPrime[j] == 0)
                                {
                                        flagPrime = true;
                                        break;
                                }
                        }
                        // すべて割り切れなかった場合
                        if (flagPrime == false)
                        {
                                vecPrime.push_back(i);
                        }
                }

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

        return 0;
}
*/

#define SIZE 100000

int main(void) {
  int n;
  int prime[SIZE] = {0};

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

    // nが2以上の時,2のみ追加
    if (n >= 2) {
      prime[0] = 2;
      ans++;
    }

    // 奇数のみ探索
    for (int i = 3; i <= n; i += 2) {
      // 割り切れたらtrueにする
      bool flagPrime = false;

      // 奇数しかループさせないため、j = 1からでよい(sqrtの部分が <
      // ではwrongAnswer)
      for (int j = 1; j < ans && prime[j] <= sqrt((double)i); j++) {
        // 割り切れたら素数でない
        if (i % prime[j] == 0) {
          flagPrime = true;
          break;
        }
      }
      // すべて割り切れなかった場合
      if (flagPrime == false) {
        prime[ans] = i;
        ans++;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:



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

int main(void) {
  int n;
  vector<int> vecPrime;

  while (cin >> n) {
    // vectorの破棄
    vecPrime.clear();

    // nが2以上の時,2のみ追加
    if (n >= 2) {
      vecPrime.push_back(2);
    }

    // 奇数のみ探索
    for (int i = 3; i <= n; i += 2) {
      // 割り切れたらtrueにする
      bool flagPrime = false;

      // 奇数しかループさせないため、j = 1からでよい
      int size = vecPrime.size();
      for (int j = 1; j < size && vecPrime[j] < sqrt((double)i); j++) {
        // 割り切れたら素数でない
        if (i % vecPrime[j] == 0) {
          flagPrime = true;
          break;
        }
      }
      // すべて割り切れなかった場合
      if (flagPrime == false) {
        vecPrime.push_back(i);
      }
    }

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

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

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  int count;
  while (cin >> n, !cin.eof()) {
    count = 0;
    vector<int> p(n + 1, 1);
    p[1] = 0;
    for (int i = 2; i * i <= n; i++) {
      if (p[i] == 0)
        continue;
      for (int j = 2; i * j <= n; j++) {
        p[i * j] = 0;
      }
    }
    for (int i = 1; i <= n; i++) {
      count += p[i];
    }
    cout << count << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

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

int countPrime(int x, vector<int> &plist) {
  int count = 0;
  for (int i = 2; i <= x; i++) {
    if (plist[i] == 0)
      continue;
    if (isPrime(i)) {
      count++;
    } else {
      for (int j = i; j < x; j += i) {
        plist[j] = 0;
      }
    }
  }
  return count;
}

int main() {
  int n;
  vector<int> plist(1000000, 1);
  while (cin >> n, !cin.eof()) {

    cout << countPrime(n, plist) << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

#define MAX_MAX 1000010

using namespace std;

bool isChecked[MAX_MAX];
vector<int> prime;

int CountPrimeNum(int max) {

  int count = 0;
  for (int i = 0; i < MAX_MAX; i++) {
    if (prime[i] > max)
      break;
    count++;
  }
  return count;
}

int main() {
  for (int i = 0; i < MAX_MAX; i++) {
    isChecked[i] = false;
  }

  isChecked[0] = true;
  isChecked[1] = true;

  for (int i = 2; i < MAX_MAX; i++) {
    if (isChecked[i])
      continue;

    prime.push_back(i);

    for (int j = 1; j * i < MAX_MAX; j++) {
      isChecked[i * j] = true;
    }
  }

  int n;
  while (cin >> n) {
    cout << CountPrimeNum(n) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

bool isChecked[1000010];
vector<int> prime;

int CountPrimeNum(int max) {

  int count = 0;
  for (int i = 0; i < 1000000; i++) {
    if (prime[i] > max)
      break;
    count++;
  }
  return count;
}

int main() {
  for (int i = 0; i < 1000000; i++) {
    isChecked[i] = false;
  }

  isChecked[0] = true;
  isChecked[1] = true;

  for (int i = 2; i < 1000000; i++) {
    if (isChecked[i])
      continue;

    prime.push_back(i);

    for (int j = 1; j * i < 1000000; j++) {
      isChecked[i * j] = true;
    }
  }

  int n;
  while (cin >> n) {
    cout << CountPrimeNum(n) << endl;
  }

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

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

int main(void) {
  int A[1000000];
  for (int i = 0; i < 1000000; i++) {
    A[i] = 1;
  }
  A[0] = A[1] = 0;
  for (int i = 2; i < sqrt(1000000); i++) {
    if (A[i] == 1) {
      for (int j = 2; i * j < 1000000; j++) {
        A[i * j] = 0;
      }
    }
  }
  int n;
  while (cin >> n) {
    int COUNT = 0;
    for (int i = 1; i <= n; i++) {
      COUNT += A[i];
    }
    cout << COUNT << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

#include <iostream>
#define K 1000
#define M 1000000
using namespace std;

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

Error: TLE

Fixed submission:

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

int main() {
  int cnt, x, n, i, j;
  bool flg;
  double rootx;
  int rootintx;
  while (cin >> n) {
    cnt = 0;
    if (n >= 2)
      cnt++;
    for (x = 3; x <= n; x += 2) {
      flg = true;
      if (x == 2) {
        cnt++;
        continue;
      }
      if (x < 2 || !(x % 2)) {
        continue;
      }
      rootx = sqrt(x);
      rootx = ceil(rootx);
      rootintx = int(rootx);
      for (j = 2; j <= rootintx; j++) {
        if (!(x % j)) {
          flg = false;
          break;
        }
      }
      if (flg)
        cnt++;
    }
    cout << cnt << endl;
    ;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 10000000
bool prime[MAX] = {true};

int countPrime(int n) {
  if (n < 2)
    return 0;
  if (n == 2)
    return 1;
  int ret = 0;
  // fill(data, data + numof(data), 0);
  fill(prime, prime + MAX, true);
  for (int i = 2; i * i <= n; i++) {
    if (prime[i]) {
      for (int j = i * i; j <= n; j = j + i) {
        prime[j] = false;
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    if (prime[i])
      ret++;
  }
  return ret;
}

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <numeric>
#include <vector>
using namespace std;

int main() {
  int N;
  int r;
  int j;
  int count;
  list<int> counter;

  while (!(cin >> N).eof()) {
    if (N == 2) {
      counter.push_back(1);
    } else if (N == 3) {
      counter.push_back(2);
    } else {
      count = 0;
      for (int i = 3; i <= N; i += 2) {
        j = 3;
        do {
          r = i % j;
          if (j != 3 && r == 0)
            break;
          j += 2;
        } while (j <= (i / 2) + 1);
        if (i != 3 && r == 0)
          continue;
        count++;
      }
      counter.push_back(count);
    }
  }
  list<int>::iterator it = counter.begin();
  while (it != counter.end()) {
    cout << *it << endl;
    it++;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

#define MAX 1000000

using namespace std;

int main() {
  char p[MAX] = {1, 1};
  int n, ans;

  for (int i = 2; i * i <= MAX; i++) {
    if (p[i] == 0) {
      for (int j = i * 2; j < MAX; j += i)
        p[j] = 1;
    }
  }

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  vector<int> p;
  int flag, n, ans;

  p.push_back(2);
  p.push_back(3);
  p.push_back(5);

  for (int i = 7; i * i < 999999; i++) {
    flag = 1;
    for (int j = 0; j < p.size(); j++) {
      if (i % p[j] == 0) {
        flag = 0;
        break;
      }
    }
    if (flag)
      p.push_back(i);
  }

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

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

#include <iostream>
#include <stdio.h>
#define M 1000000
using namespace std;
int a[M] = {0};
int main() {
  int n, i, j, k = 0, l;

  a[0] = 1;
  a[1] = 1;
  for (i = 2; i <= M; i++) {
    for (j = 2; i * j < M && a[i] != 1; j++) {
      a[i * j] = 1;
    }
  }
  while (cin >> n) {
    k = 0;

    for (i = 1; i <= n; i++) {
      if (a[i] == 0)
        k++;
    }
    cout << k << endl;
    // k=0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>
int prime(int x) {
  int i, j = 1;
  int l;
  l = sqrt(x);
  for (i = 2; i <= l + 1; i++) {
    if (x % i == 0) {
      j = 0;
      break;
    }
  }
  // if(j==0)return 0;
  if (x == 2 || j == 1)
    return 1;
}
using namespace std;
int main() {
  int n; //=999999;
  // cout<<n<<endl;
  int j, i;
  int s = 0;
  while (cin >> n) {
    s = 0;

    for (i = 2; i <= n; i++) {
      if (prime(i) == 1) {
        s++;
      } // cout<<i<<endl;}
    }
    cout << s << endl;
  }

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

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

int main() {
  int n;
  while (cin >> n) {
    bool *table = new bool[n + 1];
    for (int i = 0; i <= n; ++i) {
      table[i] = false;
    }
    if (n >= 2)
      table[2] = true;

    for (int i = 3; i <= n; i += 2) {
      table[i] = true;
    }
    for (int i = 3; i <= sqrt(static_cast<double>(n)); i += 2) {
      if (!table[i])
        continue;
      for (int j = i + i; j <= n; j += i) {
        table[j] = false;
      }
    }

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

    cout << cnt << endl;

    delete[] table;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  while (cin >> n) {
    int cnt = 0;
    if (n >= 2)
      ++cnt;
    for (int i = 3; i <= n; i += 2) {
      bool prime = true;
      for (int j = 3; j <= sqrt(static_cast<double>(i)); j += 2) {
        if (!(i % j)) {
          prime = false;
          break;
        }
      }
      if (prime)
        ++cnt;
    }

    cout << cnt << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define Max 1000000

int isPrime[Max];

int main() {
  int num;
  int i, j;

  // 配列の初期化
  isPrime[0] = isPrime[1] = 0;
  for (i = 2; i < Max; ++i) {
    isPrime[i] = 1;
  }

  for (i = 2; i < Max; i++) {
    if (isPrime[i] == 1) {
      for (j = 2; i * j <= Max; ++j) {
        isPrime[i * j] = 0;
      }
    }
  }

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


Error: TLE

Fixed submission:

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

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

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

  return true;
}

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

#include <cmath>
#include <iostream>

void setPrimesUnder(int n, bool *p) {
  if (n > 2) {
    setPrimesUnder(sqrt(n), p);
    for (int i = 2; i <= sqrt(n); i++) {
      if (!p[i])                             // if i is not a prime
        continue;                            // i++
      for (int j = 2; j <= 1000000 / i; j++) // if i is a prime
        p[i * j] = false; // the other multiples of i is not a prime
    }
  } else {
    p[0] = false;
    p[1] = false;
    p[2] = true;
  }
}

int main() {
  int n;
  bool p[1000000];
  for (int i = 0; i < 1000000; i++)
    p[i] = true;
  setPrimesUnder(1000000, p);

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

Error: TLE

Fixed submission:

#include <iostream>

int main() {
  int n, count;
  while (std::cin >> n) {
    count = 0;
    for (int i = 3; i <= n;
         i++) { // check whether i is a prime number greater than 3
      for (int j = 2; j < i; j++) { // judge from whether j is an i's divisor
        if (i % j == 0)             // if j is an i's divisor
          break;                    // exit the j-loop & start to check suc(i)
        if (j == i - 1)             // when the j-loop lasts until i-1
          count++;                  // j is a prime!
      }
    }
    if (n >= 2)
      count++; // 2 is also a prime
    std::cout << count << std::endl;
  }
  return 0;
}