Solve problem 151.

main
felixm 2024-04-13 18:31:32 -04:00
parent 06c46a3e26
commit 642432cef8
1 changed files with 31 additions and 0 deletions

31
python/e151.py Normal file
View File

@ -0,0 +1,31 @@
from fractions import Fraction
from functools import lru_cache
@lru_cache
def expected_singles(sheets):
count = 0
if len(sheets) == 1 and sheets[0] == 5:
return 0
elif len(sheets) == 1:
count += 1
weight = Fraction(1, len(sheets))
for sheet in sheets:
nsheets = list(sheets)
nsheets.remove(sheet)
for nsheet in range(sheet, 5):
nsheets.append(nsheet + 1)
count += weight * expected_singles(tuple(sorted(nsheets)))
return count
def euler_151():
return round(float(expected_singles(tuple([2, 3, 4, 5]))), 6)
if __name__ == "__main__":
solution = euler_151()
print("e151.py: " + str(solution))
assert(solution == 0.464399)