My question is about the conditional test in trial division. There seems to be some debate on what conditional test to employ. Let\'s look at the code for this from Roset
sqrt(n)
is accurate enough as long as your sqrt
is monotone increasing, it gets perfect squares right, and every unsigned int
can be represented exactly as a double
. All three of these are the case on every platform I know of.
You can get around these issues (if you consider them to be issues) by implementing a function unsigned int sqrti(unsigned int n)
that returns the floor of the square root of an unsigned int
using Newton's method. (This is an interesting exercise if you've never done it before!)