Fibonacci Sequence Programming Questions Reference
🌀 Fibonacci Sequence Calculation
💡 Instructions for use
- The Nth term is calculated exactly with a large integer: 21 digits for the 100th term, 209 digits for the 1000th term without loss of precision or scientific notation.
- The two starting writing methods only affect the numbering: starting from F(1)=1, the first item is 1, starting from F(0)=0, the 0th item is 0, and the rest of the numerical sequence is the same.
- The sum of the first N items satisfies the rule: F(1)+…+F(N) is equal to F(N+2) minus 1. The comparison value will be given in the result area for easy verification.
- The ratio of adjacent items will gradually approach the golden ratio, and the result area displays the current ratio and difference. The larger N, the closer it is.
The Fibonacci Sequence Programming Questions Reference is used to answer the questions: Compare the program output with the tool results, especially in large number scenarios where overflow errors are most likely to occur.
Common test cases
| Enter N | expected output |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 10 | 55 |
| 45 | 1134903170 |
| 90 | 2880067194370816120 |
| 100 | 354224848179261915075 |
The two most common pitfalls
- Use recursion to implement: Calculating the 45th item will be significantly slower and has exponential complexity. It needs to be changed to a loop or memorized.
- Use 32-bit integers to store: The 47th item begins to overflow into negative numbers, and the 93rd or so items exceed the 64-bit range. You need to use a large integer or high-precision library instead.
It is recommended to compare the program output with the tool results item by item, focusing on items 45 to 100. Overflow problems are usually exposed here. When a sequence of intermediate processes is needed, compare the generated results with the tool list to quickly locate the first erroneous item.
There are three common writing methods when implemented in code: recursion is intuitive but requires repeated calculations, iteration is efficient and easy to understand, and fast matrix exponentiation is suitable for finding very large terms. Teaching scenarios usually start with iteration.
When writing a recursive version, be careful to set the termination condition, otherwise it will recurse infinitely. Iterative versions need to pay attention to the bounds of integer overflow, especially in languages that use fixed-length integers.
Item 90 is a safe value before checking the 64-bit boundary, and item 100 requires a large integer.
📖 Glossary
- Fibonacci Sequence
- A sequence starting from 1, 1, where each term is equal to the sum of the previous two terms: 1, 1, 2, 3, 5, 8, 13..., also called the rabbit sequence.
- rabbit sequence
- The nickname for the Fibonacci sequence comes from the classic description of the sequence that was introduced using the rabbit breeding problem.
- golden section sequence
- Another name for the Fibonacci sequence, named after the ratio of adjacent items gradually approaches the golden ratio.
- golden ratio
- is a constant approximately 1.618033988749895, which the Fibonacci ratio of adjacent terms approaches as the number of terms increases.
- recursion formula
- F(n) = F(n-1) + F(n-2). Given the first two terms, all subsequent terms can be derived.
- general formula
- A closed-form expression (ratio formula) expressed in the golden ratio can directly calculate the nth term without having to recurse item by item.
- Sum of first N terms
- The result of adding F(1) to F(N) is equal to F(N+2) minus 1, and this tool will give a comparison value.
- large integer
- An integer outside the range of ordinary floating point precision. The Fibonacci sequence grows extremely fast and must be represented accurately by large integers.
- Lucas Sequence
- The sequence of numbers that has the same origin as Fibonacci starts from 2 and 1 and also satisfies the law of the sum of the first two terms.
- Fibonacci spiral
- Curves obtained by splicing squares and drawing quarter arcs with Fibonacci numbers as side lengths often appear in plant and shell forms.
🔗 All keywords and tutorials
❓ FAQ
- Why are there negative numbers after item 47??
- The upper limit of 32-bit signed integers is about 2.1 billion. The 47th item is out of range and becomes a negative number after overflow. It is necessary to use a larger type or high-precision integer.
- Do we have to change the way we write recursion??
- If only very small items are counted, it can be retained, but when counting the 40th item or more, it is recommended to change to loop recursion or add memoization, otherwise the time consumption will increase rapidly.