Python fourth day factorial

Source: Internet
Author: User

have been learning, but did not come to the blog park, write some, write a factorial small code

def f (N):    last =1     for  in range (1,n+1):        last *=i    return  lastnum=int (input ('what thenumber\n' ) Print ('f'+'('+str (num) +') = ', F (num))

Or with a recursive approach.

def fact (n):    if n==1:        return 1    return  n*fact (n-1) num=int (input (' What the number\n ') print (' fact ' + ') ( ' +str (num) + ') = ', fact (num))

The following is a Fibonacci sequence of three ideas:

1:

def Fibo (n):    befor=0    after=1 for    i in range (n-2):        ret=after+befor        befor=after        After=ret    return Retprint (Fibo (7))

2:

def Fibo (n):    if n==1:        return 0    elif n==2:        return 1    return Fibo (n-1) +fibo (n-2) print (Fibo (7))

3:

def Fibo (n):    if n<=2:        return n-1    return Fibo (n-1) +fibo (n-2) print (Fibo (7))

  

Python fourth day factorial

Related Article

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.