在Python中解决Kempner函数-为什么在达到基本情况后,此并发函数仍继续? [英] Solving the Kempner Function in Python - why is this concurrent function continuing after the base case is reached?

查看:63
本文介绍了在Python中解决Kempner函数-为什么在达到基本情况后,此并发函数仍继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做Python难题,而我一直在做的是使用并发函数来解决Python中的Kempner函数.

I've been doing Python puzzles and one I have been doing is using a concurrent function to solve the Kempner Function in Python.

应用于复合数的Kempner函数允许找到大于零的最小整数,阶乘将精确地除以该数.

The Kempner Function, applied to a composite number, permits to find the smallest integer greater than zero which factorial is exactly divided by the number.

kempner(6)➞3

1!= 1%6>0
2!= 2%6>0
3!= 6%6 === 0

肯纳(10)➞5

1!= 1%10>0
2!= 2%10>0
3!= 6%10>0
4!= 24%10>0
5!= 120%10 === 0

1! = 1 % 10 > 0
2! = 2 % 10 > 0
3! = 6 % 10 > 0
4! = 24 % 10 > 0
5! = 120 % 10 === 0

有多种方法可以做到这一点,而我所见的解决方案之一是:

There are various ways of doing this, and one of the solutions I have seen is this:

def kempner(n, i=1, total=1):
    if total % n == 0:
        return max(1, i-1)
    else:
        return kempner(n, i+1, total*i)

我了解这是做什么的要点,但是当我通过调试模式运行它并查看变量在做什么时,我可以看到达到基本条件时( if total%n == 0 )并返回 return max(1,i-1),那么 else 子句中的所有内容将继续运行,直到函数返回其起始状态(例如,对于 kempner(10),然后 n = 10 i = 1 total = 1 ).为什么这样做呢?如果已经达到基本条件,是否应该停止复发?

I understand the gist of what this is doing, however when I run it through debug mode and see what the variables are doing I can see that when the base condition is reached (if total % n ==0) and return max(1, i-1) is returned then everything in the else clause will continue to run until the function returns to its starting condition (e.g. for kempner(10) then n = 10, i = 1, total = 1). Why does it do that? Surely it should stop its recurrence if the base condition has been reached?

这是一个相当抽象的问题,显然是我所知的盲点.如果有人有任何见识,我将不胜感激.

This is a fairly abstract issue and is obviously a blind spot in my knowledge. If anyone has any insight I would be grateful.

推荐答案

递归调用与其他任何函数调用一样:当它们返回时,它们将控制权返回给调用它们的对象.

Recursive calls are just like any other function call: when they return, they return control back to whatever called them.

假设您有一系列编号的递归调用:

Say you have a series of numbered recursive calls:

1 -> 2 -> 3 -> 4
               Base Case Reached

如果递归调用3称为递归调用4,并且递归调用4在基本情况下结束,则从递归调用4返回将使您返回到递归调用3,因为3调用了4.这与任何其他函数调用一样:

If recursive call 3 called recursive call 4, and recursive call 4 ended at the base case, returning from recursive call 4 will take you back to recursive call 3, because 3 called 4. This is just like any other functions call:

def second_func():
    print("Inner")
    return 5

def first_func():
    return second_func()

second_func 返回时,由于 first_func 称为 second_func ,因此您将控制权返回到 first_func .您不会立即从 second_func 退出回到 main 或其他状态.递归调用也是如此.处理递归时的唯一区别是 first_func second_func 是相同的函数,但这并不影响返回的机制.

When you return from second_func, you return control back to first_func, since first_func called second_func. You don't immediately exit from second_func back to main or something else. It's the same with recursive calls. The only difference when dealing with recursion is first_func and second_func are the same function, but that doesn't affect the mechanics of returning.

除了使用异常之类的方法外,无法立即退出整个调用链.

There is no way (other than using something like exceptions) to exit from the entire call chain at once.

这篇关于在Python中解决Kempner函数-为什么在达到基本情况后,此并发函数仍继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆