Quickly determine if a number is prime in Python for numbers < 1 billion

后端 未结 5 2380
梦毁少年i
梦毁少年i 2021-02-20 11:06

My current algorithm to check the primality of numbers in python is way to slow for numbers between 10 million and 1 billion. I want it to be improved knowing that I will never

5条回答
  •  借酒劲吻你
    2021-02-20 11:27

    For solving Project Euler problems I did what you suggest in your question: Implement the Miller Rabin test (in C# but I suspect it will be fast in Python too). The algorithm is not that difficult. For numbers below 4,759,123,141 it is enough to check that a number is a strong pseudo prime to the bases 2, 7, 61. Combine that with trial division by small primes.

    I do not know how many of the problems you have solved so far, but having a fast primality test at your disposal will be of great value for a lot of the problems.

提交回复
热议问题