简体   繁体   English

递归 function 错误:“堆栈溢出”

[英]recursive function error: “stack overflow”

I wrote this function that supposed to find smallest positive integer that is divisible by every number 1 through 20. I get "stack overflow error", even though, when I test for numbers 1 through 10, it works just fine.我写了这个 function 应该找到最小的正 integer 可以被每个数字 1 到 20 整除。我得到“堆栈溢出错误”,尽管当我测试数字 1 到 10 时,它工作得很好。 here is my code:这是我的代码:

#include<iostream>
#include<cstdlib>

using namespace std;

// function prototype
int smallPos(int k, int m,int n);

int main(){
    printf("the smallest positive number that is divisible by 1 through 20 is %d ", smallPos(1,1,13));

}

int smallPos(int k, int n, int m){
    int div=0;
    for(int i = n;i<=m;i++) {
        if (k%i==0) 
            div++;
    } 
    if (div==m) {
        return k;
    } else {
        k+=1;
        smallPos(k,n,m);
    }   
}

Why does it happen?为什么会这样? The number shouldn't be that big anyway.无论如何,这个数字不应该那么大。 Thank you!谢谢!

The final condition ( div == m ) will never be true.最终条件( div == m )永远不会为真。 For div to become equal to m , the number k should be divisible by all of the numbers in range [n,m) .为了使div等于m ,数字k应该可以被范围[n,m)中的所有数字整除。

Edit : I've reread the text in the printf() call to understand what the function does.编辑:我重读了printf()调用中的文本,以了解 function 的作用。 You're looking for the first number divisible by all numbers in the range.您正在寻找可被范围内所有数字整除的第一个数字。 If my calculations are correct, this number should be the product of all unique prime factors of the numbers in the range.如果我的计算是正确的,这个数字应该是该范围内数字的所有唯一素因子的乘积。 For the range [1,13] (as per the function call, not the text), this number should be:对于范围[1,13] (根据 function 调用,而不是文本),此数字应为:

30030 = 1 * 2 * 3 * 5 * 7 * 9 * 11 * 13

Now, this means you are going to invoke the function recursively over 30,000 times, which is obviously way too many times for the size of stack you're using (defaults are relatively small).现在,这意味着您将递归调用 function 超过 30,000 次,对于您正在使用的堆栈大小而言,这显然是太多次(默认值相对较小)。 For a range this size, you should really consider writing an iterative function instead.对于这个大小的范围,你真的应该考虑写一个迭代的 function。

Edit : here's an iterative version that seems to work.编辑:这是一个似乎可行的迭代版本。

int smallPos ( int n, int m )
{
    int k = 0;
    while ( ++k )
    {
        int count = 0;
        for (int i = n; i<=m; i++)
        {
            if (k%i==0) {
                ++count;
            }
        }
        if (count == (m-n+1)) {
            return k;
        }
    }
    return k;
}

Indeed, the result for smallPos(1,10) , the result is 2520. It seems my previous estimate was a lower bound, not a fixed result.实际上, smallPos(1,10)的结果是 2520。看来我之前的估计是一个下限,而不是一个固定的结果。

Your smallPos function incurs undefined behaviour since it is not returning a value in all execution paths.您的smallPos function 会导致未定义的行为,因为它没有在所有执行路径中返回值。 You may want to say return smallPos(k,n,m);你可能想说return smallPos(k,n,m); in the last part (or just return smallPos(k + 1, n, m); in one go).在最后一部分(或者直接return smallPos(k + 1, n, m);一次)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM