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

Function fibonacci

project_euler/problem_025/sol1.py:28–55  ·  view source on GitHub ↗

Computes the Fibonacci number for input n by iterating through n numbers and creating an array of ints using the Fibonacci formula. Returns the nth element of the array. >>> fibonacci(2) 1 >>> fibonacci(3) 2 >>> fibonacci(5) 5 >>> fibonacci(10) 55 >>

(n: int)

Source from the content-addressed store, hash-verified

26
27
28def fibonacci(n: int) -> int:
29 """
30 Computes the Fibonacci number for input n by iterating through n numbers
31 and creating an array of ints using the Fibonacci formula.
32 Returns the nth element of the array.
33
34 >>> fibonacci(2)
35 1
36 >>> fibonacci(3)
37 2
38 >>> fibonacci(5)
39 5
40 >>> fibonacci(10)
41 55
42 >>> fibonacci(12)
43 144
44
45 """
46 if n == 1 or not isinstance(n, int):
47 return 0
48 elif n == 2:
49 return 1
50 else:
51 sequence = [0, 1]
52 for i in range(2, n + 1):
53 sequence.append(sequence[i - 1] + sequence[i - 2])
54
55 return sequence[n]
56
57
58def fibonacci_digits_index(n: int) -> int:

Callers 1

fibonacci_digits_indexFunction · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected