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

后端 未结 5 2292
梦毁少年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条回答
  •  梦毁少年i
    2021-02-20 11:17

    def isprime(num):
    if (num==3)or(num==2):
        return(True)
    elif (num%2 == 0)or(num%5 == 0):
        return (False)
    elif ((((num+1)%6 ==0) or ((num-1)%6 ==0)) and (num>1)):
        return (True)
    else:
        return (False)
    

    I think this code is the fastest ..

提交回复
热议问题