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

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

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

int main() {
  const int MAX_NUM{1000000};
  int NUM{0};
  std::vector<bool> vPrime(MAX_NUM, false);
  for (int i{0}; i < MAX_NUM; ++i) {
    vPrime[i] = isPrime(i);
  }

  while (std::cin >> NUM) {
    int cntPrime{0};
    for (int i = 0; i <= NUM; ++i) {
      if (vPrime[i]) {
        ++cntPrime;
      }
    }
    std::cout << cntPrime << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>

int main() {
  int NUM;
  while (std::cin >> NUM) {
    std::list<int> numList, primeList;
    for (int i = 2; i <= NUM; ++i) {
      numList.push_back(i);
    }
    for (int i = numList.front(); i * i < NUM; i = numList.front()) {
      primeList.push_back(i);
      numList.erase(std::remove_if(numList.begin(), numList.end(),
                                   [&](int num) { return num % i == 0; }),
                    numList.end());
    }
    int cnt = numList.size() + primeList.size();
    std::cout << cnt << std::endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a[1000000]{};
  bool b[1000000]{};
  a[1] = 0;
  int s = 0;
  for (int c = 2; c < 1000000; c++) {
    if (b[c] == false) {
      s++;
      for (int d = c * 2; d < 1000000; d += c)
        b[d] = true;
    }
    a[c] = s;
  }
  int k;
  while (cin >> k) {
    printf("%d\\n", a[k]);
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int a[1000000]{};
  bool b[1000000]{};
  a[1] = 0;
  int s = 0;
  for (int c = 2; c < 1000000; c++) {
    if (!b[c]) {
      s++;
      for (int d = c * 2; c < 1000000; d += c)
        b[d] = true;
    }
    a[c] = s;
  }
  int k;
  while (cin >> k) {
    printf("%d\\n", a[k]);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(void) {

  int i, j, n, count = 0;
  bool arr[999999] = {false};

  while (cin >> n) {

    for (i = 2; i <= n; i++) {
      arr[i] = true;
    }

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

    for (i = 2; i <= n; i++) {
      if (arr[i] == true)
        count++;
    }

    cout << count << endl;
    count = 0;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(void) {

  int i, j, n, count = 0;
  bool arr[99999] = {false};

  while (cin >> n) {

    for (i = 2; i <= n; i++) {
      arr[i] = true;
    }

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

    for (i = 2; i <= n; i++) {
      if (arr[i] == true)
        count++;
    }

    cout << count << endl;
    count = 0;
  }

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

#include <iostream>

int main() {
  int n, count = 0;
  bool isprime[1000000];
  for (int i = 0; i < 1000000; i++)
    isprime[i] = true;
  isprime[0] = isprime[1] = false;
  for (int i = 2; i <= 1000; i++) {
    if (isprime[i]) {
      for (int j = i * 2; j < 1000000; j += i) {
        isprime[j] = false;
      }
    }
  }
  while (std::cin >> n) {
    count = 0;
    for (int i = 2; i <= n; i++) {
      if (isprime[i])
        count++;
    }
    std::cout << count << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

#include <stdio.h>
int flg[1000000];
int main(void) {
  int n, i, j, x;

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

Error: TLE

Fixed submission:

#include <stdio.h>
int main(void) {
  int n, i, j, z, x;

  while (scanf("%d", &n) != EOF) {
    for (i = 2; i <= n; i++) {
      z = 0;
      for (j = 2; j * j <= i; j++) {
        if (i % j == 0) {
          z++;
          break;
        }
      }
      if (z == 0) {
        x++;
      }
    }
    printf("%d\\n", x);
    x = 0;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  long long int n;
  while (cin >> n) {
    int m = 0;
    long long int prime[2000000];
    bool is_prime[2000000];
    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]) {
        m++;
        for (int j = i * 2; j <= n; j += i) {
          is_prime[j] = false;
        }
      }
    }
    cout << m << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  long long int n;
  while (cin >> n) {
    int m = 0;
    long long int prime[200000];
    bool is_prime[200000];
    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]) {
        m++;
        for (int j = i * 2; j <= n; j += i) {
          is_prime[j] = false;
        }
      }
    }
    cout << m << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  ios::sync_with_stdio(false);

  int n;
  int count;
  int nmax = 1000000;
  vector<bool> flag(nmax + 1, true);

  flag[0] = !flag[0];
  flag[1] = !flag[1];

  for (int i = 2; i <= sqrt(nmax) + 1; i++) {
    if (flag[i]) {
      for (int j = i * 2; j <= nmax; j += i) {
        flag[j] = false;
      }
    }
  }

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int PrimeCount(int n) {
  int count = 0;
  bool s;

  if (n > 2) {
    count = 1;

    for (int i = 3; i <= n; i += 2) {
      s = true;
      for (int j = 3; j <= sqrt(i); j += 2) {
        if (i % j == 0) {
          s = !s;
          break;
        }
      }

      if (s) {
        count++;
      }
    }
  }

  return count;
}

int main() {
  ios::sync_with_stdio(false);

  int n;

  while (cin >> n) {
    if (n == 1)
      cout << 0 << endl;
    else if (n == 2)
      cout << 1 << endl;
    else
      printf("%d\\n", PrimeCount(n));
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool sosu_hantei(int num) {
  int i;
  for (i = 2; i <= sqrt((double)num); i++) {
    if (num % i == 0)
      return false;
  }

  if (num == 4)
    return false;

  return true;
}

int main() {
  int n, i, count = 0;
  bool sosu[N];

  for (i = 0; i < N; i++) {

    sosu[i] = sosu_hantei(i);
  }

  while (cin >> n) {

    for (i = 2; i <= n; i++) {
      if (sosu[i]) {
        count++;
      }
    }

    cout << count << endl;
    i = 2;
    count = 0;
  }
}

Error: TLE

Fixed submission:

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

int sosu(int num) {
  int i;

  for (i = 2; i <= sqrt((double)num); i++) {
    if (num % i == 0)
      return 0;
  }

  if (num == 4)
    return 0;

  return 1;
}

int main() {
  int n, i, count;

  while (cin >> n) {

    for (i = 2; i <= n; i++) {
      if (sosu(i)) {
        count++;
      }
    }

    cout << count << endl;
    i = 2;
    count = 0;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <math.h>
#define MAX 1000000
using namespace std;
int main() {
  bool p[MAX + 1];
  int n, count;
  for (int i = 2; i <= MAX; i++)
    p[i] = true;
  for (int i = 2; i <= sqrt(MAX); i++) {
    if (p[i] == true) {
      for (int j = 2 * i; j <= MAX; j++) {
        if (j % i == 0)
          p[j] = false;
      }
    }
  }
  while (cin >> n) {
    count = 0;
    for (int i = 0; i <= n; i++) {
      if (p[i] == true)
        count++;
    }
    cout << count << endl;
  }
}

Error: 0

Fixed submission:

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

#include <stdio.h>
const int MAX_V = 999999;
int main() {
  int prime[MAX_V + 1] = {0};
  int sum[MAX_V + 2] = {0};
  int i, k, v, n;
  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 (i = 0; i <= MAX_V + 1; i++) {
    sum[i + 1] += sum[i] + prime[i];
  }

  while (scanf("%d", &n) != EOF) {
    printf("%d\\n", sum[n + 1]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
const int MAX_V = 999999;
int main() {
  int prime[MAX_V + 1] = {0};
  int sum[MAX_V + 2] = {0};
  int i, k, v, n;
  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 (i = 0; i <= MAX_V + 1; i++) {
    sum[i + 1] += sum[i] + prime[i];
  }

  while (scanf("%d", &n)) {
    if (n == 0)
      break;
    printf("%d\\n", sum[n + 1]);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <list>
#include <vector>

#define MAX 1000000

using namespace std;

int main() {
  vector<int> prime_numbers;
  prime_numbers.push_back(2);
  prime_numbers.push_back(3);
  prime_numbers.push_back(5);
  prime_numbers.push_back(7);

  list<int> search_list;
  for (int i = 5; i < MAX; i++) {
    if (i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0) {
      search_list.push_back(i);
    }
  }
  int h = search_list.front();
  while (h * h < search_list.back()) {
    prime_numbers.push_back(h);
    list<int>::iterator itr = search_list.begin();
    while (itr != search_list.end()) {
      if (*itr % h == 0) {
        itr = search_list.erase(itr);
      } else {
        itr++;
      }
    }
    h = search_list.front();
  }
  for (list<int>::iterator itr = search_list.begin(); itr != search_list.end();
       itr++) {
    prime_numbers.push_back(*itr);
  }

  int n;
  while (cin >> n) {
    int count = 0;
    for (vector<int>::iterator itr = prime_numbers.begin();
         itr != prime_numbers.end(); itr++) {
      if (*itr > n)
        break;
      count++;
    }
    cout << count << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  int count;
  while (cin >> n) {
    if (n == 1) {
      cout << 0 << endl;
      continue;
    }
    vector<int> prime_numbers; // 素数リスト
    count = 1;
    for (int i = 2; i <= n; i++) {
      bool prime = true;
      for (vector<int>::iterator itr = prime_numbers.begin();
           itr != prime_numbers.end(); itr++) {
        if (i % *itr == 0) {
          prime = false;
          break;
        }
      }
      if (prime) {
        prime_numbers.push_back(i);
        count += 1;
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define N 1000001
using namespace std;
int main() {
  int n;
  int p[N] = {0};
  for (int i = 3; i <= N; i += 2)
    p[i] = 1;
  p[2] = 1;
  for (int i = 3; i <= 1000; i += 2) {
    if (p[i] == 1) {
      for (int j = 2 * i; j <= N; j += i)
        p[j] = 0;
    }
  }
  while (cin >> n) {
    int count = 0;
    if (2 <= n)
      count++;
    for (int i = 3; i <= n; i += 2) {
      if (p[i])
        count++;
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#include <iostream>
using namespace std;

int main() {
  bool is_prime[1000000];
  for (int i = 0; i < 1000000; i++) {
    is_prime[i] = true;
  }
  is_prime[0] = false;
  is_prime[1] = false;

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

  int n, counter;
  while (cin >> n) {
    counter = 0;

    for (int k = 1; k <= n; k++)
      if (is_prime[k]) {
        counter++;
      }

    cout << counter << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, i, j, x, counter;
  while (cin >> n) {
    counter = 0;

    for (i = 1; i <= n; i++) {
      x = 0;

      for (j = 1; j <= i; j++) {
        if (i % j == 0)
          x++;
      }

      if (x == 2)
        counter++;
    }

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

#include <iostream>

using namespace std;

#define MAX_N (1000000)

bool is_prime[MAX_N];
int sum[MAX_N + 1];

int main() {
  int n;

  for (int i = 0; i < MAX_N; i++) {
    is_prime[i] = true;
  }

  is_prime[0] = false;
  is_prime[1] = false;

  for (int i = 4; i < MAX_N; i += 2) {
    is_prime[i] = false;
  }

  for (int i = 3; i * i < MAX_N; i += 2) {
    for (int j = i + i; j < MAX_N; j += i) {
      is_prime[j] = false;
    }
  }

  sum[0] = 0;
  for (int i = 0; i < MAX_N; i++) {
    sum[i + 1] = sum[i] + is_prime[i];
  }

  while (cin >> n) {
    cout << sum[n + 1] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

int prime_number_count(const int n) {
  int cnt;
  bool was_not_divisible;

  if (n < 2) {
    return 0;
  }

  cnt = 1;
  for (int i = 3; i <= n; i += 2) { // dividend: 被除数
    was_not_divisible = true;
    for (int j = 3; j * j <= i; j += 2) { // divisor: 除数
      if (i % j == 0) {
        was_not_divisible = false;
        break;
      }
    }
    if (was_not_divisible) {
      cnt++;
    }
  }

  return cnt;
}

int main() {
  int n;

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

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

#include <cmath>
#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;

bool isPrime[1000000];

void setIsPrime(int x) {
  isPrime[1] = true;
  for (int i = 1; i < x; i++) {
    int n = (int)sqrt(i);
    int yakusu = 0;
    for (int j = 1; j <= n; j++) {
      if (i % j == 0)
        yakusu++;
      if (yakusu > 2)
        break;
    }
    if (yakusu == 1) {
      isPrime[i] = true;
    }
  }
}

int numPrimes(int x) {
  int cnt = 0;
  for (int n = 1; n <= x; n++) {
    if (isPrime[n])
      cnt++;
  }
  return cnt - 1;
}

int main() {
  int x;
  setIsPrime(1000000);
  while (cin >> x) {
    cout << numPrimes(x) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;

int prime(int x) {
  int ret = 0;
  for (int i = x; i > 1; i--) {
    int n = (int)sqrt(i);
    int yakusu = 0;
    for (int j = 1; j <= n; j++) {
      if (i % j == 0)
        yakusu++;
    }
    if (yakusu == 1) {
      ret++;
    }
  }
  return ret;
}

int main() {
  int x;
  while (cin >> x) {
    cout << prime(x) << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

#define N 1000000

using namespace std;

int main() {
  bool prm[N];
  for (int i = 0; i < N; i++)
    prm[i] = true;
  int upto = (int)sqrt(N);
  for (int i = 2; i <= upto; i++) {
    if (prm[i]) {
      for (int j = 2; i * j <= N; j++)
        prm[i * j] = false;
    }
  }
  int n;
  while (cin >> n) {
    int count = 0;
    for (int i = 2; i <= n; i++) {
      if (prm[i])
        count++;
    }
    cout << count << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  bool prm[N];
  for (int i = 0; i < N; i++) {
    prm[i] = true;
    int upto = (int)sqrt(N);
    for (int i = 2; i <= upto; i++) {
      if (prm[i])
        for (int j = 2; j * i < N; j++) {
          prm[i * j] = false;
        }
    }
    int n;
    while (cin >> n) {
      int count = 0;
      for (int i = 2; i <= n; i++) {
        if (prm[i])
          count++;
      }
      cout << count << endl;
    }
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iostream>
#include <map> // pair
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define MAXSIZE 200000
#define PI 3.141592653589
using namespace std;

template <typename T> T add(T a, T b) { return a + b; }

namespace A {
int f(void) { return 1; }
} // namespace A

// int main(){
//   int n;
//   while (std::cin >> n) {
//     int count=0;//素数の数
//     for(int i=2;i<=n;i++){
//       int flag=0;
//       if(i==2){
//         count++;
//         continue;
//       }else if(i%2==0){
//         continue;
//       }
//
//       for(int j=3;j<=sqrt(i);j=j+2){
//         if(i%j==0){
//           flag=1;
//           break;
//         }
//       }
//       if(flag==0){
//         count++;
//       }
//     }
//     std::cout << count << '\\n';
//   }
//
// }

int main() {
  int a, t[1000000], u[1000000];
  for (int i = 0; i < 1000000; i++) {
    t[i] = 1;
  }
  t[0] = t[1] = u[0] = u[1] = 0;
  for (int i = 2; i <= 1000000; i++) {
    if (t[i] == 1) {
      for (int j = i + i; j < 1000000; j += i)
        t[j] = 0;
      u[i] = u[i - 1] + 1;
    } else {
      u[i] = u[i - 1];
    }
  }
  while (cin >> a) {
    cout << u[a] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iostream>
#include <map> // pair
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define MAXSIZE 200000
#define PI 3.141592653589
using namespace std;

template <typename T> T add(T a, T b) { return a + b; }

namespace A {
int f(void) { return 1; }
} // namespace A

int main() {
  int n;
  while (std::cin >> n) {
    int count = 0; // 素数の数
    for (int i = 2; i <= n; i++) {
      int flag = 0;
      if (i == 2) {
        count++;
        continue;
      } else if (i % 2 == 0) {
        continue;
      }

      for (int j = 3; j <= sqrt(i); j = j + 2) {
        if (i % j == 0) {
          flag = 1;
          break;
        }
      }
      if (flag == 0) {
        count++;
      }
    }
    std::cout << count << '\\n';
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int ans[1000000];
  int count = 2;

  for (int i = 4; i < 1000000; i++) {
    bool p = true;
    double sqrti = i;
    for (int j = 2; j <= sqrt(sqrti); j++) {
      if (i % j == 0) {
        p = false;
        break;
      }
    }
    if (p) {
      count++;
      ans[i] = count;
    } else
      ans[i] = count;
  }
  ans[1] = 0;
  ans[2] = 1;
  ans[3] = 2;

  int n;

  while (scanf("%d", &n) == 1) {
    printf("%d\\n", ans[n]);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;

  while (scanf("%d", &n) == 1) {
    int count = 0;
    double sqrtn = n;
    for (int i = 3; i <= n; i += 2) {
      bool p = true;
      for (int j = 3; j < sqrt(sqrtn); j++) {
        if (i % j == 0) {
          p = false;
          break;
        }
      }
      if (p)
        count++;
    }
    printf("%d\\n", count + 1);
  }
  return 0;
}