question link : https://www.codechef.com/problems/PRB01
my code takes t as an input and then takes t no of inputs in the lines that follow, it puts these nos in a function where I check divisibility of a no. n by every no. that is <= root n. if n is divisible to i ie (n%i == 0) add 1 to count(which was initially set to 0). I tried optimising with an if statement which would break me out of the loop if count >= 2. Finally the function returns 1 if prime and 0 if the no. is composite is this approach correct, i get correct answers for the inputs i give but codechef's online judge graded it as wrong
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int prime(int);
int
main()
{
int t;
scanf("%d", &t);
if ((t >= 1) && (t <= 20)) {
for (int i = 0; i < t; i++) {
int n;
scanf("%d", &n);
if (prime(n) == 0)
printf("no\n");
else
printf("yes\n");
}
}
return 0;
}
int
prime(int x)
{
int count = 0;
int y = sqrt((double) x);
for (int i = 1; i <= y; i++) {
if (x % i == 0)
count++;
if (count >= 2)
break;
}
if (count <= 1)
return 1;
else
return 0;
}