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

Function solution

project_euler/problem_002/sol1.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 i = 1
37 j = 2
38 total = 0
39 while j <= n:
40 if j % 2 == 0:
41 total += j
42 i, j = j, i + j
43
44 return total
45
46
47if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected