I was researching algorithm of finding primal numbers and saw the statement below, I don't get why.
while (k*k <= n)
is better than
while (k <= Math.sqrt(n))
Is it because of function call? That calling function uses more resources.
Update: As @Codor said I think I need to mention that k
is changed inside loop, while n
is not changed.
So if I store Math.sqrt(n)
before and use it every time, will it be more efficient than multiplying each time k*k
?