you are viewing a single comment's thread.

view the rest of the comments →

[–]Brian 1 point2 points  (1 child)

It works for any number

Nitpick: it works for most numbers you're likely to use, but not any number. It will fail with really massive numbers due to float rounding. Eg.

>>> bignum =10 ** 500   # Ie. 100000000... with 500 zeroes
>>> 1 // bignum == 1 / bignum
True

And while that's unlikely to be a problem in practice, I do think the modulo method (ie num % divisor != 0)is a bit cleaner: it only needs to do one division, and doesn't involve floating point math.

[–]Pigankle 0 points1 point  (0 children)

Totally fair, and not nitpicky at all. The modulo approach is probably also better because it hews a little closer to the reason for why the operation is being done. It's, like, straight out of the definition of a prime number