Untitled
raw download clone
TEXT
views 14
,
size 783 b
def gen_prime_numbers(x):
    prime=[True]*x
    for i in range(2,int(math.ceil(math.sqrt(x)))): 
        if prime[i]:
        	j=i*i
        	while j<x:
        		prime[j]=False
        		j+=i
    return {i for i, is_prime in enumerate(prime) if is_prime}


def is_simple(n):
    if n < 0 or int(n) != n:
        raise ValueError("Invalid input number")
    if not is_simple.cache:
    	is_simple.cache = gen_prime_numbers(10000)
    if n in is_simple.cache:
    	return True
    else:
    	if n > max(is_simple.cache):
    		is_simple.cache = gen_prime_numbers(n * 2)
    		return is_simple(n)
    	return False


def find_simple(numbers):
	return [n for n in numbers if is_simple(n)]

is_simple.cache = None

print(find_simple([17, 2, 17, 5, 6, 18]))
close fullscreen
Login or Register to edit or fork this paste. It's free.