Recursion of the full-stack Python series

Source: Internet
Author: User
The so-called recursion is actually a function called by the function itself. after the specified conditions are met, the function exits layer by layer. for example, there was a mountain, a temple in the mountains, and an old monk in the temple. he was telling a story to the monk! What is the story? "There used to be a mountain, a temple in the mountains, and an old monk in the temple,... the so-called recursion is actually the function itself calling the function until the specified conditions are met and then exit the function layer by layer. for example

There used to be a mountain, a temple in the mountains, and an old monk in the temple. they are telling stories to the little monk! What is the story? "There was a mountain, a temple in the mountains, and an old monk in the temple. he was telling a story to the monk! What is the story? 'I used to have a mountain, a temple in the mountains, and an old monk in the temple. I am telling a story to the little monk! What is the story ?...... '"

  • Compile a Fibonacci series using functions

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368

The Fibonacci series is the first to add the two numbers to get the next number, followed

The code is as follows:

#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ def Counter (n1, n2): if n1> 10000: # exit return print ("Counter:", n1) if the value to be calculated is greater than 10000) # output the current calculated value n3 = n1 + n2 # The first value plus the first value equals to the third value Counter (n2, n3) # Call the Counter function, at this time, the first value is the last value passed by the call function, and the second value is the calculated third value Counter (0, 1) # Call the Counter function

Output result

/Usr/bin/python3.5/home/ansheng/Documents/PycharmProjects/blogcodes/Fibonacci. pyCounter: 0 Counter: 1 Counter: 1 Counter: 2 Counter: 3 Counter: 5 Counter: 8 Counter: 13 Counter: 21 Counter: 34 Counter: 55 Counter: 89 Counter: 144 Counter: 233 Counter: 377 Counter: 610 Counter: 987 Counter: 1597 Counter: 2584 Counter: 4181 Counter: 6765 Process finished with exit code 0
  • Recursively obtain the number of 10th in the Fibonacci series and return this value to the caller.

Code:

#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ def Counter (Index, Start, End): print ("% d calculation, the first digit is % d, and the second digit is % d "% (Index, Start, End) if Index = 10: # If the value to be calculated is 10, exit return Start N = Start + End # N equals to the first Number plus the second Number = Counter (Index + 1, End, N) # continue to call the Counter function. End is equivalent to the first number passed to the function. N is the second number passed to the function. return Numberresult = Counter (1, 0, 1) print ("The resulting number is:", result)

Output result

/Usr/bin/python3.5/home/ansheng/Documents/PycharmProjects/blogcodes/recursion. py calculates 1st times, the first digit is 0, the second digit is 1 2nd times, the first digit is 1, and the second digit is 1 3rd times, the first digit is 1, the second digit is 2 4th, the first digit is 2, the second digit is 3 5th, and the first digit is 3, the second digit is calculated 6th times, the first digit is 5, the second digit is 8 7th times, the first digit is 8, and the second digit is 13 8th times, the first digit is 13, the second digit is 21 9th, the first digit is 21, the second digit is 34 10th, and the first digit is 34, the second number is 55. The result is 34 Process finished with exit code 0.

Original article link


The so-called recursion means that the function itself calls the function and exits the function layer by layer after the specified conditions are met. for example

There used to be a mountain, a temple in the mountains, and an old monk in the temple. they are telling stories to the little monk! What is the story? "There was a mountain, a temple in the mountains, and an old monk in the temple. he was telling a story to the monk! What is the story? 'I used to have a mountain, a temple in the mountains, and an old monk in the temple. I am telling a story to the little monk! What is the story ?...... '"

  • Compile a Fibonacci series using functions

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368

The Fibonacci series is the first to add the two numbers to get the next number, followed

The code is as follows:

#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ def Counter (n1, n2): if n1> 10000: # exit return print ("Counter:", n1) if the value to be calculated is greater than 10000) # output the current calculated value n3 = n1 + n2 # The first value plus the first value equals to the third value Counter (n2, n3) # Call the Counter function, at this time, the first value is the last value passed by the call function, and the second value is the calculated third value Counter (0, 1) # Call the Counter function

Output result

/Usr/bin/python3.5/home/ansheng/Documents/PycharmProjects/blogcodes/Fibonacci. pyCounter: 0 Counter: 1 Counter: 1 Counter: 2 Counter: 3 Counter: 5 Counter: 8 Counter: 13 Counter: 21 Counter: 34 Counter: 55 Counter: 89 Counter: 144 Counter: 233 Counter: 377 Counter: 610 Counter: 987 Counter: 1597 Counter: 2584 Counter: 4181 Counter: 6765 Process finished with exit code 0
  • Recursively obtain the number of 10th in the Fibonacci series and return this value to the caller.

Code:

#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ def Counter (Index, Start, End): print ("% d calculation, the first digit is % d, and the second digit is % d "% (Index, Start, End) if Index = 10: # If the value to be calculated is 10, exit return Start N = Start + End # N equals to the first Number plus the second Number = Counter (Index + 1, End, N) # continue to call the Counter function. End is equivalent to the first number passed to the function. N is the second number passed to the function. return Numberresult = Counter (1, 0, 1) print ("The resulting number is:", result)

Output result

/Usr/bin/python3.5/home/ansheng/Documents/PycharmProjects/blogcodes/recursion. py calculates 1st times, the first digit is 0, the second digit is 1 2nd times, the first digit is 1, and the second digit is 1 3rd times, the first digit is 1, the second digit is 2 4th, the first digit is 2, the second digit is 3 5th, and the first digit is 3, the second digit is calculated 6th times, the first digit is 5, the second digit is 8 7th times, the first digit is 8, and the second digit is 13 8th times, the first digit is 13, the second digit is 21 9th, the first digit is 21, the second digit is 34 10th, and the first digit is 34, the second number is 55. The result is 34 Process finished with exit code 0.

For more articles about the full-stack Python series of recursion, refer to the PHP Chinese network!

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.