Similar problem: Sum Of Prime
I've done the given problem in roughly \$\mathcal{O}(n\sqrt{n})\$.
How can I improve this algorithm?
#include <stdio.h>
int isPrime(long long int n)
{
long long int i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
int main(void) {
int i, t, prime;
long long int n, start, end;
scanf("%d", &t);
for(i=0;i<t;i++)
{
prime = 0;
scanf("%lld", &n);
if(n<4)
printf("No\n");
else
{
if(n%2==0)
printf("Yes\n");
else
{
start = 2;
end = n-2;
while(start<=end)
{
if(isPrime(start) && isPrime(end))
{
printf("Yes\n");
prime = 1;
break;
}
else
{
start++;
end–-;
}
}
if(prime==0)
printf("No\n");
}
}
}
return 0;
}
“...”
. \$\endgroup\$2
(the only even prime number). \$\endgroup\$n = p + q
is odd thenp
is even andq
is odd, or vice versa. There is only one even prime number. – Btw, if this is from a programming challenge then you might add a link to the problem. \$\endgroup\$