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 Falsefor 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]
test.assert_equals(prime(12345),False) test.assert_equals(prime(36),False) test.assert_equals(prime(36),False) test.assert_equals(prime(0), False) test.assert_equals(prime(1), False) test.assert_equals(prime(2), True) test.assert_equals(prime(7), True) test.assert_equals(prime(13), True) test.assert_equals(prime(97), True) test.assert_equals(prime(29), True) test.assert_equals(prime(115249), True)
- test.assert_equals(prime(12345),False)
- test.assert_equals(prime(36),False)
- test.assert_equals(prime(36),False)
- test.assert_equals(prime(0), False)
- test.assert_equals(prime(1), False)
- test.assert_equals(prime(2), True)
- test.assert_equals(prime(7), True)
- test.assert_equals(prime(13), True)
- test.assert_equals(prime(97), True)
- test.assert_equals(prime(29), True)
- test.assert_equals(prime(115249), True)