File tree 5 files changed +27
-27
lines changed
5 files changed +27
-27
lines changed Original file line number Diff line number Diff line change 11
11
"""
12
12
13
13
14
- def solution (n ) :
14
+ def solution (n : int = 4000000 ) -> int :
15
15
"""Returns the sum of all fibonacci sequence even elements that are lower
16
16
or equals to n.
17
17
@@ -28,13 +28,13 @@ def solution(n):
28
28
"""
29
29
i = 1
30
30
j = 2
31
- sum = 0
31
+ total = 0
32
32
while j <= n :
33
33
if j % 2 == 0 :
34
- sum += j
34
+ total += j
35
35
i , j = j , i + j
36
36
37
- return sum
37
+ return total
38
38
39
39
40
40
if __name__ == "__main__" :
Original file line number Diff line number Diff line change 11
11
"""
12
12
13
13
14
- def solution (n ) :
14
+ def solution (n : int = 4000000 ) -> int :
15
15
"""Returns the sum of all fibonacci sequence even elements that are lower
16
16
or equals to n.
17
17
18
18
>>> solution(10)
19
- [2, 8]
19
+ 10
20
20
>>> solution(15)
21
- [2, 8]
21
+ 10
22
22
>>> solution(2)
23
- [2]
23
+ 2
24
24
>>> solution(1)
25
- []
25
+ 0
26
26
>>> solution(34)
27
- [2, 8, 34]
27
+ 44
28
28
"""
29
- ls = []
29
+ even_fibs = []
30
30
a , b = 0 , 1
31
31
while b <= n :
32
32
if b % 2 == 0 :
33
- ls .append (b )
33
+ even_fibs .append (b )
34
34
a , b = b , a + b
35
- return ls
35
+ return sum ( even_fibs )
36
36
37
37
38
38
if __name__ == "__main__" :
Original file line number Diff line number Diff line change 11
11
"""
12
12
13
13
14
- def solution (n ) :
14
+ def solution (n : int = 4000000 ) -> int :
15
15
"""Returns the sum of all fibonacci sequence even elements that are lower
16
16
or equals to n.
17
17
Original file line number Diff line number Diff line change 13
13
from decimal import Decimal , getcontext
14
14
15
15
16
- def solution (n ) :
16
+ def solution (n : int = 4000000 ) -> int :
17
17
"""Returns the sum of all fibonacci sequence even elements that are lower
18
18
or equals to n.
19
19
@@ -57,8 +57,8 @@ def solution(n):
57
57
58
58
index = (math .floor (math .log (n * (phi + 2 ), phi ) - 1 ) // 3 ) * 3 + 2
59
59
num = Decimal (round (phi ** Decimal (index + 1 ))) / (phi + 2 )
60
- sum = num // 2
61
- return int (sum )
60
+ total = num // 2
61
+ return int (total )
62
62
63
63
64
64
if __name__ == "__main__" :
Original file line number Diff line number Diff line change 11
11
"""
12
12
13
13
14
- def solution (n ) :
14
+ def solution (n : int = 4000000 ) -> int :
15
15
"""Returns the sum of all fibonacci sequence even elements that are lower
16
16
or equals to n.
17
17
@@ -27,19 +27,19 @@ def solution(n):
27
27
44
28
28
"""
29
29
30
- a = [0 , 1 ]
30
+ fib = [0 , 1 ]
31
31
i = 0
32
- while a [i ] <= n :
33
- a .append (a [i ] + a [i + 1 ])
34
- if a [i + 2 ] > n :
32
+ while fib [i ] <= n :
33
+ fib .append (fib [i ] + fib [i + 1 ])
34
+ if fib [i + 2 ] > n :
35
35
break
36
36
i += 1
37
- sum = 0
38
- for j in range (len (a ) - 1 ):
39
- if a [j ] % 2 == 0 :
40
- sum += a [j ]
37
+ total = 0
38
+ for j in range (len (fib ) - 1 ):
39
+ if fib [j ] % 2 == 0 :
40
+ total += fib [j ]
41
41
42
- return sum
42
+ return total
43
43
44
44
45
45
if __name__ == "__main__" :
You can’t perform that action at this time.
0 commit comments