I\'m having trouble with this C++ code. The integer num
is a number that I want to check if it is prime. However this program is always returning false. It\'s proba
i == num
will never occur, since your loop condition is i
for(int i=2;i
As pointed out below, the else
condition here is redundant, and you only need to check from 2 to sqrt(num)
- since the remaining factors have already been checked.
There are more improvements that can be made depending on how complex you want to make the problem. Most methods in reality use probabilistic algorithms.