-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectEuler.py
273 lines (255 loc) · 8.33 KB
/
ProjectEuler.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# ProjectEuler.py
# by Matthew Eads
#
# Graphical interface for solving and displaying solutions to select problems
# from www.projecteuler.org
from Tkinter import *
from dill.source import getsource
from functools import partial
from math import sqrt
from math import factorial
top = Tk()
# Displays window with description of problem, the result, and an option to
# show the code for finding the solution
# Takes the description, result, and code for a solution (all strings)
def showResult(description, result, code):
# Create new window
newtop = Toplevel()
showcode = False
def closeWin():
newtop.withdraw()
# Displays given code
def showCode(code):
showcode = True
codebloc = Text(newtop)
codebloc.insert(INSERT, code)
codebloc.insert(END, "")
codebloc.pack()
codefunc = partial(showCode, code)
# Create close buton
close = Button(newtop, text = "Close", command = closeWin)
close.pack()
# Create Show Code button
code = Button(newtop, text = "Show Code", command = codefunc)
code.pack()
# Display description and result of code
text = Text(newtop, height = (len(description)+len(result))/80+5, wrap = WORD)
text.insert(INSERT, description)
text.insert(INSERT, "\n")
text.insert(END, result)
text.pack()
if showcode:
text.insert(END, code)
newtop.mainloop()
# Returns a string containing the code used for the given function
# Only retrieves code between the _BEGIN_ and _END_ tags
def getCode(func):
allcode = getsource(eval(func))
start = allcode.index("_BEGIN_") + len("_BEGIN_") + 1
end = allcode.index("_END_", start) - 2
code = str("".join(map(str, allcode[start:end])))
return code
################## Functions for solving problems ##############################
# All functions take no arguments, and return nothing, just calling a function
# to display the result of the problem in a new window.
# Certain elements, such as descriptions and given numbers, are hard-coded
################################################################################
def p1():
sum = 0
#begin code _BEGIN_
for x in xrange(1, 1000):
if x % 3 == 0 or x % 5 == 0:
sum += x
#_END_ end code
result = "Sum = " + str(sum)
code = getCode("p1")
#print code
showResult("Problem 1: Find sum of all the multiples of 3 or 5 below 1000",
result, code)
def p2():
description = "Problem 2: Find the sum of all even numbers within the Fibonacci sequence,"
description += " not exceeding four million."
#begin code _BEGIN_
sum = 0
n1 = 1
n2 = 2
while n1 < 4000000:
if n1 % 2 == 0:
sum += n1
n2 += n1
n1 = n2 - n1
#_END_ end code
result = "Sum = " + str(sum)
code = getCode("p2")
showResult(description, result, code)
def p3():
description = "Problem 3: Find the largest prime factor of the number 600851475143"
#_BEGIN_
def isPrime(num):
x = 2
while x < sqrt(num):
if num % x == 0:
return False
x += 1
return True
num = 600851475143
primefactors = []
i = 2
while i < sqrt(num):
if num % i == 0:
if isPrime(i):
primefactors.append(i)
i += 1
#_END_
result = "Largest prime factor is: " + str(max(primefactors))
code = getCode("p3")
showResult(description, result, code)
def p4():
description = "Problem 4: Find the largest palindromic number which is the product of two 3-digit numbers"
#_BEGIN_
def isPalindrome(num):
strnum = str(num)
palindromic = True
for x in xrange(0, len(strnum)/2):
if strnum[x] != strnum[len(strnum) - x - 1]:
palindromic = False
break
return palindromic
i = 999
j = 999
palindromes = []
while i > 0:
while j > 0:
if isPalindrome(i * j):
palindromes.append(i * j)
j -= 1
i -= 1
j = 999
#_END_
result = "Largest palindrome is: " + str(max(palindromes))
code = getCode("p4")
showResult(description, result, code)
def p5():
description = "Find the smallest positive number evenly divisible by all the numbers"
description += " from 1 to 20."
#_BEGIN_
def isDivisible(num):
for x in xrange(1, 21):
if num % x != 0:
return False
return True
num = 20
print factorial(20)
while num < factorial(20):
if isDivisible(num):
break
if num %1000000 == 0:
print num
num += 20
#_END_
result = "Smallest number divisible is: " + str(num)
code = getCode("p5")
showResult(description, result, code)
def p6():
description = "Find the difference of the sum of the squares of the first hundred"
description += " natural numbers and the square of the sum of the first hundred natural numbers."
#_BEGIN_
sum1 = 0
sum2 = 0
for x in xrange(1, 101):
sum1 += x
sum2 += x * x
sum1 *= sum1
diff = sum1 - sum2
#_END_
result = "Difference is: " + str(diff)
code = getCode("p6")
showResult(description, result, code)
def p7():
description = "Find the 10001st prime number."
#_BEGIN_
primes = [2, 3]
def isPrime(num):
x = 2
while x < sqrt(num):
if num % x == 0:
return False
x += 1
return True
numprimes = 2
i = 5
interval = 2
while numprimes < 10002:
if isPrime(i):
primes.append(i)
numprimes += 1
i += interval
#_END_
result = "10001st prime is: " + str(primes[10001])
code = getCode("p7")
showResult(description, result, code)
def p8():
description = "Find the thirteen adjacent digits in the following"
description += " 1000-digit number whith the greatest product\n"
number = "73167176531330624919225119674426574742355349194934"
number += "96983520312774506326239578318016984801869478851843"
number += "85861560789112949495459501737958331952853208805511"
number += "12540698747158523863050715693290963295227443043557"
number += "66896648950445244523161731856403098711121722383113"
number += "62229893423380308135336276614282806444486645238749"
number += "30358907296290491560440772390713810515859307960866"
number += "70172427121883998797908792274921901699720888093776"
number += "65727333001053367881220235421809751254540594752243"
number += "52584907711670556013604839586446706324415722155397"
number += "53697817977846174064955149290862569321978468622482"
number += "83972241375657056057490261407972968652414535100474"
number += "82166370484403199890008895243450658541227588666881"
number += "16427171479924442928230863465674813919123162824586"
number += "17866458359124566529476545682848912883142607690042"
number += "24219022671055626321111109370544217506941658960408"
number += "07198403850962455444362981230987879927244284909188"
number += "84580156166097919133875499200524063689912560717606"
number += "05886116467109405077541002256983155200055935729725"
number += "71636269561882670428252483600823257530420752963450"
description += number
result = "Greatest product is: "
#_BEGIN_
max = 0
start = 0
end = 13
x = 0
while x < 1000-12:
product = 1
y = 0
while y < 13:
if number[y+x] == '0':
break
product *= int(number[y+x])
y += 1
if product >= max:
max = product
start = x
end = x + 13
x += 1
result += str(max)
#_END_
code = getCode("p8")
showResult(description, result, code)
# Define and pack all buttons for completed problems
B1= Button(top, text = "Problem 1", command = p1)
B2 = Button(top, text = "Problem 2", command = p2)
B3 = Button(top, text = "Problem 3", command = p3)
B4 = Button(top, text = "Problem 4", command = p4)
B5 = Button(top, text = "Problem 5", command = p5)
B6 = Button(top, text = "Problem 6", command = p6)
B7 = Button(top, text = "Problem 7", command = p7)
B8 = Button(top, text = "Problem 8", command = p8)
B1.pack()
B2.pack()
B3.pack()
B4.pack()
B5.pack()
B6.pack()
B7.pack()
B8.pack()
top.mainloop()