Python 2: The addition and multiplication of numbers within 100 are calculated by cyclic and recursive functions respectively.

Source: Internet
Author: User

Look at the recursion of the function tonight, inside the function, you can call other functions. If a function calls itself internally, the function is a recursive function. the advantage of recursive functions is that they are simple in definition and clear in logic. In theory, all recursive functions can be written in a circular way, but the logic of the loop is not as clear as recursion.

It seems that there are basically three ways to write about the number of 100 or less in the previous cycle:

1. For loop

sum = 0

For I in Range (1,101):

sum = Sum +i

Print sum

2 While loop:

Sum =0

I =1

While I <101:

sum = Sum +i

i = i +1

Print sum


3. Recursive functions:

def fact (N):

Return Fact_iter (0,1,n)

def fact_iter (product, COUNT, max):

If Count >max:

Return product

Return Fact_iter (Product+count, count+1, Max)


print fact (100)


corresponding to the multiplication of the number within 100, the following wording:

    1. For loop

sum = 1

For I in Range (1,101):

sum = Sum *i

Print sum


2.while Cycle

Sum =1

I =1

While I <101:

sum = Sum *i

i = i +1

Print sum


3. Recursive functions:

def fact (N):

Return Fact_iter (1,1,n)

def fact_iter (product, COUNT, max):

If Count >max:

Return product

Return Fact_iter (Product*count, count+1, Max)


print fact (100)



The use of recursive functions requires careful prevention of stack overflow. In the computer, the function call is implemented through a stack (stack) of this data structure, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will be reduced by a stack of frames. Because the size of the stack is not infinite, there are too many recursive calls that can cause the stack to overflow.

The method of solving recursive call stack overflow is optimized by tail recursion , in fact, the effect of tail recursion and loop is the same, so it is possible to think of the loop as a special tail recursive function.

Tail recursion means that when a function returns, it calls itself, and the return statement cannot contain an expression. In this way, the compiler or interpreter can optimize the tail recursion, so that the recursive itself, regardless of the number of calls, only occupy a stack frame, there is no stack overflow situation.


The advantage of using a recursive function is that the logic is simple and clear, and the disadvantage is that too-deep calls cause a stack overflow.

The language that is optimized for tail recursion can prevent stack overflow by tail recursion. The tail recursion is actually equivalent to the loop, and the programming language without the loop statement can only be looped by the tail recursion.


This article is from the "Zhongyuan Fire Unicorn" blog, please be sure to keep this source http://robertoji.blog.51cto.com/1165386/1590854

Python 2: The addition and multiplication of numbers within 100 are calculated by cyclic and recursive functions respectively.

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.