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

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

using namespace std;

bool num[1000000] = {false};

void primNumberCheck() {
  num[2] =
      true; //?\\???°?????????????????????????????????2???????????§???????????????

  int i, j, k;
  for (i = 3; i <= 1000000; i += 2) // 2????????°???????????????
  {
    k = 0;
    for (
        j = 3; j <= sqrt((double)i);
        j +=
        2) //?????????i??\\??????i????´???°??????????????\\??????i????´???°??¨?????????????????§????????????
    {
      if (i % j == 0) {
        k = 1;
        break;
      }
    }
    if (k == 0) {
      num[i] = true;
    }
  }
}

int main() {
  primNumberCheck();

  int n;

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int result = 0;
    for (int i = 2; i <= n; i++) {
      for (int j = 2; j <= i; j++) {
        if (i == j)
          result++;
        if (i % j == 0)
          break;
      }
    }
    cout << result << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_VALUE = 1000007;
bool prime[MAX_VALUE];

int main() {
  FOR(i, 2, MAX_VALUE) {
    if (prime[i])
      continue;
    for (int j = i * 2; j < MAX_VALUE; j += i)
      prime[j] = true;
  }

  int n;
  while (cin >> n) {
    int cnt = 0;
    FOR(i, 2, n + 1) {
      if (!prime[i])
        cnt++;
    }
    cout << cnt << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_VALUE = 100007;
bool prime[MAX_VALUE];

int main() {
  FOR(i, 2, MAX_VALUE) {
    if (prime[i])
      continue;
    for (int j = i * 2; j < MAX_VALUE; j += i)
      prime[j] = true;
  }

  int n;
  while (cin >> n) {
    int cnt = 0;
    FOR(i, 2, n + 1) {
      if (!prime[i])
        cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int main() {
  static int N, count[1000009];
  count[1] = 1;
  for (int x = 3; x <= 1000000; x = x + 2) {
    int flag = 0;
    for (int y = 3; y <= sqrt(x); y = y + 2) {
      if ((x % y) == 0)
        flag = 1;
    }
    count[x] = count[x - 2];
    count[x - 1] = count[x - 2];
    if (flag == 0)
      count[x]++;
  }
  count[1] = 0;
  while (cin >> N) {
    cout << count[N] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long long int N, count = 0;
  while (cin >> N) {
    count = 0;
    if (N < 0)
      break;
    for (int x = 2; x <= N; x++) {
      int flag = 0;
      for (int y = 2; y < x; y++) {
        if ((x % y) == 0)
          flag = 1;
      }
      if (flag == 0)
        count++;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  bool p[MAX + 1];
  int n, ans;

  for (int i = 0; i <= MAX; i++) {
    p[i] = (i <= 1) ? false : true;
  }

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

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

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int c, g;
  bool prime[30];
  for (int i = 0; i <= 30; i++)
    prime[i] = true;
  prime[0] = prime[1] = false;
  for (int i = 2; i <= 30; i++) {
    if (prime[i]) {
      for (int j = i * i; j <= 30; j += i)
        prime[j] = false;
    }
  }

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

    for (int i = 0; i <= g; i++) {
      if (prime[i])
        c++;
    }
    cout << c << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
const int inf = 1e9;
const ll linf = 1LL << 50;

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

int main(int argc, char const *argv[]) {
  fill(is_prime, is_prime + 1000000, true);
  is_prime[0] = false;
  is_prime[1] = false;
  int res = 0;
  for (int i = 2; i < 1000000; i++) {
    if (is_prime[i]) {
      res++;
      for (int j = 2; j * i < 1000000; j++) {
        is_prime[j * i] = false;
      }
    }
    prime[i] = res;
  }
  while (cin >> n) {
    cout << prime[n] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
const int inf = 1e9;
const ll linf = 1LL << 50;

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

int main(int argc, char const *argv[]) {
  fill(is_prime, is_prime + 100000, true);
  is_prime[0] = false;
  is_prime[1] = false;
  int res = 0;
  for (int i = 2; i < 100000; i++) {
    if (is_prime[i]) {
      res++;
      for (int j = 2; j * i < 100000; j++) {
        is_prime[j * i] = false;
      }
    }
    prime[i] = res;
  }
  while (cin >> n) {
    cout << prime[n] << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, i, j, k = 1, wareru = 0;
  int sosu[500000];
  sosu[0] = 2;
  for (i = 3; i < 1000000; i += 2) {
    wareru = 0;
    for (j = 3; j <= sqrt(i * 1.0); j += 2) {
      if (i % j == 0) {
        wareru = 1;
        break;
      }
    }
    if (wareru == 0) {
      sosu[k++] = i;
    }
  }

  while (scanf("%d", &n) != EOF) {
    for (i = 0; (sosu[i] <= n) && (sosu[i] != NULL); i++)
      ;
    printf("%d\\n", i);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, i, j, k, cnt;
  while (scanf("%d", &n) != EOF) {
    cnt = 0;
    if (n <= 1) {
      printf("0\\n");
      continue;
    }
    if (n == 2) {
      printf("1\\n");
      continue;
    }
    if (n <= 4) {
      printf("2\\n");
      continue;
    }
    for (j = 5; j <= n; j += 2) {
      k = 0;
      for (i = 3; i <= sqrt(j * 1.0); i += 2) {
        if (j % i == 0) {
          k = -1;
          break;
        }
      }
      if (k == -1)
        continue;
      cnt++;
    }
    printf("%d\\n", cnt + 2);
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int arr[1000000];

int main() {
  int x, cnt, j, i = 3;
  arr[2] = 1;
  for (; i < 1000000; ++i) {
    cnt = 1;
    for (j = 2; j <= sqrt(i); ++j) {
      if (i % j == 0) {
        arr[i] = arr[i - 1];
        --cnt;
        break;
      }
    }
    arr[i] = arr[i - 1] + cnt;
  }
  while (cin >> x) {
    cout << arr[x] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int arr[1000000];

void ar() {
  int cnt;
  arr[2] = 1;
  int j;
  int i = 3;
  for (; i < 1000000; ++i) {
    cnt = 1;
    for (j = 2; j < i; ++j) {
      if (i % j == 0) {
        arr[i] = arr[i - 1];
        --cnt;
        break;
      }
    }
    arr[i] = arr[i - 1] + cnt;
  }
}

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

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

using namespace std;

int setPrimeNumberCount(int n) {
  int count = 0;
  vector<int> primeList;
  bool findList[n + 1];

  if (n >= 2) {
    primeList.push_back(2);
    count++;

    for (int i = 2; i <= n; i++) {
      findList[i] = false;
    }
    for (int i = 2; i <= sqrt(n); i++) {
      if (findList[i]) {
        continue;
      }
      for (int j = i * 2; j <= n; j += i) {
        if (!findList[j]) {
          findList[j] = true;
        }
      }
    }
  }
  for (int i = 3; i <= n; i += 2) {
    if (!findList[i]) {
      primeList.push_back(i);
      count++;
    }
  }
  return count;
}

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

  while (cin >> n) {
    count = setPrimeNumberCount(n);
    cout << count << endl;
    count = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int setPrimeNumberCount(int n) {
  int count = 0;
  vector<int> primeList;
  vector<int> searchList;

  if (n >= 2) {
    for (int i = 2; i <= n; i++) {
      searchList.push_back(i);
    }

    vector<int>::iterator end_it;

    for (int i = 0; i <= n; i++) {
      primeList.push_back(searchList[0]);

      for (vector<int>::iterator it = searchList.begin();
           it != searchList.end();) {
        if (*it % primeList[count] == 0) {
          it = searchList.erase(it);
        } else {
          ++it;
        }
      }
      count++;
      if (pow(primeList[i], 2) > searchList.back()) {
        for (int i = 0; i < searchList.size(); i++) {
          primeList.push_back(searchList[i]);
          count++;
        }
        break;
      }
    }
  }
  return count;
}

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

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

#include <stdio.h>

#define MAX 2000000
#define UNCHECKED 0
#define PRIME 1
#define COMPO 2

bool isPrime(int n);

int main(void) {
  int n;
  int primes;

  while (scanf("%d", &n) != EOF) {
    primes = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime(i))
        primes++;
    }
    printf("%d\\n", primes);
  }
}

bool isPrime(int n) {
  static int table[MAX] = {0};
  static int minPrime = 2;

  if (n < 2)
    return 0;

  do {
    if (table[n] != UNCHECKED) {
      return table[n] == PRIME ? 1 : 0;
    }
    table[minPrime] = PRIME;
    for (int i = 2; i * minPrime <= MAX; i++) {
      table[i * minPrime] = COMPO;
    }
    while (minPrime <= MAX && table[minPrime] != UNCHECKED)
      minPrime++;
  } while (minPrime <= MAX);
}

Error: TLE

Fixed submission:

#include <stdio.h>

bool isPrime(int n);

int main(void) {
  int n;
  int primes;

  while (scanf("%d", &n) != EOF) {
    primes = 0;
    for (int i = 2; i <= n; i++) {
      if (isPrime(i))
        primes++;
    }
    printf("%d\\n", primes);
  }
}

bool isPrime(int n) {
  if (n == 1 || n % 2 == 0)
    return false;
  if (n == 2)
    return true;
  for (int t = 3; t * t <= n; t += 2) {
    if (n % t == 0)
      return false;
  }
  return true;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main(void) {
  int i, j, n;
  bool judge[1000000] = {0};

  for (i = 3; i < 1000000; i++)
    if (i % 2 == 0)
      judge[i] = 1;

  for (i = 3; i < 500000; i += 2) {
    if (judge[i] == 0) {
      for (j = i * 2; j < 1000000; j += i) {
        if (j % i == 0) {
          judge[j] = 1;
        }
      }
    }
  }

  while (scanf("%d", &n) != EOF) {
    int count = 0;
    for (i = 2; i <= n; i++) {
      if (judge[i] == 0)
        count++;
    }
    printf("%d\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main(void) {
  int i, j, n;
  bool judge[1000000] = {0};

  for (i = 3; i < 1000000; i++)
    if (i % 2 == 0)
      judge[i] = 1;

  for (i = 3; i < 500000; i += 2) {
    if (judge[i] == 0) {
      for (j = i * 2; j < 1000000; j += i) {
        if (j % i == 0) {
          judge[j] = 1;
        }
      }
    }
  }

  for (;;) {
    int count = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 3; i <= n; i += 2) {
      if (judge[i] == 0)
        count++;
    }
    printf("%d\\n", count + 1);
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
bool isprime[1000005];
int prime[1000005];
void sieve() {
  for (int i = 0; i <= 1000000; i++)
    isprime[i] = true;
  isprime[0] = isprime[1] = false;
  prime[0] = prime[1] = 0;
  for (int i = 2; i <= 1000000; i++) {
    if (isprime[i]) {
      prime[i] = prime[i - 1] + 1;
      for (int j = 2 * i; j <= 1000000; j += i)
        isprime[j] = false;
    } else
      prime[i] = prime[i - 1];
  }
}
int main() {
  sieve();
  int n;
  while (cin >> n) {
    cout << prime[n] << endl;
  }
}

Error: TLE

Fixed submission:

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

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

  return true;
}
int main() {
  while (cin >> num) {
    int count = 1;
    for (int loop = 3; loop <= num; loop += 2) {
      if (isPrime(loop)) {
        count++;
      }
    }
    cout << count << endl;
  }
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <math.h>
using namespace std;
const int MAX = 1000000;
int prime(int n) {
  int ans = 0;
  bool prime[MAX];
  memset(prime, 0, sizeof(prime));
  for (int i = 2; i < n; i++) {
    for (int j = 2; j <= n / i; j++) {
      if (i * j <= n)
        prime[i * j] = 1;
    }
  }
  for (int i = 2; i <= n; i++) {
    if (!prime[i])
      ans++;
  }
  return ans;
}
int main() {
  int n, m = 0;
  while (cin >> n && m < 30) {
    cout << prime(n) << endl;
    m++;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#include <iostream>
#define Number 1000000
using namespace std;
int main() {
  int n;
  int table[Number] = {0};
  for (int i = 2; i < Number; i++) {
    if (table[i] == 0) {
      for (int j = 2 * i; j < Number; j += i) {
        table[j] = 1;
      }
    }
    table[i] = (table[i] ? 0 : 1) + table[i - 1];
  }
  while (cin >> n) {
    cout << table[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#define FOR(x, s, g) for (int x = (s); x <= (g); x++)
using namespace std;

int main() {
  int n;
  while (cin >> n) {
    int count = 0;
    FOR(i, 2, n) {
      int e = sqrt(i);
      FOR(j, 2, e) {
        if (i % j == 0) {
          goto end;
        }
      }
      count++;
    end:;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
#define MAX 999999

int main(void) {

  int flag[MAX + 1];
  int n, count;

  int i, j;

  for (i = 0; i <= MAX; i++) {
    flag[i] = (i <= 1) ? 0 : 1;
  }

  for (i = 2; i <= (int)sqrt(MAX) + 1; i++) {
    if (flag[i] == 1) {
      for (j = i * 2; j <= MAX; j += i)
        flag[j] = 0;
    }
  }
  // for(i=0;i<=MAX;i++) printf("%d\\n",flag[i]);
  while (scanf("%d", &n) != EOF) {

    count = 0;

    for (i = 2; i <= n; i++) {

      if (flag[i] == 1)
        count++;
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {

  int n;
  int num;
  int i, flag, count;

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

    count = 0;

    for (num = 1; num <= n; num++) {

      flag = 1;

      for (i = 2; i < num / 2 + 1; i++) {
        if (num % i == 0) {
          flag = 0;
          break;
        }
      }

      if (num == 1)
        flag = 0;

      if (flag == 1) {
        printf("%d\\n", num);
        count++;
      }
    }

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

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

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

int const M = 1000010;
int a, hantei[M];

int main() {
  hantei[0] = hantei[1] = 1;
  for (int i = 2; i < M; i++) {
    if (hantei[i] == 0) {
      for (int j = 2; j * i < M; j++) {
        if (j * i < M) {
          hantei[j * i] = 1;
        }
      }
    }
  }
  for (; cin >> a;) {
    int count = 0;
    for (int i = 0; i <= a; i++) {
      if (hantei[i] == 0) {
        count++;
      }
    }
    cout << count << endl;
  }
}

Error: 0

Fixed submission:

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

int const M = 40;
int a, hantei[M];

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

#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
bool isPr(int n);
int main() {
  int a[1000000], n;
  memset(a, 0, sizeof(a));
  a[2] = 1;
  for (int i = 3; i < 1000001; i++)
    a[i] = a[i - 1] + isPr(i);
  while (cin >> n) {
    cout << a[n] << endl;
  }
  return 0;
}
bool isPr(int n) {
  for (int i = sqrt(n) + 1; i > 1; i--) {
    if (!(n % i)) {
      return 0;
    }
  }
  return 1;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define PI acos(-1.0)
#define eps 1e-12
const int INF_INT = 2147483647;
const ll INF_LL = 9223372036854775807LL;
const ll N = 1e6 + 1;
const int MOD = 1e9 + 7;
bool flag = true;
int prime[N];
bool check[N]{0};
int main() {
  //    freopen("C:\\\\Users\\\\Administrator\\\\Desktop\\\\in.txt","r",stdin);
  //    freopen("C:\\\\Users\\\\Administrator\\\\Desktop\\\\out.txt","w",stdout);
  //%%%llaa
  int i, j, n, tot = 0;
  for (i = 2; i < N; i++) {
    if (!check[i]) {
      prime[tot++] = i;
      for (j = i + i; j < N; j += i)
        check[j] = 1;
    }
  }
  while (~scanf("%d", &n) && n) {
    int num = 0;
    i = 0;
    while (prime[i++] <= n)
      num++;
    printf("%d\\n", num);
  }
  return 0;
}

/*

*/
Problem p00009 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> prime;
void init() {
  for (int i = 2; i < 999999; i++) {
    bool isprime = true;
    for (vector<int>::iterator p(prime.begin()); p != prime.end(); p++) {
      if ((*p) * (*p) > i)
        break;
      if (i % *p == 0) {
        isprime = false;
        break;
      }
    }
    if (isprime) {
      prime.push_back(i);
    }
  }
}
int main() {
  int n;
  init();
  while (cin >> n) {
    cout << upper_bound(prime.begin(), prime.end(), n) - prime.begin() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> prime;
void init() {
  for (int i = 2; i < 999999; i++) {
    bool isprime = true;
    for (vector<int>::iterator p(prime.begin()); p != prime.end(); p++) {
      if (i % *p == 0) {
        isprime = false;
        break;
      }
    }
    if (isprime) {
      prime.push_back(i);
    }
  }
}
int main() {
  int n;
  init();
  while (cin >> n) {
    cout << lower_bound(prime.begin(), prime.end(), n) - prime.begin() + 1
         << endl;
  }
  return 0;
}
Problem p00009 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(n) rep(i, n)
#define all(n)
const int MAXN = 1000000;

int p[MAXN];

int main() {
  p[0] = p[1] = 1;
  REP(MAXN) if (!p[i]) for (int j = i * 2; j < MAXN; j += i) p[j] = 1;

  int n, ans;
  while (cin >> n) {
    ans = 0;
    REP(n + 1) ans += !p[i];
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 10000000
int prime[MAX];

int main() {
  prime[1] = 1;
  prime[2] = 0;
  prime[3] = 0;

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

  int n;
  while (true) {

    cin >> n;
    if (cin.eof())
      break;

    int num = 0;
    for (int k = 1; k <= n; k++)
      if (prime[k] == 0)
        num++;

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

#include <bits/stdc++.h>
using namespace std;
int prime[1000005], e;
bool is_prime[1000005];
int presum[1000005];
void solve() {
  prime[0] = prime[1] = 1;
  for (int i = 2; i <= 1000000; i++) {
    if (!is_prime[i]) {
      prime[++e] = i;
      if ((long long)i * i > 1000000)
        continue;
      for (int j = i * i; j <= 1000000; j += i)
        is_prime[j] = 1;
    }
  }
}
void pre() {
  presum[0] = presum[1] = 0;
  for (int i = 2; i <= 1000000; i++)
    presum[i] = is_prime[i] ? presum[i - 1] : presum[i - 1] + 1;
}
int main() {
  int n;
  solve();
  pre();
  // cout<<e;
  while (cin >> n) {
    cout << presum[n] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, m;
int prime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return 0;
  return 1;
}
int main() {
  while (cin >> n) {
    int s = 0;
    for (int i = 2; i <= n; i++)
      if (prime(i))
        s++;
    cout << s << endl;
  }
  return 0;
}