prithivMLmods/Coder-Stat huggingface dataset:
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int gcd(int x, int y) { return (x % y > 0 || x > y) ? gcd(y, x % y) : y; }
int main() {
  int x, y;
  int w;
  scanf("%d %d", &x, &y);
  w = gcd(x, y);
  printf("%d\n", w);
}

Fixed submission:
#include <stdio.h>
int gcd(int x, int y) { return (x % y > 0) ? gcd(y, x % y) : y; }
int main() {
  int x, y;
  int w;
  scanf("%d %d", &x, &y);
  w = gcd(x, y);
  printf("%d\n", w);
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int gcd(int x, int y) {
  int ans;
  if (x == 1 || y == 1) {
    return x * y;
  } else if (x > y) {
    ans = gcd(y, x % y);
  } else {
    ans = gcd(x, y % x);
  }
  return ans;
}

int main(void) {
  int x, y;
  scanf("%d %d", &x, &y);
  printf("%d\n", gcd(x, y));
  return 0;
}

Fixed submission:
#include <stdio.h>

int gcd(int x, int y) {
  int ans;
  if (x == 0) {
    return y;
  } else if (y == 0) {
    return x;
  } else if (x > y) {
    ans = gcd(y, x % y);
  } else {
    ans = gcd(x, y % x);
  }
  return ans;
}

int main(void) {
  int x, y;
  scanf("%d %d", &x, &y);
  printf("%d\n", gcd(x, y));
  return 0;
}
Problem p02256 (C++)

Buggy submission (status: Runtime Error):

#include <cmath>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main(void) {
  int x, y, d, a, b;
  cin >> x >> y;
  if (y > x) {
    b = y;
    y = x;
    x = b;
  }
  a = x % y;
  for (d = a;; d--) {
    if (y % d == 0 && a % d == 0) {
      cout << d << endl;
      break;
    }
  }
  return 0;
}


Fixed submission:

#include <cmath>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main(void) {
  int x, y, d, a, b;
  cin >> x >> y;
  if (y > x) {
    b = y;
    y = x;
    x = b;
  }
  if (x == y) {
    cout << x << endl;
    return 0;
  }
  a = x % y;
  for (d = a;; d--) {
    if (y % d == 0 && a % d == 0) {
      cout << d << endl;
      break;
    }
  }
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main() {
  int x, y, n, b = 0, o;
  cin >> n;
  for (o = 0; o < n; o++) {
    cin >> x;
    for (y = 2; y < x; y++) {
      if (x % y == 0) {
        goto loop;
      }
    }
    b++;
  loop:;
  }
  cout << b << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main() {
  int x, y, n, b = 0, o;
  cin >> n;
  for (o = 0; o < n; o++) {
    cin >> x;
    for (y = 2; y * y <= x; y++) {
      if (x % y == 0) {
        goto loop;
      }
    }
    b++;
  loop:;
  }
  cout << b << endl;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int A[n], count = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    int L = A[i];
    int M = sqrt(L);
    for (int x = 2; x < L; x++) {
      if (L % x == 0) {
        count--;
        break;
      }
    }
    count++;
  }
  cout << count << endl;
  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int A[n], count = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    int L = A[i];
    int M = sqrt(L);
    for (int x = 2; x <= M; x++) {
      if (L % x == 0) {
        count--;
        break;
      }
    }
    count++;
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

bool is_prime[50000000];
int main() {

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

  is_prime[0] = false;
  for (int i = 1; i < 50000000; i++) {
    if (is_prime[i]) {
      for (int j = i * 3 + 1; j < 50000000; j += 2 * i + 1) {
        is_prime[j] = false;
      }
    }
  }

  int n, tag;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> tag;
    if (tag % 2 == 0) {
      if (tag == 2) {
        cnt++;
      }
    } else {
      if (is_prime[tag / 2]) {
        cnt++;
      }
    }
  }

  cout << cnt << endl;
  int xxx;
  cin >> xxx;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

bool is_prime[50000000];
int main() {

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

  is_prime[0] = false;
  for (int i = 1; i * i + i < 25000000; i++) {
    if (is_prime[i]) {
      for (int j = i * 3 + 1; j < 50000000; j += 2 * i + 1) {
        is_prime[j] = false;
      }
    }
  }

  int n, tag;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> tag;
    if (tag % 2 == 0) {
      if (tag == 2) {
        cnt++;
      }
    } else {
      if (is_prime[tag / 2]) {
        cnt++;
      }
    }
  }

  cout << cnt << endl;
  int xxx;
  cin >> xxx;
  return 0;
}
Problem p02257 (Python)

Buggy submission (status: Time Limit Exceeded):
prime = set([2])
for i in range(3, 100000000, 2):
    for j in prime:
        if i % j == 0:
            break
    else:
        prime.add(i)
n = int(input())
cnt = 0
for i in (int(input()) for _ in range(n)):
    if i in prime:
        cnt += 1
print(cnt)


Fixed submission:
import sys
from math import *

readline = sys.stdin.readline


def isPrime(x):
    if x == 2 or x == 3:
        return True
    elif x % 2 == 0 or x % 3 == 0:
        return False
    s = ceil(sqrt(x))
    for i in range(5, s + 1, 2):
        if x % i == 0:
            return False
    return True


print(sum(isPrime(int(readline())) for _ in range(int(input()))))
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

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 cnt = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (isPrime(x)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

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

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (isPrime(x)) {
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

#define CALC_PRIME_MAX 10000   // _/10 ^ 8
#define PRIME_ELEMENT_MAX 1229 // prime( _/10 ^ 8 )

int prime_number[PRIME_ELEMENT_MAX];

bool isPrime(int element) {
  for (int i = 0; prime_number[i] * prime_number[i] <= element; i++) {
    if (element % prime_number[i] == 0)
      return false;
  }
  return true;
}

void calcPrime() {
  // add 2, 3 (prime)
  int prime_index = 2;
  prime_number[0] = 2;
  prime_number[1] = 3;
  // next prime is 5
  for (int element = 5; element <= CALC_PRIME_MAX; element += 2) {
    bool is_prime = true;
    // judge prime
    for (int i = 1; prime_number[i] * prime_number[i] <= element; i++) {
      if (element % prime_number[i] == 0) {
        is_prime = false;
        break;
      }
    }
    // add prime
    if (is_prime)
      prime_number[prime_index++] = element;
  }
  //	cout << "prime_element_num = " << prime_index << endl;
  return;
}

int main() {
  int n, element, count = 0;
  calcPrime();
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> element;
    if (isPrime(element))
      count++;
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

#define CALC_PRIME_MAX 10007   // _/10 ^ 8 + next prime
#define PRIME_ELEMENT_MAX 1230 // prime( _/10 ^ 8 ) + 1(10007)

int prime_number[PRIME_ELEMENT_MAX];

bool isPrime(int element) {
  for (int i = 0; prime_number[i] * prime_number[i] <= element; i++) {
    if (element % prime_number[i] == 0)
      return false;
  }
  return true;
}

void calcPrime() {
  // add 2, 3 (prime)
  int prime_index = 2;
  prime_number[0] = 2;
  prime_number[1] = 3;
  // next prime is 5
  for (int element = 5; element <= CALC_PRIME_MAX; element += 2) {
    bool is_prime = true;
    // judge prime
    for (int i = 1; prime_number[i] * prime_number[i] <= element; i++) {
      if (element % prime_number[i] == 0) {
        is_prime = false;
        break;
      }
    }
    // add prime
    if (is_prime)
      prime_number[prime_index++] = element;
  }
  //	cout << "prime_element_num = " << prime_index << endl;
  return;
}

int main() {
  int n, element, count = 0;
  calcPrime();
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> element;
    if (isPrime(element))
      count++;
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

bool isPrime(int val);
int countPrime(vector<int> &array);

int main() {
  int size;
  vector<int> array;
  cin >> size;
  for (int i = 0; i < size; i++) {
    int tmp;
    cin >> tmp;
    array.push_back(tmp);
  }
  cout << countPrime(array) << endl;
  return 0;
}

int countPrime(vector<int> &array) {
  int num = 0;
  for (int i = 0; i < array.size(); i++) {
    if (isPrime(array[i])) {
      num++;
    }
  }
  return num;
}

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

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

bool isPrime(int val);
int countPrime(vector<int> &array);

int main() {
  int size;
  vector<int> array;
  cin >> size;
  for (int i = 0; i < size; i++) {
    int tmp;
    cin >> tmp;
    array.push_back(tmp);
  }
  cout << countPrime(array) << endl;
  return 0;
}

int countPrime(vector<int> &array) {
  int num = 0;
  for (int i = 0; i < array.size(); i++) {
    if (isPrime(array[i])) {
      num++;
    }
  }
  return num;
}

bool isPrime(int val) {
  if (val <= 1) {
    return false;
  }
  for (int i = 2; i * i <= val; i++) {
    if (val % i == 0) {
      return false;
    }
  }
  return true;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
int main(void) {
  std::ios::sync_with_stdio(false);
  int n, COUNT = 0;
  std::cin >> n;
  int x;
  for (int i = 0; i < n; ++i) {
    std::cin >> x;
    if (x == 2) {
      ++COUNT;
    } else if (x % 2 != 0) {
      bool FLUG = false;
      int Max = static_cast<int>(x / 2) + 1;
      for (int i = 3; i < Max; i += 2) {
        if (x % i == 0) {
          FLUG = true;
          break;
        }
      }
      if (FLUG == false) {
        ++COUNT;
      }
    }
  }
  std::cout << COUNT << std::endl;
  // system("pause");
  return 0;
}

Fixed submission:
#include <iostream>
int main(void) {
  std::ios::sync_with_stdio(false);
  int n, COUNT = 0;
  std::cin >> n;
  int x;
  for (int i = 0; i < n; ++i) {
    std::cin >> x;
    if (x == 2) {
      ++COUNT;
    } else if (x % 2 != 0) {
      bool FLUG = false;
      for (int i = 3; i * i <= x; i += 2) {
        if (x % i == 0) {
          FLUG = true;
          break;
        }
      }
      if (FLUG == false) {
        ++COUNT;
      }
    }
  }
  std::cout << COUNT << std::endl;
  // system("pause");
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>

using namespace std;

void PrimeNumSum(int &sum, unsigned int &num) {
  unsigned int divisor = num / 2 + 1;

  if (divisor == 2) {
    sum++;
    return;
  }
  while (divisor >= 2) {
    if (num % divisor == 0) {
      return;
    }
    divisor--;
  }

  sum++;
}

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

  unsigned int *list = new unsigned int[count];

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

  int sum = 0;

  for (int i = 0; i < count; i++) {
    PrimeNumSum(sum, list[i]);
  }

  cout << sum << endl;

  delete[] list;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>

using namespace std;

void PrimeNumSum(int &sum, unsigned int &num) {
  unsigned int divisor = sqrt(num) + 1;

  if (divisor == 2) {
    sum++;
    return;
  }
  while (divisor >= 2) {
    if (num % divisor == 0) {
      return;
    }
    divisor--;
  }

  sum++;
}

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

  unsigned int *list = new unsigned int[count];

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

  int sum = 0;

  for (int i = 0; i < count; i++) {
    PrimeNumSum(sum, list[i]);
  }

  cout << sum << endl;

  delete[] list;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>

bool is_prime(int a);

int main() {
  int n;
  int cnt = 0;
  std::cin >> n;
  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    bool flag = is_prime(a[i]);
    if (flag)
      cnt++;
  }
  std::cout << cnt << '\n';
  return 0;
}

bool is_prime(int a) {
  for (int i = 0; i < std::sqrt(a); i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

bool is_prime(int a);

int main() {
  int n;
  int cnt = 0;
  std::cin >> n;
  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    bool flag = is_prime(a[i]);
    if (flag)
      cnt++;
  }
  std::cout << cnt << '\n';
  return 0;
}

bool is_prime(int a) {
  if (a == 2) {
    return true;
  } else if (a == 0 || a == 1) {
    return false;
  }
  for (int i = 2; i <= std::sqrt(a); i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
Problem p02257 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

void Makepn(vector<int> &v, int n) {
  int count = 1;
  int i, j;
  vector<bool> pn(n + 1, true);
  pn[0] = false;
  pn[1] = false;
  for (i = 2; i < sqrt(n); i++) {
    if (pn[i]) {
      for (j = 0; i * (j + 2) < n; j++) {
        pn[i * (j + 2)] = false;
      }
    }
  }
  for (i = 2; i < n; i++) {
    if (pn[i]) {
      count++;
    }
  }
  v.resize(count);

  v[0] = 2;
  j = 1;
  for (i = 3; i <= n; i += 2) {
    if (pn[i]) {
      v[j++] = i;
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  vector<int> pn;
  Makepn(pn, 10000);
  int n, num, ans;
  cin >> n;
  ans = n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    for (int j = 0; j < pn.size(); j++) {
      if (pn[j] * pn[j] > num)
        break;
      if (num % pn[j] == 0) {
        ans--;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

void Makepn(vector<int> &v, int n) {
  int count = 0;
  int i, j;
  vector<bool> pn(n + 1, true);
  pn[0] = false;
  pn[1] = false;
  for (i = 2; i < sqrt(n); i++) {
    if (pn[i]) {
      for (j = 0; i * (j + 2) < n; j++) {
        pn[i * (j + 2)] = false;
      }
    }
  }
  for (i = 2; i < n; i++) {
    if (pn[i]) {
      count++;
    }
  }
  v.resize(count);

  v[0] = 2;
  j = 1;
  for (i = 3; i <= n; i += 2) {
    if (pn[i]) {
      v[j++] = i;
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  vector<int> pn;
  Makepn(pn, 10000);
  int n, num, ans;
  cin >> n;
  ans = n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    for (int j = 0; j < pn.size(); j++) {
      if (pn[j] * pn[j] > num)
        break;
      if (num % pn[j] == 0) {
        ans--;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;
bool Prime(int x) {
  for (int i = 2; i < (x / 2 + 1); i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  scanf("%d", &n);
  int N[n];
  for (int i = 0; i < n; i++) {
    scanf(" %d", &N[i]);
  }
  int cnt;
  for (int i = 0; i < n; i++) {
    if (Prime(N[i]) == true) {
      cnt++;
    }
  }
  printf("%d\n", cnt);
  return 0;
}


Fixed submission:
#include <cstdio>
using namespace std;
bool Prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  scanf("%d", &n);
  int N[n];
  for (int i = 0; i < n; i++) {
    scanf(" %d", &N[i]);
  }
  int cnt;
  for (int i = 0; i < n; i++) {
    if (Prime(N[i]) == true) {
      cnt++;
    }
  }
  printf("%d\n", cnt);
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;

#define MAX 256

int f(int x) {
  if (x == 2)
    return 1;
  if (x % 2 == 0)
    return 0;
  for (int i = 3; i <= x / 2; i += 2) {
    if (x % i == 0)
      return 0;
  }
  return 1;
}

int main(void) {

  int a;
  scanf("%d", &a);
  int count = 0, b;

  for (int i = 0; i < a; i++) {
    scanf("%d", &b);
    count += f(b);
  }

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

  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;

#define MAX 256

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

int main(void) {

  int a;
  scanf("%d", &a);
  int count = 0, b;

  for (int i = 0; i < a; i++) {
    scanf("%d", &b);
    count += f(b);
  }

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

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, f, n) for (int i = (f); i <= (n); ++i)
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
template <class A, class B> inline bool chmax(A &a, B b) {
  return b > a ? a = b, 1 : 0;
}
template <class A, class B> inline bool chmin(A &a, B b) {
  return b < a ? a = b, 1 : 0;
}
inline int updiv(int a, int b) { return ((a + b - 1) / b); }

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

int main() {
  FAST();
  int n, a, cnt = 0;
  cin >> n;
  while (n--) {
    cin >> a;
    cnt += prime(a);
  }
  outl(cnt);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, f, n) for (int i = (f); i <= (n); ++i)
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
template <class A, class B> inline bool chmax(A &a, B b) {
  return b > a ? a = b, 1 : 0;
}
template <class A, class B> inline bool chmin(A &a, B b) {
  return b < a ? a = b, 1 : 0;
}
inline int updiv(int a, int b) { return ((a + b - 1) / b); }

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

int main() {
  FAST();
  int n, a, cnt = 0;
  cin >> n;
  while (n--) {
    cin >> a;
    cnt += prime(a);
  }
  outl(cnt);
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

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

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;

    for (int j = 2; j < a; j++) {
      if (a % j == 0) {
        cnt++;
        break;
      }
    }
  }

  cout << n - cnt << endl;

  return 0;
}


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

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

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;

    int t = sqrt(a);
    for (int j = 2; j <= t; j++) {
      if (a % j == 0) {
        cnt++;
        break;
      }
    }
  }

  cout << n - cnt << endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;

int prs[5000];
int pri = 1;

int isPrime(int a) {
  int flg;
  if (sqrt(a) > prs[pri]) {
    for (int i = prs[pri] + 2; i <= sqrt(a); i += 2) {
      flg = 1;
      for (int j = 0; j <= pri; j++) {
        if (sqrt(i) < prs[i])
          break;
        if (i % prs[j] == 0) {
          flg = 0;
          break;
        }
      }
      if (flg)
        prs[++pri] = i;
    }
    // for (int i=0;i<=pri;i++)cout << prs[i] << " ";
  }
  for (int i = 0; i <= pri; i++) {
    if (sqrt(a) < prs[i])
      break;
    if (a % prs[i] == 0)
      return 0;
  }
  return 1;
}

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

  int n, a, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cnt += isPrime(a);
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

int prs[10000];
int pri = 1;

int isPrime(int a) {
  int flg;
  if (sqrt(a) > prs[pri]) {
    for (int i = prs[pri] + 2; i <= sqrt(a); i += 2) {
      flg = 1;
      for (int j = 0; j <= pri; j++) {
        if (sqrt(i) < prs[i])
          break;
        if (i % prs[j] == 0) {
          flg = 0;
          break;
        }
      }
      if (flg)
        prs[++pri] = i;
    }
    // for (int i=0;i<=pri;i++)cout << prs[i] << " ";
  }
  for (int i = 0; i <= pri; i++) {
    if (sqrt(a) < prs[i])
      break;
    if (a % prs[i] == 0)
      return 0;
  }
  return 1;
}

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

  int n, a, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cnt += isPrime(a);
  }
  cout << cnt << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
typedef long long ll;
bool isPrime(ll a);

int main() {
  using namespace std;
  ll A[10001];
  int n, prime = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (isPrime(A[i])) {
      prime++;
    }
  }
  cout << prime << endl;
  return 0;
}

bool isPrime(ll a) {
  for (ll i = 2; i < a; i++) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}


Fixed submission:
#include <iostream>
typedef long long ll;
bool isPrime(ll a);

int main() {
  using namespace std;
  ll A[10001];
  int n, prime = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (isPrime(A[i])) {
      prime++;
    }
  }
  cout << prime << endl;
  return 0;
}

bool isPrime(ll a) {
  for (ll i = 2; i * i <= a; i++) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n, m, i, count;
  cin >> n;
  count = 0;

  for (int j = 0; j < n; ++j) {
    cin >> m;
    if (m == 2)
      ++count;
    else if (m < 2 || m % 2 == 0) {
      continue;
    } else {
      i = 3;
      while (i <= sqrt(m)) {
        if (m % i == 0)
          continue;
        i = i + 2;
      }
      if (i > sqrt(m)) {
        ++count;
      }
    }
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n, m, i, count;
  cin >> n;
  count = 0;

  for (int j = 0; j < n; ++j) {
    cin >> m;
    if (m == 2)
      ++count;
    else if (m < 2 || m % 2 == 0) {
      continue;
    } else {
      i = 3;
      while (i <= sqrt(m)) {
        if (m % i == 0)
          break;
        i = i + 2;
      }
      if (i > sqrt(m)) {
        ++count;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

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

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

int main() {
  int N;
  int count = 0;
  int num;

  cin >> N;

  REP(i, N) {
    cin >> num;
    if (isPrime(num))
      ++count;
  }

  cout << count << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

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

  // +1 重要
  for (int i = 3; i < sqrt(num) + 1; i += 2) {
    if (num % i == 0)
      return false;
  }
  return true;
}

int main() {
  int N;
  int count = 0;
  int num;

  cin >> N;

  REP(i, N) {
    cin >> num;
    if (isPrime(num))
      ++count;
  }

  cout << count << endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

using namespace std;

const int maxn = 50001;

int prime[maxn], cnt, res;
bool isprime[maxn];

void maketable() {
  int base = (int)sqrt((double)maxn);
  isprime[0] = isprime[1] = 1;
  for (int i = 2; i < base; i++) {
    if (!isprime[i]) {
      prime[cnt++] = i;
      for (int j = i + i; j < maxn; j += i) {
        isprime[j] = 1;
      }
    }
  }
}

void solve(int n) {
  int k = (int)sqrt((double)n);
  for (int i = 0; prime[i] <= k; i++) {
    if (n % prime[i] == 0) {
      return;
    }
  }
  res++;
}

int main() {
  maketable();
  int n, s;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s);
    solve(s);
  }
  printf("%d\n", res);
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 50001;

int prime[maxn], cnt, res;
bool isprime[maxn];

void maketable() {
  int base = (int)sqrt((double)maxn);
  isprime[0] = isprime[1] = 1;
  for (int i = 2; i < maxn; i++) { // 此处i<maxn与以前的不同
    if (!isprime[i]) {
      prime[cnt++] = i;
      for (int j = i + i; j < maxn; j += i) {
        isprime[j] = 1;
      }
    }
  }
}

void solve(int n) {
  int k = (int)sqrt((double)n);
  for (int i = 0; prime[i] <= k; i++) {
    if (n % prime[i] == 0) {
      return;
    }
  }
  res++;
}

int main() {
  maketable();
  int n, s;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &s);
    solve(s);
  }
  printf("%d\n", res);
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: 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);
    }
  }

  return (true);
}


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);
    }
    j += 2;
  }

  return (true);
}
Problem p02257 (C++)

Buggy submission (status: 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 < a[i]; ++j) {
      if (a[i] % j == 0) {
        sosu = false;
        break;
      }
    }
    if (sosu)
      ++cnt;
  }

  cout << cnt << endl;

  return 0;
}


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 * 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 (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int N;
  int x;
  int count = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x;
    for (int j = 2; j < x / 2 + 1; j++) {
      if (x != 2) {
        if (x % j == 0) {
          count++;
          break;
        }
      }
    }
  }
  cout << N - count << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N;
  int x;
  int count = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> x;
    for (int j = 2; j * j <= x; j++) {
      if (x % j == 0) {
        count++;
        break;
      }
    }
  }
  cout << N - count << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 1e6

int main(void) {
  vector<bool> eratos(MAX_N, true);
  eratos[0] = eratos[1] = false;
  for (int i = 2; i * i < MAX_N; i++) {
    if (eratos[i]) {
      for (int j = 2; i * j < MAX_N; j++) {
        eratos[i * j] = false;
      }
    }
  }

  int n, input, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input;
    if (eratos[input]) {
      ++ans;
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N (1e8) + 1

int main(void) {
  vector<bool> eratos(MAX_N, true);
  eratos[0] = eratos[1] = false;
  for (int i = 2; i * i < MAX_N; i++) {
    if (eratos[i]) {
      for (int j = 2; i * j < MAX_N; j++) {
        eratos[i * j] = false;
      }
    }
  }

  int n, input, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input;
    if (eratos[input]) {
      ++ans;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

const int MAX_NUM = 100000000;
vector<bool> ps(MAX_NUM + 1, true);

int main() {
  ps[0] = ps[1] = false;
  for (int i = 2; i <= MAX_NUM; i++) {
    if (!ps[i]) {
      continue;
    }
    for (int j = i + i; j <= MAX_NUM; j += i) {
      ps[j] = false;
    }
  }

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

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

const int MAX_NUM = 100000000;
vector<bool> ps(MAX_NUM + 1, true);

int main() {
  ps[0] = ps[1] = false;
  for (int i = 4; i <= MAX_NUM; i += 2) {
    ps[i] = false;
  }
  for (int i = 3; i <= MAX_NUM; i += 2) {
    if (!ps[i]) {
      continue;
    }
    for (int j = i + i; j <= MAX_NUM; j += i) {
      ps[j] = false;
    }
  }

  int n;
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    if (ps[k]) {
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p02257 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N (1e9) + 1

int main(void) {
  //?????????????????????
  vector<bool> eratos(MAX_N, true);
  eratos[0] = eratos[1] = false;
  for (int i = 2; i * i < MAX_N; i++)
    if (eratos[i])
      for (int j = 2; i * j < MAX_N; j++)
        eratos[i * j] = false;

  //?????¨?????????
  int n, input, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input;
    if (eratos[input])
      ++ans;
  }

  //??????
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N (1e8) + 1

int main(void) {
  //?????????????????????
  vector<bool> eratos(MAX_N, true);
  eratos[0] = eratos[1] = false;
  for (int i = 2; i * i < MAX_N; i++)
    if (eratos[i])
      for (int j = 2; i * j < MAX_N; j++)
        eratos[i * j] = false;

  //?????¨?????????
  int n, input, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input;
    if (eratos[input])
      ++ans;
  }

  //??????
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

bool is_prime(int num) {
  for (int i = 2; i < num; i++) {

    if (num % i == 0) {
      return false;
    }
  }
  return true;
}
int main(void) {
  int n;
  cin >> n;

  int num;
  int prime_count = 0;
  for (int i = 0; i < n; i++) {
    cin >> num;

    if (is_prime(num) == true) {
      prime_count++;
    }
  }
  cout << prime_count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

bool is_prime(int num) {
  int a = sqrt(num);

  for (int i = 2; i <= a; i++) {

    if (num % i == 0) {
      return false;
    }
  }
  return true;
}
int main(void) {
  int n;
  cin >> n;

  int num;
  int prime_count = 0;
  for (int i = 0; i < n; i++) {
    cin >> num;

    if (is_prime(num) == true) {
      prime_count++;
    }
  }
  cout << prime_count << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: 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);
  }

  return (true);
}


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);
    i += 2;
  }

  return (true);
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
using namespace std;

int GCD(int x, int y) {
  if (y == 0) {
    return x;
  }
  return GCD(y, x % y);
}

bool isPrime(int N) {
  for (int i = 2; i < N; i++) {
    if (GCD(N, i) != 1)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int primeNumberCount = 0;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if (isPrime(num) == true)
      primeNumberCount++;
  }
  cout << primeNumberCount << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
using namespace std;

int GCD(int x, int y) {
  if (y == 0) {
    return x;
  }
  return GCD(y, x % y);
}

bool isPrime(int N) {
  if (N == 2)
    true;
  if (N % 2 == 0)
    false;
  for (int i = 2; i * i <= N; i++) {
    if (GCD(N, i) != 1)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  int primeNumberCount = 0;
  for (int i = 0; i < n; i++) {
    int num;
    cin >> num;
    if (isPrime(num) == true)
      primeNumberCount++;
  }
  cout << primeNumberCount << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

bool prime(int);

int main() {
  int n, k, c = 0;
  cin >> n;
  while (n--) {
    cin >> k;
    if (prime(k))
      c++;
  }
  cout << c << endl;
  return 0;
}

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

Fixed submission:
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

bool prime(int);

int main() {
  int n, k, c = 0;
  cin >> n;
  while (n--) {
    cin >> k;
    if (prime(k))
      c++;
  }
  cout << c << endl;
  return 0;
}

bool prime(int a) {
  for (int i = 2; i < (int)(sqrt((double)a)) + 1; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int N, a, ans = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    bool flag = true;
    if (a != 2 && a % 2 == 0 || a == 1) {
      flag = false;
    } else if (a != 2) {
      for (int j = 3; j < a; j += 2) {
        if (a % j == 0) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      ans += 1;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N, a, ans = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    bool flag = true;
    if (a != 2 && a % 2 == 0 || a == 1) {
      flag = false;
    } else if (a > 2) {
      for (int j = 3; j * j <= a; j += 2) {
        if (a % j == 0) {
          flag = false;
          break;
        }
      }
    }
    if (flag) {
      ans += 1;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <typeinfo>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>

using namespace std;

// prime=>true, not prime=>false
// bool isPrime(int i){
//     for(int j=2; j<=i/2; j++){
//         if(i%j==0){
//             return false;
//         }
//     }
//     return true;
// }

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

int main() {

  int n;
  cin >> n;

  vector<long> n_nums(n);
  for (int i = 0; i < n; i++) {
    cin >> n_nums[i];
  }

  int prime_count = 0;
  for (int i = 0; i < n; i++) {
    if (isPrime(n_nums[i])) {
      prime_count += 1;
    }
  }

  cout << prime_count << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <typeinfo>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>

using namespace std;

// prime=>true, not prime=>false
// bool isPrime(int i){
//     for(int j=2; j<=i/2; j++){
//         if(i%j==0){
//             return false;
//         }
//     }
//     return true;
// }

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

int main() {

  int n;
  cin >> n;

  vector<long> n_nums(n);
  for (int i = 0; i < n; i++) {
    cin >> n_nums[i];
  }

  int prime_count = 0;
  for (int i = 0; i < n; i++) {
    if (isPrime(n_nums[i])) {
      prime_count += 1;
    }
  }

  cout << prime_count << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main() {
  int n, m;
  ;
  cin >> m;
  int count = m;
  for (int s = 0; s < m; s++) {
    cin >> n;
    if (n == 2)
      continue;
    else if (n % 2 == 0)
      count--;
    else {
      for (int i = 3; i < n; i += 2) {
        if (n % i == 0) {
          count--;
          break;
        }
      }
    }
  }
  cout << count << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main() {
  int n, m;
  ;
  cin >> m;
  int count = m;
  for (int s = 0; s < m; s++) {
    cin >> n;
    if (n == 2)
      continue;
    else if (n % 2 == 0)
      count--;
    else {
      for (int i = 3; i <= sqrt(n); i += 2) {
        if (n % i == 0) {
          count--;
          break;
        }
      }
    }
  }
  cout << count << endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
bool heheda(int a);

bool heheda(int a) {
  for (int i = 2; i * 2 <= a; i++)
    if (!(a % i))
      return false;
  return true;
}

int main() {
  int n;
  cin >> n;
  int tmp2 = 0;
  int c;
  for (int i = 0; i < n; i++) {
    cin >> c;
    if (heheda(c))
      tmp2 += 1;
  }
  cout << tmp2 << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
bool heheda(int a);

bool heheda(int a) {
  for (int i = 2; i * i <= a; i++)
    if (!(a % i))
      return false;
  return true;
}

int main() {
  int n;
  cin >> n;
  int tmp2 = 0;
  int c;
  for (int i = 0; i < n; i++) {
    cin >> c;
    if (heheda(c))
      tmp2 += 1;
  }
  cout << tmp2 << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, i, x, j, flag = 0, cut = 0, a;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    a = sqrt(x);
    flag = 0;
    for (j = x - 1; j > 1; j--) {
      if (x % j == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 0)
      cut++;
  }
  cout << cut << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, i, x, j, flag = 0, cut = 0, a;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x;
    a = sqrt(x);
    flag = 0;
    for (j = a; j > 1; j--) {
      if (x % j == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 0)
      cut++;
  }
  cout << cut << endl;
  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

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

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

  int cnt = 0;
  for (int i = 0; i < n; i++) {

    //        cout << a[i] << " : ";

    int numOfDivisor = 0;

    if (a[i] == 1 ||
        (a[i] != 2 &&
         a[i] % 2 == 0)) { // 1????????????2??\????????¶??°?????????
      //            cout << "even\n";
      continue;
    }

    for (int j = 3; j < a[i] / 2; j += 2) {
      if (a[i] % j == 0) {
        numOfDivisor++;
        //                cout << j << " ";
      }
    }

    if (numOfDivisor == 0) {
      cnt++;

      //            cout << "***";
    }

    //        cout << endl;
  }

  cout << cnt << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

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

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

  int cnt = 0;
  for (int i = 0; i < n; i++) {

    //        cout << a[i] << " : ";

    int numOfDivisor = 0;

    if (a[i] == 1 ||
        (a[i] != 2 &&
         a[i] % 2 == 0)) { // 1????????????2??\????????¶??°?????????
      //            cout << "even\n";
      continue;
    }

    for (int j = 3; j <= sqrt(a[i]); j += 2) {
      if (a[i] % j == 0) {
        numOfDivisor++;
        //                cout << j << " ";
      }
    }

    if (numOfDivisor == 0) {
      cnt++;

      //            cout << "***";
    }

    //        cout << endl;
  }

  cout << cnt << endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;

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

  return true;
}

int main() {

  int n;
  cin >> n;

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

  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;

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

  return true;
}

int main() {

  int n;
  cin >> n;

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

  cout << sum << endl;

  return 0;
}
Problem p02257 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main(void) {
  bool flag;
  int n, k = 0, a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    flag = false;
    for (int j = a - 1; 1 < j; j--) {
      if (a % j == 0) {
        flag = true;
        break;
      }
    }
    if (flag == false)
      k += 1;
  }
  cout << k << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  bool flag;
  int n, k = 0, a;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    flag = false;
    for (int j = 2; j * j <= a; j++) {
      if (a % j == 0) {
        flag = true;
        break;
      }
    }
    if (flag == false)
      k += 1;
  }
  cout << k << endl;
  return 0;
}