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

Function solution

project_euler/problem_002/sol3.py:19–44  ·  view source on GitHub ↗

Returns the sum of all even fibonacci sequence elements that are lower or equal to n. >>> solution(10) 10 >>> solution(15) 10 >>> solution(2) 2 >>> solution(1) 0 >>> solution(34) 44

(n: int = 4000000)

Source from the content-addressed store, hash-verified

17
18
19def solution(n: int = 4000000) -> int:
20 """
21 Returns the sum of all even fibonacci sequence elements that are lower
22 or equal to n.
23
24 >>> solution(10)
25 10
26 >>> solution(15)
27 10
28 >>> solution(2)
29 2
30 >>> solution(1)
31 0
32 >>> solution(34)
33 44
34 """
35
36 if n <= 1:
37 return 0
38 a = 0
39 b = 2
40 count = 0
41 while 4 * b + a <= n:
42 a, b = b, 4 * b + a
43 count += a
44 return count + b
45
46
47if __name__ == "__main__":

Callers 1

sol3.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected