示例#1
0
#!/usr/bin/env python

from common import memo
#denominations of currency
values = [200, 100, 50, 20, 10, 5, 2, 1]

#recursive function to find permutations of the currency passed in
def permutations(denomination, maxNum = 200):
	count = 0
	for v in values:
		#if the number is greater than the value or max number to go to.
		if v > denomination or v > maxNum:
			continue
		#if they are equal then add 1 otherwise continue down permutations
		if v == denomination:
			count += 1
		else:
			count += permutations(denomination - v, v) 
	return count

#attach memory function to recursive function
permutations = memo(permutations)

print permutations(200)

示例#2
0
#!/usr/bin/python
#get the memo function from common
from common import memo

def fib(n):
	if n == 2 or n == 1:
		return 1
	else:
		return fib(n-1) + fib(n-2)

#attach memory function to fibinoci function
fib = memo(fib)

#how many digits to look for
numDigits = 1000

#largest number given in problem
count = 12

#loop while the number of digits is less than 1000
while(len(str(fib(count))) < 1000):
	count += 1

#print result
print count
示例#3
0
def sumFact(num):
	global facts
	total = 0
	for i in str(num):
		total += facts[int(i)]
	return total

#value of factorials
facts = [1]
for i in range(1,10):
	facts.append(facts[i-1] * i)

#holds the chain
nums = {}
#attach a memory function
sumFact = memo(sumFact)
#count of the number of numbers that have chain = 60
count = 0

#for all numbers less than 1,000,000
for i in range(0, 1000000):
	#add the number to the chain
	nums[i] = 0
	j = sumFact(i)
	#while the chain number isn't in the chain already
	while j not in nums:
		nums[j] = 0
		j = sumFact(j)
	#if its length is 60
	if len(nums) == 60:
		count += 1