#include <stdio.h>
#include <math.h>
bool isPrime(long long int n) {
if(n <= 1) {
return false;
} else {
for(long long int i = 2; i <= sqrt(n); i++) {
if(n % i == 0) {
return false;
}
}
}
return true;
}
int main() {
int cases;
long long int num;
scanf("%d", &cases);
for(int i = 0; i < cases; i++) {
scanf("%lld", &num);
if(isPrime(num)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
Is there any way I could do to make this code run faster? I tried the Sieve of Eratosthenes algorithm but that was slower, and apparently this "try to divide a number from 2 to its square root" is faster, but not fast enough according to the online judge.