The Fibonacci sequence (Fibonacci sequence), which was introduced by the mathematician Leonardo's Fibonacci (Leonardoda Fibonacci) as an example of rabbit reproduction, is also known as the "rabbit sequence", and because its two adjacent items are infinitely closer to the golden ratio, So also known as the Golden section of the series, refers to a number of such a series: 1, 1, 2, 3, 5, 8, 13, 21, the 、......, namely, the latter is the first two and.
#!/usr/bin/python#coding:utf-8#斐波那契数列x=[0,1]for i in range(int(raw_input(‘请输入数字:‘))):tmp=x[-1]+x[-2]x.append(tmp)print x
Monkey Eat banana problem: There is a bunch of bananas, monkeys eat half of the first day, did not endure to eat one more, every day is like this, eat more than half eat one, to the Nineth day there is still one, beg a total number of bananas. This sort of problem boils down to a formula that is x**1= (X2+1*)2.
#!/usr/bin/python#coding:utf-8#每天吃一半多一个,第九天还剩1个,计算一共有多少个香蕉i=1a=9while 0<a<=9:tmp=(i+1)*2i=tmpa-=1print i
Sum up the series of related problems: related to similar series of questions, and ultimately through the formula and the cycle to calculate, so the analysis of the problem should be summed up the law, the formula, and then the code to achieve, first from the number of items to find the law, more items to push.
It is important to note that the choice of loop mode is to select a For loop or a while loop.
Two Python scripts for series (Fibonacci numbers and monkeys eating bananas)