-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler.py
145 lines (121 loc) · 2.31 KB
/
euler.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def gen_primes():
"""
Prime Numbers Generator
"""
D = {}
q = 2
while True:
if q not in D:
yield q
D[q * q] = [q]
else:
for p in D[q]:
D.setdefault(p + q, []).append(p)
del D[q]
q += 1
def sum_squares(n):
"""
sums a sequence of squares
1^2 + 2^2 + ... + 10^2 = 385
"""
sum = 0
for i in range(1, n+1):
sum += i*i
return sum
def sum_sequence(n):
sum = 0
for i in range(1, n+1):
sum += i
return sum
def sum_digits(n):
s = 0
while n:
s += n % 10
n /= 10
return s
def fib(n, max=None):
"""
Fibonnacci
"""
ret = [1,2]
for i in range(3, n+1):
j = sum(ret[i-3:i-1])
if max and j >= max:
return ret
ret.append(j)
return ret
def mul_list(lst):
"""
Multiply a List
"""
n = 1
for i in lst:
n*=i
return n
def is_prime(n):
"""
Check if Number is Prime
"""
for i in range(2, n):
if n % i == 0:
return False
else:
return True
def get_factors(n):
"""
Return a list with a number's factors
"""
factors = []
for i in gen_primes():
if (n % i == 0):
factors.append(i)
if mul_list(factors) == n:
return factors
#----------------------------------
# PROBLEM SOLUTIONS
#----------------------------------
def p1(n):
"""
Euler Problem 1
"""
sum = 0
for i in range(1,n+1):
if ((i%3)==0 or (i%5) == 0):
sum+=i
return sum
def p2(n):
sum = 0
for i in fib(n, max=n):
if (i % 2 == 0):
sum += i
return sum
def p3(n):
factors = get_factors(n)
return factors[len(factors)-1]
def p6(n):
i = sum_squares(n)
tmp = sum_sequence(n)
j = tmp*tmp
return j-i
def p7(n):
tmp = 0
for i,j in zip(gen_primes(), range(n)):
tmp = i
return tmp
def p10(n):
sum = 0
for p in gen_primes():
if p < n:
sum+=p
else:
return sum
def p16(n):
n = pow(2,n)
return sum_digits(n)
print(p1(999))
print(p2(4000000))
print(p3(600851475143))
print(p6(100))
print(p7(10001))
print(p10(2000000))
print(p16(1000))