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

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

    You can first divide your n only by your primes_under_100.

    Also, precompute more primes.

    Also, you're actually store your range() result in memory - use irange() instead and use this memory for running Sieve of Eratosthenes algorithm.

提交回复
热议问题