Sep 25, 2019

Python Prime number

import math

def is_prime(n):
    if n<2 == 0:
        return False

    sqrt_n = int(math.floor(math.sqrt(n)))
    for i in range(2, sqrt_n + 1): 
        if n % i == 0:
            return False
    return True

foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
print("All list :", list(foo))
print("Prime list :", list(filter(is_prime, foo)))

Output:
All list : [2, 18, 9, 22, 17, 24, 8, 12, 27]
Prime list : [2, 17]


No comments:

Post a Comment