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)
| 26 | |
| 27 | |
| 28 | def 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 | |
| 58 | def fibonacci_digits_index(n: int) -> int: |
no test coverage detected