Python full stack road series recursion

高洛峰
Release: 2017-02-09 10:44:16
Original
1196 people have browsed it

The so-called recursion actually means that the function itself calls the function until the specified conditions are met and then exits the function layer by layer. For example,

Once upon a time, there was a mountain, and there was a temple in the mountain. There was an old monk in the temple, who was giving the child The monk is telling stories! What's the story? "Once upon a time, there was a mountain, and there was a temple in the mountain. There was an old monk in the temple, and he was telling a story to the young monk! What is the story? 'Once upon a time, there was a mountain, and there was a temple in the mountain. There was an old monk in the temple, and he was telling a story to the young monk. Tell a story to the little monk! What is the story?...'"

  • ##Use functions to write a Fibonacci sequence

##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 to add the two numbers in front to get the next number, and then proceed in sequence

The code is as follows
#!/usr/bin/env python
# _*_ coding: utf-8 _*_

def Counter(n1, n2):
    if n1 > 10000:  # 当要计算的值大于10000就退出
        return
    print("Counter:", n1)  # 输出当前计算到那个值了
    n3 = n1 + n2  # 第一个值加上第一个值等于第三个值
    Counter(n2, n3)  # 调用计数器函数,此时第一个值是调用函数传过来的最后一个值,而第二个值是计算出来的第三个值


Counter(0, 1)  # 调用计数器函数
Copy after login
Copy after login

Output results

/usr/bin/python3.5 /home/ansheng/Documents/PycharmProjects/blogcodes/斐波那契.py
Counter: 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
Copy after login
Copy after login

    Use recursion to obtain the 10th number in the Fibonacci sequence and return the value to the caller
  • Code:
#!/usr/bin/env python
# _*_ coding: utf-8 _*_

def Counter(Index, Start, End):
    print("第%d次计算,第一个数字是%d,第二个数字是%d" % (Index, Start, End))
    if Index == 10:  # 如果要计算的值是10就退出
        return Start
    N = Start + End  # N等于第一个数加上第二个数
    Number = Counter(Index + 1, End, N)  # 继续调用计数器函数,End相当与传给函数的第一个数,N是传给函数的第二个数
    return Number


result = Counter(1, 0, 1)
print("得出的数字是:", result)
Copy after login
Copy after login

Output result

/usr/bin/python3.5 /home/ansheng/Documents/PycharmProjects/blogcodes/递归.py
第1次计算,第一个数字是0,第二个数字是1
第2次计算,第一个数字是1,第二个数字是1
第3次计算,第一个数字是1,第二个数字是2
第4次计算,第一个数字是2,第二个数字是3
第5次计算,第一个数字是3,第二个数字是5
第6次计算,第一个数字是5,第二个数字是8
第7次计算,第一个数字是8,第二个数字是13
第8次计算,第一个数字是13,第二个数字是21
第9次计算,第一个数字是21,第二个数字是34
第10次计算,第一个数字是34,第二个数字是55
得出的数字是: 34

Process finished with exit code 0
Copy after login
Copy after login

Original link


The so-called recursion is actually the function itself calling the function until Exit functions layer by layer after meeting the specified conditions. For example,

Once upon a time there was a mountain, a temple in the mountain, and an old monk in the temple who was telling stories to the young monk! What's the story? "Once upon a time, there was a mountain, and there was a temple in the mountain. There was an old monk in the temple, and he was telling a story to the young monk! What is the story? 'Once upon a time, there was a mountain, and there was a temple in the mountain. There was an old monk in the temple, and he was telling a story to the young monk. Tell a story to the little monk! What is the story?...'"

##Use functions to write a Fibonacci sequence
  • ##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 to add the two numbers in front to get the next number, and then proceed in sequence

The code is as follows

#!/usr/bin/env python
# _*_ coding: utf-8 _*_

def Counter(n1, n2):
    if n1 > 10000:  # 当要计算的值大于10000就退出
        return
    print("Counter:", n1)  # 输出当前计算到那个值了
    n3 = n1 + n2  # 第一个值加上第一个值等于第三个值
    Counter(n2, n3)  # 调用计数器函数,此时第一个值是调用函数传过来的最后一个值,而第二个值是计算出来的第三个值


Counter(0, 1)  # 调用计数器函数
Copy after login
Copy after login
Output results
/usr/bin/python3.5 /home/ansheng/Documents/PycharmProjects/blogcodes/斐波那契.py
Counter: 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
Copy after login
Copy after login

Use recursion to obtain the 10th number in the Fibonacci sequence and return the value to the caller

  • Code:

    #!/usr/bin/env python
    # _*_ coding: utf-8 _*_
    
    def Counter(Index, Start, End):
        print("第%d次计算,第一个数字是%d,第二个数字是%d" % (Index, Start, End))
        if Index == 10:  # 如果要计算的值是10就退出
            return Start
        N = Start + End  # N等于第一个数加上第二个数
        Number = Counter(Index + 1, End, N)  # 继续调用计数器函数,End相当与传给函数的第一个数,N是传给函数的第二个数
        return Number
    
    
    result = Counter(1, 0, 1)
    print("得出的数字是:", result)
    Copy after login
    Copy after login
  • Output result
/usr/bin/python3.5 /home/ansheng/Documents/PycharmProjects/blogcodes/递归.py
第1次计算,第一个数字是0,第二个数字是1
第2次计算,第一个数字是1,第二个数字是1
第3次计算,第一个数字是1,第二个数字是2
第4次计算,第一个数字是2,第二个数字是3
第5次计算,第一个数字是3,第二个数字是5
第6次计算,第一个数字是5,第二个数字是8
第7次计算,第一个数字是8,第二个数字是13
第8次计算,第一个数字是13,第二个数字是21
第9次计算,第一个数字是21,第二个数字是34
第10次计算,第一个数字是34,第二个数字是55
得出的数字是: 34

Process finished with exit code 0
Copy after login
Copy after login

For more Python full-stack road series recursion related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!