Skip to content

Commit 38fca2f

Browse files
committed
Added fives searcher
1 parent 61b59d1 commit 38fca2f

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

fibo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
total = 0
33
while l[-1] < 4000000:
44
l.append(l[-1] + l[-2])
5-
l.pop();
5+
l.pop()
66

77
for n in l:
88
if not n%2:

fives.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def add (a, b):
2+
return a + b
3+
4+
def subtract (a, b):
5+
return a - b
6+
7+
def multiply (a, b):
8+
return a * b
9+
10+
def divide (a, b):
11+
if b == 0:
12+
return None
13+
else:
14+
return a/b
15+
16+
def search (depth):
17+
"""Returns a dictionary whose keys are integers and whose values are the
18+
minumum number of 5s needed to make the key using +-*/."""
19+
results = {5:1}
20+
newresults = {}
21+
for i in range(depth):
22+
for k1, v1 in results.iteritems():
23+
for k2, v2 in results.iteritems():
24+
for f in [add, subtract, multiply, divide]:
25+
newkey = f(k1, k2)
26+
newval = v1 + v2
27+
if not newkey in results and newkey != None and (not newkey in newresults or newresults[newkey] > newval) and newval<=depth:
28+
newresults[newkey] = newval
29+
30+
results = dict(newresults.items() + results.items())
31+
print results.items()

fives.pyc

1.36 KB
Binary file not shown.

main.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
import primes
1+
import fives
22

3-
def sumPrimePowers(STOP):
4-
A = int(STOP**0.5)+1
5-
B = int(STOP**(1.0/3.0))+1
6-
C = int(STOP**0.25)+1
7-
P = primes.first(A)
8-
Results = set([])
9-
for a in range(A):
10-
for b in range(B):
11-
for c in range(C):
12-
n = P[a]**2 + P[b]**3 + P[c]**4
13-
if n < STOP:
14-
Results.add(n)
15-
return len(Results)
16-
17-
print sumPrimePowers(5000000)
3+
print fives.search(5)

0 commit comments

Comments
 (0)