-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp10.py
55 lines (43 loc) · 1019 Bytes
/
p10.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
44
45
46
47
48
49
50
51
52
53
54
55
"""
sum of all primes below 1000
"""
# stolen from p7
def is_prime(n):
lowN = 2
highN = n-1
while (lowN <= highN):
#print "lowN: %d, highN: %d" % (lowN, highN)
if n % lowN == 0:
return False
lowN += 1
highN = math.ceil(n / float(lowN))
return True
def prime_gen(n):
num = 2
while (num < n):
if is_prime(num):
yield num
num += 1
# woof - way too inefficient. My generator needs some work
sum(prime_gen(n))
def better_primes(n):
possible = set(range(2, n))
for i in range(2, n/2):
v = 0
j = 2
while v < n:
v = i * j
possible.discard(v)
j += 1
return possible
sum(better_primes(1000))
# one from the clever edu thing:
top_num = 10
primes = []
cand_list = range(2, top_num)
while len(cand_list) > 0:
i = cand_list[0]
cand_list = cand_list[1:]
primes.append(i)
cand_list = [c for c in cand_list if c % i != 0]
print sum(primes)