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

#include <iostream>
#include <vector>

using namespace std;

vector<bool> isPrime(10001, true);
vector<int> primeNumbers;

int main() {
  isPrime[0] = isPrime[1] = false;
  for (int i = 2; i * i <= 10000; i += 2) {
    if (!isPrime[i])
      continue;
    for (int j = i * i; j <= 10000; j += i) {
      isPrime[j] = false;
    }
    i -= i == 2;
  }
  int n, ans = 0;
  for (int i = 0; i <= 10000; i++) {
    if (isPrime[i]) {
      primeNumbers.push_back(i);
    }
  }
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    if (a <= 1e4) {
      ans += isPrime[a];
      continue;
    }
    ans++;
    for (int i = 0;
         i < primeNumbers.size() && primeNumbers[i] * primeNumbers[i] <= a;
         i++) {
      if (a % primeNumbers[i] == 0) {
        ans--;
        break;
      }
    }
  }
  cout << ans << "\\n";
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

static bool is_prime[100000001];

int main() {
  memset(is_prime, true, sizeof(is_prime));
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i * i < 100000000; i += 2) {
    if (is_prime[i]) {
      for (int j = i * i; j <= 100000000; j += i) {
        is_prime[j] = false;
        if (i != 2) {
          j += i;
        }
      }
      if (i == 2) {
        --i;
      }
    }
  }
  int ans = 0, n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int num;
    scanf("%d", &num);
    ans += is_prime[num];
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

bool check_prime(int n) {
  if (n % 2 == 0) {
    if (n != 2)
      return false;
  }

  int r = sqrt(n);
  for (int i = 2; i <= r; i++) {
    if (n % i == 0) {
      return false;
    }
  }

  return true;
  ;
}

int main() {
  int n, counter = 0;
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    if (check_prime(a))
      counter++;
  }
  cout << counter << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

bool check_prime(int n) {
  bool res = true;

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

  double r = n / 2;
  for (int i = 2; i < r; i++) {
    if (n % i == 0) {
      res = false;
      break;
    }
  }
  return res;
}

int main() {
  int n, counter = 0;
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    if (check_prime(a))
      counter++;
  }
  cout << counter << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

bool isprime(int x);

int main() {
  int i, j;
  int a, n;
  int ans = 0;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> a;
    if (isprime(a) == true)
      ans++;
  }

  cout << ans << "\\n";

  return (0);
}

bool isprime(int x) {
  int i = 3;

  if (x == 2)
    return (true);
  if (x < 2 || x % 2 == 0)
    return (false);

  while (x >= i * i) {
    if (x % i == 0)
      return (false);
    i += 2;
  }

  return (true);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

bool isprime(int x);

int main() {
  int i, j;
  int a, n;
  int ans = 0;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> a;
    if (isprime(a) == true)
      ans++;
  }

  cout << ans << "\\n";

  return (0);
}

bool isprime(int x) {
  int i = 3;

  if (x == 2)
    return (true);
  if (x < 2 || x % 2 == 0)
    return (false);

  while (x >= i * i) {
    if (x % i == 0)
      return (false);
  }

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

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

bool isPrime(int x) {
  if (x == 2) {
    return true;
  }
  if (x < 2 || x % 2 == 0) {
    return false;
  }

  int i = 3;
  while (i <= sqrt(x)) {
    if (x % i == 0) {
      return false;
    }
    i = i + 2;
  }

  return true;
}

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

  int N[n];
  int c = 0;
  for (int i = 0; i < n; i++) {
    cin >> N[i];
    if (isPrime(N[i])) {
      c++;
    }
  }
  cout << c << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  return true;
}

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

  int N[n];
  int c = 0;
  for (int i = 0; i < n; i++) {
    cin >> N[i];
    if (isPrime(N[i])) {
      c++;
    }
  }
  cout << c << endl;

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

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

int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  if (a >= b) {
    gcd(b, a % b);
  } else {
    gcd(a, b % a);
  }
}

int main() {
  int N, d = 1;
  int sum = 0;
  cin >> N;
  int *A = (int *)malloc(sizeof(int) * (N + 1));
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    for (int k = 2; k <= floor(sqrt(A[i])); k++) {
      d = gcd(k, A[i]);
      if (d != 1) {
        break;
      }
    }
    if (d == 1) {
      sum++;
    }
  }
  cout << sum << "\\n";
  free(A);
  return 0;
}

Error: TLE

Fixed submission:

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

int gcd(int a, int b) {
  if (b == 0) {
    return a;
  }
  if (a >= b) {
    gcd(b, a % b);
  } else {
    gcd(a, b % a);
  }
}

int main() {
  int N, d = 1;
  int sum = 0;
  cin >> N;
  int *A = (int *)malloc(sizeof(int) * (N + 1));
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    for (int k = 2; k < A[i]; k++) {
      d = gcd(k, A[i]);
      if (d != 1) {
        break;
      }
    }
    if (d == 1) {
      sum++;
    }
  }
  cout << sum << "\\n";
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int prime(int num) {
  if (num == 2 || num == 3)
    return 1;
  if ((num % 2) * (num % 3) == 0)
    return 0;

  if (num <= 4) {
    return 0;
  }
  for (int k = 3; k <= int(sqrt(num)) + 2; k += 2) {
    if (num % k == 0)
      return 0;
  }
  return 1;
}

int main() {
  int N;
  cin >> N;
  int count = 0;
  for (int i = 0; i < N; i++) {
    int num;
    cin >> num;
    count += prime(num);
  }

  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int prime(int num) {
  if (num == 2 || num == 3 || num == 5) {
    return 1;
  }
  for (int k = 2; k <= (num + 1) / 2; k++) {
    if (num % k == 0) {
      return 0;
    }
  }
  return 1;
}

int main() {
  int N;
  cin >> N;
  int count = 0;
  for (int i = 0; i < N; i++) {
    int num;
    cin >> num;
    count += prime(num);
  }

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

#include <iostream>
using namespace std;

int main() {
  int n, a[11000], cnt = 0;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  for (int i = 0; i < n; ++i) {
    bool sosu = true;
    for (int j = 2; j * j <= a[i]; ++j) {
      if (a[i] % j == 0) {
        sosu = false;
        break;
      }
    }
    if (sosu)
      ++cnt;
  }

  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, a[11000], cnt = 0;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  for (int i = 0; i < n; ++i) {
    bool sosu = true;
    for (int j = 2; j < a[i]; ++j) {
      if (a[i] % j == 0) {
        sosu = false;
        break;
      }
    }
    if (sosu)
      ++cnt;
  }

  cout << cnt << endl;

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

#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {

  int N, inp, cnt = 0;
  bool f;

  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> inp;
    f = true;
    for (int i = 2; i <= sqrt((double)inp); i++) {
      if (inp % i == 0) {
        f = false;
        break;
      }
    }
    if (f)
      cnt++;
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {

  int N, inp, cnt = 0;
  bool f;

  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> inp;
    if (inp == 0 || inp == 1)
      f = false;
    else if (inp == 2)
      f = true;
    else if (inp % 2 != 0) {
      for (int j = 3; j <= inp; j += 2) {
        if (inp == j) {
          f = true;
          break;
        }
        if (inp % j == 0) {
          f = false;
          break;
        }
      }
    } else
      f = false;
    if (f)
      cnt++;
  }

  cout << cnt << endl;

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

// last edited at 26.8.2015 by charis

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

/* Time Limit Exceeded
bool isPrime(int x){
        if(x<=1) return false;
        for(int i=2; i<x; i++){
                if(x%i==0) return false;
        }
        return true;
}
*/

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

int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int c = 0;
  for (int i = 0; i < N; i++) {
    if (isPrime(A[i]))
      c++;
  }
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

// last edited at 26.8.2015 by charis

#include <iostream>
using namespace std;
/* Time Limit Exceeded
bool isPrime(int x){
        if(x<=1) return false;
        for(int i=2; i<x; i++){
                if(x%i==0) return false;
        }
        return true;
}
*/

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

int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int c = 0;
  for (int i = 0; i < N; i++) {
    if (isPrime(A[i]))
      c++;
  }
  cout << c << endl;
  return 0;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>

using namespace std;

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

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

  int x;
  int prime = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (is_prime(x))
      prime++;
  }
  cout << prime << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int x;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (x == 2) {
      sum++;
      continue;
    }
    if (x % 2 == 0) {
      continue;
    }
    int d = 3;
    while (1) {
      if (x % d == 0) {
        if (x == d) {
          sum++;
          break;
        } else {
          break;
        }
      }
      d = d + 2;
    }
  }
  cout << sum << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int N;

bool isPrime(int a) {
  if (a == 2)
    return true;
  if (a % 2 == 0)
    return false;

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

  return true;
}

int main() {
  cin >> N;

  int count = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (isPrime(a))
      count++;
  }

  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int N;

bool isPrime(int a) {
  if (a == 2)
    return true;
  if (a % 2 == 0)
    return false;

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

  return true;
}

int main() {
  cin >> N;

  int count = 0;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (isPrime(a))
      count++;
  }

  cout << count << endl;

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

#include <cassert>
#include <iostream>

#define MAX_N (100000000)

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

int main(void) {
  int N;
  std::cin >> N;
  int *a;
  a = new int[N];
  for (int i = 0; i < N; i++) {
    std::cin >> a[i];
  }

  int num_of_prime = 0;
  for (int i = 0; i < N; i++) {
    if (is_prime(a[i]))
      ++num_of_prime;
  }

  std::cout << num_of_prime << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cassert>
#include <iostream>
#include <string.h>

#define MAX_N (100000000)

bool ptable[MAX_N];

void init_ptable() {
  memset(ptable, false, MAX_N * sizeof(bool));

  ptable[0] = true;
  int base = 2;

  while (base <= MAX_N / 2) {
    // std::cout << "base: " << base << std::endl;
    for (int cur = base * 2; cur <= MAX_N; cur += base) {
      assert(1 <= cur && cur <= MAX_N);
      ptable[cur - 1] = true;
    }
    ++base;
    while (ptable[base - 1] == true)
      ++base;
  }
  return;
}

void dump_ptable() {
  for (int i = 0; i < MAX_N; i++) {
    std::cout << ptable[i];
  }
  return;
}

int main(void) {
  int N;
  std::cin >> N;
  int *a;
  a = new int[N];
  for (int i = 0; i < N; i++) {
    std::cin >> a[i];
  }
  // std::cout << "input success" << std::endl;

  init_ptable();
  // dump_ptable();

  // std::cout << "ptable scan success" << std::endl;
  int num_of_prime = 0;
  for (int i = 0; i < N; i++) {
    if (ptable[a[i] - 1] == false)
      ++num_of_prime;
  }

  // std::cout << "ptable check success" << std::endl;
  std::cout << num_of_prime << std::endl;

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

#include <iostream>

using namespace std;

bool primenum(int x);

int main() {
  int n;
  int i;
  int num[10000];
  int ans = 0;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> num[i];
  }

  for (i = 0; i < n; i++) {
    if (primenum(num[i]) == true)
      ans++;
  }

  cout << ans << "\\n";
}

bool primenum(int x) {
  int j = 3;

  if (x == 2)
    return (true);
  if (x % 2 == 0 || x < 2)
    return (false);

  while (j * j <= x) {
    if (x % j == 0) {
      return (false);
    }
    j += 2;
  }

  return (true);
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

bool primenum(int x);

int main() {
  int n;
  int i;
  int num[10000];
  int ans = 0;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> num[i];
  }

  for (i = 0; i < n; i++) {
    if (primenum(num[i]) == true)
      ans++;
  }

  cout << ans << "\\n";
}

bool primenum(int x) {
  int j = 3;

  if (x == 2)
    return (true);
  if (x % 2 == 0 || x < 2)
    return (false);

  while (j * j <= x) {
    if (x % j == 0) {
      return (false);
    }
  }

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

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

int prime(int x);

int main() {
  int n; // how many numbers
  int x;
  int cou = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x; // stop endl
    cou += prime(x);
  }
  cout << cou << endl;

  return 0;
}

int prime(int x) {
  int c = 0;

  if (x <= 1)
    return c;
  else if (x == 2)
    return ++c; // c++ だとreturnして++
  else {
    for (int i = 2; i <= sqrt(x); i++) {
      if (x % i == 0)
        return c;
    }
    return ++c;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int c = 0; // count
void prime(int x);

int main() {
  int n; // how many numbers
  int x;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x; // stop endl
    prime(x);
  }
  cout << c << endl;

  return 0;
}

void prime(int x) {
  if (x == 2)
    c++;
  if (x % 2 != 0 && x > 2) {
    for (int i = 3; i <= x; i++) {
      if (x % i == 0 && i < x)
        break;
      if (i == x)
        c++;
    }
  }
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <complex>
#include <iostream>

using namespace std;

int main() {
  int n, count = 0, i, j;
  cin >> n;
  int list[n];
  for (i = 0; i < n; i++)
    cin >> list[i];

  for (i = 0; i < n; i++) {
    if (list[i] <= 1)
      continue;
    if (list[i] == 2 || list[i] == 3) {
      count++;
      continue;
    }
    if (list[i] > 3 && list[i] % 2 == 0)
      continue;
    for (j = 2; j <= sqrt(list[i]); j++)
      if (list[i] % j == 0)
        break;
    if (j > sqrt(list[i]))
      count++;
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, count = 0;
  cin >> n;
  int list[n];
  for (int i = 0; i < n; i++)
    cin >> list[i];

  for (int i = 0; i < n; i++) {
    for (int j = 2; j <= list[i]; j++) {
      if (j == list[i]) {
        count++;
        break;
      }
      if (j >= 3 && list[i] % 2 == 0)
        continue;
      if (list[i] % j == 0)
        break;
    }
  }
  cout << count << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

using namespace std;

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

  auto isPrime = [](int n) {
    if (n < 2) {
      return false;
    } else if (n == 2) {
      return true;
    } else if (n % 2 == 0) {
      return false;
    }
    for (int i = 3; (i * i) <= n; i += 2) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  };

  int count{0};
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (isPrime(x)) {
      count += 1;
    }
  }
  cout << count << endl;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

  vector<int> v(100000000 + 1, -1);
  for (int i = 2; i < v.size(); ++i) {
    if (v[i] == -1) {
      v[i] = 1;
      for (int j = i * 2; j < v.size(); j += i) {
        v[j] = 0;
      }
    }
  }

  int count{0};
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (v[x] == 1) {
      count += 1;
    }
  }
  cout << count << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <cmath>
#include <iostream>

using namespace std;

bool isPrime(int x) {
  if (x == 2) {
    return true;
  }
  if (x == 1 || x % 2 == 0) {
    return false;
  }

  for (int i = 3; i < sqrt(x) + 1; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

int main() {
  int N;
  cin >> N;
  int prime = 0;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (isPrime(x)) {
      prime++;
    }
  }
  cout << prime << endl;
}


Error: TLE

Fixed submission:


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  int prime = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    for (int j = 2; j <= A[i]; j++) {
      if (A[i] == 2) {
        prime++;
        break;
      }
      if (A[i] % j == 0) {
        break;
      }
      if (j == A[i] - 1) {
        prime++;
      }
    }
  }
  cout << prime << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool isPrime(int n) {

  if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;

  int i = 3;
  while (i <= sqrt(n)) {
    if (n % i == 0) {
      return false;
    } else {
      i = i + 2;
    }
  }
  return true;
}

int main() {
  int N, element, count = 0;

  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> element;
    if (isPrime(element) == true) {
      count++;
      // cout << element << endl;
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool check_prime(int n) {
  int i = 2;
  bool flg = true;
  while (i < n) {
    if (n % i == 0) {
      flg = false;
      break;
    } else {
      i++;
    }
  }
  return flg;
}

int main() {
  int N;
  const int MAX_N = 10000;

  cin >> N;

  int arr[MAX_N];

  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }

  int count = 0;
  for (int i = 0; i < N; i++) {
    if (check_prime(arr[i]) == true) {
      count++;
    }
  }
  cout << count << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int isprime(int);

int main() {

  int i, n, x, count = 0;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> x;
    count += isprime(x);
  }

  cout << count << endl;

  return 0;
}

int isprime(int x) {

  if (x == 2)
    return 1;
  else if (x < 2 || x % 2 == 0)
    return 0;

  int i = 3;
  for (i; i <= sqrt(x); i += 2)
    if (x % i == 0)
      return 0;

  return 1;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {

  int N, num, i, j, judge, count = 0;

  cin >> N;

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

    judge = 1;
    cin >> num;

    for (j = 2; j <= num / 2; j++)
      if (num % j == 0)
        judge++;
    if (judge == 1)
      count++;
  }

  cout << count << endl;
}
Problem p02257 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n;
  scanf("%d", &n);
  int cnt = 0;
  rep(i, n) {
    int a;
    scanf("%d", &a);
    if (a == 1)
      continue;
    bool ok = true;
    for (int j = 2; j * j <= a; j++) {
      if (a % j == 0) {
        ok = false;
        break;
      }
    }
    if (ok)
      cnt++;
  }
  printf("%d\\n", cnt);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

bool is_prime[100000001];
int main() {
  memset(is_prime, 1, sizeof(is_prime));
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i <= 100000000; i++) {
    if (is_prime[i]) {
      for (int j = i * 2; j <= 100000000; j += i)
        is_prime[j] = false;
    }
  }
  int n;
  scanf("%d", &n);
  int cnt = 0;
  rep(i, n) {
    int a;
    scanf("%d", &a);
    if (is_prime[a])
      cnt++;
  }
  printf("%d\\n", cnt);
}
Problem p02257 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int couent = 0;
int keisann(int a) {
  if (a == 2) {
    couent++;
  }
  if (a % 2 == 0) {
    return 0;
  }
  int v = sqrt(a);
  for (int i = 3; i <= v; i += 2) {
    if (a % i == 0) {
      return 0;
    }
  }
  couent++;
}
int main() {
  int dataset;
  cin >> dataset;
  for (size_t i = 0; i < dataset; i++) {
    int a;
    cin >> a;
    keisann(a);
  }
  cout << couent << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int couent = 0;
int keisann(int a) {
  for (int i = 2; i < a; i++) {
    if (a % i == 0) {
      return 0;
    }
  }
  couent++;
}
int main() {
  int dataset;
  cin >> dataset;
  for (size_t i = 0; i < dataset; i++) {
    int a;
    cin >> a;
    keisann(a);
  }
  cout << couent << endl;
}