Ad
Code
Diff
  • from math import sqrt
    
    #Same but with some memoization, better if you need to check some numbers more than once
    def prime(x, cache={}): 
        if x not in cache:
            if x <= 1:
                cache[x]= False
                return False
            for i in range(2,int(sqrt(x))):
                if x % i is 0:
                    cache[x] = False
                    return False
            cache[x] = True        
        return cache[x]
    • from math import sqrt
    • def prime(x):
    • if x <= 1:
    • return False
    • for i in range(2,int(sqrt(x))):
    • if x % i is 0:
    • #Same but with some memoization, better if you need to check some numbers more than once
    • def prime(x, cache={}):
    • if x not in cache:
    • if x <= 1:
    • cache[x]= False
    • return False
    • return True
    • for i in range(2,int(sqrt(x))):
    • if x % i is 0:
    • cache[x] = False
    • return False
    • cache[x] = True
    • return cache[x]