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

Function solution

project_euler/problem_002/sol2.py:19–42  ·  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 even_fibs = []
37 a, b = 0, 1
38 while b <= n:
39 if b % 2 == 0:
40 even_fibs.append(b)
41 a, b = b, a + b
42 return sum(even_fibs)
43
44
45if __name__ == "__main__":

Callers 1

sol2.pyFile · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected