How to create the most compact mapping n → isprime(n) up to a limit N?

后端 未结 30 2706
遇见更好的自我
遇见更好的自我 2020-11-22 02:11

Naturally, for bool isprime(number) there would be a data structure I could query.
I define the best algorithm, to be the algorithm that pr

30条回答
  •  名媛妹妹
    2020-11-22 02:54

    Here's my take on the answer:

    def isprime(num):
        return num <= 3 or (num + 1) % 6 == 0 or (num - 1) % 6 == 0
    

    The function will return True if any of the properties below are True. Those properties mathematically define what a prime is.

    1. The number is less than or equal to 3
    2. The number + 1 is divisible by 6
    3. The number - 1 is divisible by 6

提交回复
热议问题