The so-called recursion is actually the function itself that calls the function until a layer exits after the specified condition is met, for example
Once there was a mountain, there is a temple in the mountain, there is an old monk in the temple, is telling a story to the Little monk! What is the story? "There used to be a mountain, a temple in the mountains, an old monk in the temple, telling a story to the Little monk!" What is the story? There used to be a mountain, a temple in the mountains, an old monk in the Temple, and a story for the Little Monk. What is the story? ......’”
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 sequence is the first number that is added to the two numbers to get the back one, in turn
The code is as follows
#!/usr/bin/env python# _*_ coding:utf-8 _*_def Counter (N1, N2): if N1 > 10000: # Exit return< when the value to be calculated is greater than 10000 C6/>print ("Counter:", N1) # The output is currently calculated to that value n3 = n1 + N2 # The first value plus the first value equals the third value Counter (N2, N3) # Call counter function , the first value is the last value passed by the calling function, and the second value is the third value computed counter (0, 1) # Call counter function
Output results
/usr/bin/python3.5/home/ansheng/documents/pycharmprojects/blogcodes/Fibonacci. Pycounter:0counter:1counter:1counter: 2counter:3counter:5counter:8counter:13counter:21counter:34counter:55counter:89counter:144counter:233counter:37 7counter:610counter:987counter:1597counter:2584counter:4181counter:6765process finished with exit code 0
Code:
#!/usr/bin/env python# _*_ coding:utf-8 _*_def Counter (Index, Start, End): print ("%d", the first number is%d, the second number is%d "% ( Index, start, End) If index = =: # If the value to be calculated is 10, exit return start n = start + End # n equals the first number plus the second number n Umber = Counter (Index + 1, End, N) # continues 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) p Rint ("The number obtained is:", result)
Output results
/usr/bin/python3.5/home/ansheng/documents/pycharmprojects/blogcodes/ Recursive. py the 1th time calculation, the first number is 0, the second number is 1 for the 2nd time, the first number is 1, the second number is 1 3rd time, the first number is 1, the second number is 2, the first number is 2, the second is 3 5th, the first number is 3, the second number is 5 6th, the first number is 5, the second number is 8 for the 7th time, the first number is 8, the second number is 13 for the 8th time, the first number is 13, the second number is 21, the first number is 21, the second is 34 10th, the first number is 34, the second is 55, the number is: 34Process finished with exit code 0
Original link
The so-called recursion is actually the function itself that calls the function until a layer exits after the specified condition is met, for example
Once there was a mountain, there is a temple in the mountain, there is an old monk in the temple, is telling a story to the Little monk! What is the story? "There used to be a mountain, a temple in the mountains, an old monk in the temple, telling a story to the Little monk!" What is the story? There used to be a mountain, a temple in the mountains, an old monk in the Temple, and a story for the Little Monk. What is the story? ......’”
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 sequence is the first number that is added to the two numbers to get the back one, in turn
The code is as follows
#!/usr/bin/env python# _*_ coding:utf-8 _*_def Counter (N1, N2): if N1 > 10000: # Exit return< when the value to be calculated is greater than 10000 C3/>print ("Counter:", N1) # The output is currently calculated to that value n3 = n1 + N2 # The first value plus the first value equals the third value Counter (N2, N3) # Call the Counter function, At this point the first value is the last value passed by the calling function, and the second value is the computed third value counter (0, 1) # Call counter function
Output results
/usr/bin/python3.5/home/ansheng/documents/pycharmprojects/blogcodes/Fibonacci. Pycounter:0counter:1counter:1counter: 2counter:3counter:5counter:8counter:13counter:21counter:34counter:55counter:89counter:144counter:233counter:37 7counter:610counter:987counter:1597counter:2584counter:4181counter:6765process finished with exit code 0
Code:
#!/usr/bin/env python# _*_ coding:utf-8 _*_def Counter (Index, Start, End): print ("%d", the first number is%d, the second number is%d "% ( Index, start, End) If index = =: # If the value to be calculated is 10, exit return start n = start + End # n equals the first number plus the second number n Umber = Counter (Index + 1, End, N) # continues 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) p Rint ("The number obtained is:", result)
Output results
/usr/bin/python3.5/home/ansheng/documents/pycharmprojects/blogcodes/ Recursive. py the 1th time calculation, the first number is 0, the second number is 1 for the 2nd time, the first number is 1, the second number is 1 3rd time, the first number is 1, the second number is 2, the first number is 2, the second is 3 5th, the first number is 3, the second number is 5 6th, the first number is 5, the second number is 8 for the 7th time, the first number is 8, the second number is 13 for the 8th time, the first number is 13, the second number is 21, the first number is 21, the second is 34 10th, the first number is 34, the second is 55, the number is: 34Process finished with exit code 0