Python分区问题超过最大递归深度

发布于 2025-02-08 21:01:26 字数 387 浏览 3 评论 0原文

我创建了一个递归函数,以计算在(启动,结束)范围内N范围内的N总数。该功能适用​​于较小的数字,除非它们开始变得更大。 start = 1,结束= 10 ** 12-1给我一个错误,说超过了最大递归深度。我如何修复我的代码以停止此错误:

def count(start, end, n, tot=0):
    if start > end:
        return tot
    else:
        if start % n == 0:
            tot += 1
    return count(start + 1, end, n, tot)


start = 1
end = 10**12 - 1
n = 5
print(count(start, end, n))

I have created a recursive function to calculate the total numbers divisible by n within a range of (start,end). The function works for smaller numbers except when they start getting larger for ex. start=1 and end=10**12 - 1 gives me an error saying that the maximum recursion depth has been exceeded. How do I fix my code to stop this error:

def count(start, end, n, tot=0):
    if start > end:
        return tot
    else:
        if start % n == 0:
            tot += 1
    return count(start + 1, end, n, tot)


start = 1
end = 10**12 - 1
n = 5
print(count(start, end, n))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望她远 2025-02-15 21:01:26

您可以通过简单的算术解决此问题。考虑(3,18)(包含)和n 5之类的范围,实际关注范围来自5 to 15(由于5 5 5 或以上 15 。您可以将rownend值绕到这样的新端点:

start = (start + 4) // n * n
end = end // n * n

您需要做的是只计算5的值15(3)您可以从新的结束中减去新的start,由n划分并添加1 IE

(end // n * n - (start + 4) // n * n) // n + 1

可以简化,从而

end // n - (start + 4) // n + 1

总共您的功能变为:

def count(start, end, n):
    return end // n - (start + 4) // n + 1

You can solve this problem with simple arithmetic. Thinking about a range such as (3, 18) (inclusive) and an n of 5, the actual range of interest is from 5 to 15 (since there are no multiples of 5 below 5 or above 15. You can round the start and end values to the new endpoints like this:

start = (start + 4) // n * n
end = end // n * n

What you need to do then is just count the values from 5 to 15 (3) which you can do subtracting the new start from the new end, dividing by n and adding 1 i.e.

(end // n * n - (start + 4) // n * n) // n + 1

which can be simplified to

end // n - (start + 4) // n + 1

So in total your function becomes:

def count(start, end, n):
    return end // n - (start + 4) // n + 1
妄断弥空 2025-02-15 21:01:26

两个分。

  1. (直接回答您的问题)您可以使用SYS模块来增加递归深度。
import sys
sys.setrecursionlimit(10**12)
  1. (要让您成为更好的程序员)您无需用于此问题的递归。实际上,正如其他人所指出的那样,这是一个非常糟糕的主意。像您要做的那样的递归是o(n^2),也称为“真的很糟糕”。线性方法将是O(n),这非常好。更好的是采用Samwise和Btilly建议的数学方法,因为您负责返回符合标准的数字。

Two points.

  1. (directly answering your question) You can increase the recursion depth by using the sys module.
import sys
sys.setrecursionlimit(10**12)
  1. (towards making you a better programmer) You don't need to use recursion for this problem. In fact, it's a really bad idea as others have noted. Recursion like what you would be looking to do is O(n^2) which is, also known as 'really bad'. A linear approach would be O(n) which is pretty good. Better yet is to take the mathematical approach as Samwise and btilly suggested because you arent tasked to return the numbers that meet the criteria.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文