Wednesday, 29 June 2011

Project Euler problem 162

http://projecteuler.net/index.php?section=problems&id=162


# python 2.x

import string

digits=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']

def f2(cache, have_0=False, have_1=False, have_A=False, have_something_in_front=False, remain=0):

fn_params=(have_0, have_1, have_A, have_something_in_front, remain)
if fn_params in cache:
return cache[fn_params]

if remain==0:
if have_0 and have_1 and have_A:
return 1
else:
return 0
else:
rt=0
for d in digits:
rt=rt + f2 (cache, have_0 or ('0' in d and have_something_in_front), have_1 or ('1' in d), have_A or ('A' in d), have_something_in_front or (d != '0'), remain-1)

cache[fn_params]=rt
return rt

print (string.upper(hex(f2({}, remain=16))[2:][:-1]))

No comments:

Post a Comment