Traditional Culture Encyclopedia - The 24 Solar Terms - Solve Python programming problems
Solve Python programming problems
There are at least two ways to achieve this.
The most commonly used method is iterative method, and its core idea is
fib(n) =? fib(n- 1) +? Fiber (n-2)
And in n
Def fib(num): if n<2 other optical fibers (No.1)+optical fiber (No.2)
This is a very simple implementation. It works well when there are few steps. When the number of steps is large, it will slow down because of the iteration of the second hand. Therefore, the intermediate value (steps from 1 to n- 1) can be saved in the calculation to reduce the amount of calculation:
This method can keep good performance when calculating 10000 steps. If you need to calculate this sequence multiple times, you can use the object to save this list of intermediate values. In the following code, the Fibonaci instance only calculates the number of steps that have not been calculated, which is more advantageous when calling repeatedly:
Fibonacci class (object):
... History =[ 1, 1]
..... define cache (itself, quantity):
...... and len (self. history) < = Quantity:
............self . history . append(self . history[- 1]+self . history[-2])
..... return? Self. history[ number]
if __name__ == '__main__ ':
....fib =? Fibonacci ()
... print (fib.calc( 100))
....print(fib.calc(32))
... print (fib.calc( 10000))
- Previous article:Valve Adjustment Sequence and Valve Clearance of Forklift Isuzu c240
- Next article:How to refuel the fuel tank for natural gas vehicles?
- Related articles
- White Snake Xiehouyu: 36 sentences
- Which month is the best for Weihai tourism?
- What is the vernal equinox? Is this the beginning of spring?
- Read the dichotomy chart (left) and the sunshine chart of a certain day (right) and complete the following questions. (The shaded part in the figure indicates the night hemisphere, and the dotted line
- How much is the throttle cleaning price?
- Is the Millennium Mid-Autumn Festival?
- According to the current lunar calendar, is it spring or summer?
- What does it mean to preserve in winter and in the wind?
- Poetry without rhetorical devices
- Solar terms and a scenic spot in Hangzhou ~ ~