Which is the fastest algorithm to find prime numbers?

后端 未结 14 1418
情深已故
情深已故 2020-11-22 06:49

Which is the fastest algorithm to find out prime numbers using C++? I have used sieve\'s algorithm but I still want it to be faster!

14条回答
  •  清酒与你
    2020-11-22 07:12

    It depends on your application. There are some considerations:

    • Do you need just the information whether a few numbers are prime, do you need all prime numbers up to a certain limit, or do you need (potentially) all prime numbers?
    • How big are the numbers you have to deal with?

    The Miller-Rabin and analogue tests are only faster than a sieve for numbers over a certain size (somewhere around a few million, I believe). Below that, using a trial division (if you just have a few numbers) or a sieve is faster.

提交回复
热议问题