Returns the index of the first term in the Fibonacci sequence to contain n digits. >>> solution(1000) 4782 >>> solution(100) 476 >>> solution(50) 237 >>> solution(3) 12
(n: int = 1000)
| 26 | |
| 27 | |
| 28 | def solution(n: int = 1000) -> int: |
| 29 | """Returns the index of the first term in the Fibonacci sequence to contain |
| 30 | n digits. |
| 31 | |
| 32 | >>> solution(1000) |
| 33 | 4782 |
| 34 | >>> solution(100) |
| 35 | 476 |
| 36 | >>> solution(50) |
| 37 | 237 |
| 38 | >>> solution(3) |
| 39 | 12 |
| 40 | """ |
| 41 | f1, f2 = 1, 1 |
| 42 | index = 2 |
| 43 | while True: |
| 44 | i = 0 |
| 45 | f = f1 + f2 |
| 46 | f1, f2 = f2, f |
| 47 | index += 1 |
| 48 | for _ in str(f): |
| 49 | i += 1 |
| 50 | if i == n: |
| 51 | break |
| 52 | return index |
| 53 | |
| 54 | |
| 55 | if __name__ == "__main__": |