Python Case 006 (Fibonacci series)

Source: Internet
Author: User



#! /usr/bin/python
#-*-coding:utf-8-*-"" "An interesting topic Fibonacci sequence

" "
"

#the method We know to solve this issue  ----recursion

def fib (n):
    if n ==1 or n ==2: return
        1
    else:
        Retu RN Fib (n-1) + fib (n-2)
n = ten
print "Fibonacci sequence the value of%d is:"% N,FIB (n)

# somewhat meaning

def fibv2 ( N):
    a,b =1,1
    for I in Range (n-1):  # if n = ten then 10-1 = 9 Range (9), recursion 9 times
        a,b= B,a+b
 # ...
        print ' a= ', A, ' b= ', b return
    a
print Fibv2 (Ten) "" "
Fibonacci sequence value:  55
a= 1 b= 2
a= 2 b= 3
a= 3 b= 5
a= 5 b= 8
a= 8 b=
a= b= a= a b= a= 34 b=
a= b= "" "





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.