project-euler/121_disc_game_prize_fund.py

19 lines
495 B
Python

#!/usr/bin/env python3
from fractions import Fraction
def search(turns, wins=0, discs=2, cache={}):
if discs - 2 == turns:
return 2* wins > turns
if turns // 2 - wins > turns - discs + 1:
return 0
if (discs, wins) in cache:
return cache[(discs, wins)]
r = Fraction(1, discs) * search(turns, wins+1, discs+1)
r += Fraction(discs-1, discs) * search(turns, wins, discs+1)
cache[(discs, wins)] = r
return r
N = 15
print(int(search(N)**(-1)))