MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_301/sol1.py:36–54  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

34
35
36def 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
57if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected