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

后端 未结 30 2742
遇见更好的自我
遇见更好的自我 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:35

    import math
    import time
    
    
    def check_prime(n):
    
        if n == 1:
            return False
    
        if n == 2:
            return True
    
        if n % 2 == 0:
            return False
    
        from_i = 3
        to_i = math.sqrt(n) + 1
    
        for i in range(from_i, int(to_i), 2):
            if n % i == 0:
                return False
        return True
    

提交回复
热议问题