-
Notifications
You must be signed in to change notification settings - Fork 0
/
primenumbers.py
44 lines (32 loc) · 1.17 KB
/
primenumbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import math
def is_prime(num):
"""Returns True if the number is prime
else False."""
if num <= 1:
""" 0, 1 and negative numbers are not prime numbers.
Also catered for in the filter function as ange starts from 1
"""
return False
if num == 2:
""" 0, 1 and negative numbers are not prime numbers.
Also catered for in the filter function as ange starts from 1
"""
return True
""" only need to get to square root of inputted max number
as opposed to going all the way to the last number
reduces performance time from 0.545s to 0.038s for num as 10000
"""
limit = int(math.sqrt(num) + 1)
for x in range(2, limit): # boost performance by not iterating all numbers
if num % x == 0: # not prime if divisible by other numbers
return False
else:
return True
def prime_generator(max_num):
if max_num < 0:
return False
all_primes = [2]
""" range function skips all even numbers to boost performance"""
gen_primes = list(filter(is_prime, range(1, max_num+1, 2)))
all_primes.extend(gen_primes)
return all_primes