For any given exponent x >= 0, 1 <= n <= 2^x. This function returns how many Nim games are lost given that each Nim game has three heaps of the form (n, 2*n, 3*n). >>> solution(0) 1 >>> solution(2) 3 >>> solution(10) 144
(exponent: int = 30)
| 34 | |
| 35 | |
| 36 | def solution(exponent: int = 30) -> int: |
| 37 | """ |
| 38 | For any given exponent x >= 0, 1 <= n <= 2^x. |
| 39 | This function returns how many Nim games are lost given that |
| 40 | each Nim game has three heaps of the form (n, 2*n, 3*n). |
| 41 | >>> solution(0) |
| 42 | 1 |
| 43 | >>> solution(2) |
| 44 | 3 |
| 45 | >>> solution(10) |
| 46 | 144 |
| 47 | """ |
| 48 | # To find how many total games were lost for a given exponent x, |
| 49 | # we need to find the Fibonacci number F(x+2). |
| 50 | fibonacci_index = exponent + 2 |
| 51 | phi = (1 + 5**0.5) / 2 |
| 52 | fibonacci = (phi**fibonacci_index - (phi - 1) ** fibonacci_index) / 5**0.5 |
| 53 | |
| 54 | return int(fibonacci) |
| 55 | |
| 56 | |
| 57 | if __name__ == "__main__": |