while(p*p<=m)
why is it so?please explain
I am not able to understand the logic behind this
@jatin_wadhwa,
If a number n is not a prime, it can be factored into two factors a and b :
n = a * b
Now a and b can’t be both greater than the square root of n , since then the product a * b would be greater than sqrt(n) * sqrt(n) = n . So in any factorization of n , at least one of the factors must be smaller than the square root of n , and if we can’t find any factors less than or equal to the square root, n must be a prime.
i know this is little tricky to understand so dry run the code taking different value of n , in case you have any doubt feel free to ask 