Two Python scripts for series (Fibonacci numbers and monkeys eating bananas)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.