Can anyone tell me why am I getting wrong answer?

CodeChef: Practical coding for everyone : link to the solution. My program successfully ran the given two test cases. Can anyone help me find what’s wrong in my code?

Check your bounds of the for loop while checking if it is a prime number or not.
It should be i<=sum/2.

1 Like

But does that matter?? In primality test we check only uptil sqrt(n) so i don’t think checking upto i < sum / 2 matters.

The basic limits to find whether the number is prime or not is:

for optimization purpose we cut out the upper limit of the for loop to
1. i<=sqrt(num)
2. i<=sum/2

Answer to your question:
when we use upper limit of for as sqrt(num) loop looks like:
we use a equal to sign there, but in the above solution the loop used is:

so when num=4.
he misses out i=2.

1 Like

Hey! You should read about primality test.
I will attach a video link:L1 | Check for Prime, Get Divisors, Practice Problem | Raj (Striver) | Prime Numbers for CP - YouTube
check this out or else you can even read some blogs.
I used primality test to your solution and it passed up.
Time complexity:O(Sqrt(N))
I feel like somehow time limit was getting exceeded or the process was stuck in the while loop .
my solution (which i modified using primality test):CodeChef: Practical coding for everyone
thanks @rishabh7896 for pointing out my mistake :smiley:

Thanks bro!! I just missed that case

1 Like

:grinning: :grinning:

1 Like