How can I test for primality?

前端 未结 16 2162
南方客
南方客 2020-12-05 08:29

I am writing a little library with some prime number related methods. As I\'ve done the groundwork (aka working methods) and now I\'m looking for some optimization. Ofcours

16条回答
  •  [愿得一人]
    2020-12-05 09:04

    Your for loop should look like this:

    for (int idx = 3; idx * idx <= test; idx++) { ... }
    

    That way, you avoid floating-point computation. Should run faster and it'll be more accurate. This is why the for statement conditional is simply a boolean expression: it makes things like this possible.

提交回复
热议问题