Advertisement
doanhtu

Project Euler #57

Mar 8th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. from functools import lru_cache
  2. from fractions import Fraction
  3.  
  4.  
  5. @lru_cache(maxsize=None)
  6. def calculate_fraction(n):
  7.     if n == 0:
  8.         return 0
  9.     if n == 1:
  10.         return 1
  11.     else:
  12.         return 1 + Fraction(1, 1 + continued_fraction(n - 1))
  13.  
  14.  
  15. is_more_digits = lambda l: len(str(l.numerator)) > len(str(l.denominator))
  16.  
  17.  
  18. def main():
  19.     count = 0
  20.     for i in range(2, 1003):
  21.         if is_more_digits(calculate_fraction(i)):
  22.             count += 1
  23.     return count
  24.  
  25.  
  26. if __name__ == '__main__':
  27.     main()
  28.  
  29. ###################################################
  30. # CPU times: user 128 ms, sys: 0 ns, total: 128 ms
  31. # Wall time: 126 ms
  32. # Out[141]: 153
  33. ###################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement