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

后端 未结 5 2372
梦毁少年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:16

    Well, I have a follow-up to my comment under (very good) Peter Van Der Heijden's answer about there being nothing good for really large primes (numbers in general) in the "popular" Python libraries. Turns out I was wrong - there is one in sympy (great library for symbolic algebra, among others):

    https://docs.sympy.org/latest/modules/ntheory.html#sympy.ntheory.primetest.isprime

    Of course, it may yield false positives above 10**16, but this is already much better than anything else I could get doing nothing (except maybe pip install sympy ;) )

提交回复
热议问题