Recursive functions in Python

Source: Internet
Author: User

Recursive definition: An application that directly or indirectly invokes the recursive function itself within a function:

To find the factorial of a number

1 def Jiecheng (n): 2     if n = = 1:3         return 14     else:5          return N*jiecheng (n-1)6print(Jiecheng (4))

The first few Fibonacci numbers

1 def f (n): 2     if n = = 2:3         return 14     elif n ==1:  5         return 16     else:7         return f (n-1) +f (n-2)8print(f (33))

Binary Method Search

1L = [1,3,5,6,11,13,15,17,21,32,36,52,56,58,66,77,88,99]2 defFind (l,n,start=0,end=None):3end = Len (l)ifEnd isNoneElseEnd4Mid_index = (end-start)//2 +Start5     ifstart>=End:6         return 'I can't find them .'7     Else:8         ifN>L[mid_index]:9             returnFind (l,n,mid_index+1, end)Ten         elifn<L[mid_index]: One             returnFind (l,n,start,mid_index-1) A         Else:returnMid_index -  -res = FIND (l,17) the Print(RES)

Recursive functions in Python

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.