当我输入10 5000000之类的大数字时,为什么这个程序会崩溃?

发布于 2025-02-11 02:58:00 字数 678 浏览 1 评论 0原文

#include <iostream>

using namespace std;
bool is_prime(int num , int cnt = -1){
    if(cnt == -1)
        cnt = num-1;

    if(num <= 1)
        return false;

    if(cnt == 1)
        return true;

    if(num % cnt == 0)
        return false;

    return is_prime(num , cnt-1);

}

int count_primes(int start , int end){
    if(start>end)
        return 0;
    if(is_prime(start))
        return 1 + count_primes(start+1 , end);
    else
    return 0 + count_primes(start+1 , end);
}
int main()
{
    cout<<count_primes(10 , 200);

    return 0;
}

当我传递到功能时,为什么这个程序会崩溃 count_primes的参数像10和5000000。

当我通过10和50这样的参数时,它效果很好。

#include <iostream>

using namespace std;
bool is_prime(int num , int cnt = -1){
    if(cnt == -1)
        cnt = num-1;

    if(num <= 1)
        return false;

    if(cnt == 1)
        return true;

    if(num % cnt == 0)
        return false;

    return is_prime(num , cnt-1);

}

int count_primes(int start , int end){
    if(start>end)
        return 0;
    if(is_prime(start))
        return 1 + count_primes(start+1 , end);
    else
    return 0 + count_primes(start+1 , end);
}
int main()
{
    cout<<count_primes(10 , 200);

    return 0;
}

why does this program crash when I pass to the function
count_primes arguments like 10 and 5000000.

when I pass arguments like 10 and 50 it works well.

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

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

发布评论

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

评论(1

末蓝 2025-02-18 02:58:00

很高兴您尝试以功能性的方式解决它。您可以在两个点上进行改进:

  • 使其递归递归:在返回呼叫之前,请勿对递归电话做任何事情,即,count_primes()中的所有返回语句都应该看起来像<代码>返回count_primes(...); 。为了实现这一目标,您可能需要引入其他累加器参数,在这种情况下,只有acc
  • 优化一点:请注意,is_prime()返回布尔值,因此您可以将其添加到累加器中,而无需使用IF。 SET numis_prime() to sqrt(num)中的初始值。另外,使用-O3和新标准(可能是C ++ 20)编译。

我的计算机上适用于10、5000000的代码看起来像:

#include <iostream>
#include <math.h>

using namespace std;
bool is_prime(int num , int cnt = -1){
    if(cnt == -1)
        cnt = sqrt(num);

    if(num <= 1)
        return false;

    if(cnt == 1)
        return true;

    if(num % cnt == 0)
        return false;

    return is_prime(num , cnt-1);
}
 
int count_primes(int start , int end, int acc = 0){
    if(start>end)
        return acc;
    return count_primes(start+1 , end, acc + is_prime(start));
}

int main()
{
    cout<<count_primes(10 , 5000000);
 
    return 0;
}

It is nice that you try to solve it in a functional manner. There are two points where you can improve it:

  • make it tail recursive: do not do anything to the result of the recursing call before returning it, i.e., all return statements in count_primes() should look like return count_primes(...);. To achieve this, you might need to introduce additional accumulator parameter(s), in this case just an acc.
  • optimize a bit: note that is_prime() returns a boolean, so you can add it to the accumulator, no need for if-statement. Set num's initial value in is_prime() to sqrt(num). Also, compile with -O3 and newer standards (C++20 possibly).

The code that works for 10, 5000000 on my machine looks like:

#include <iostream>
#include <math.h>

using namespace std;
bool is_prime(int num , int cnt = -1){
    if(cnt == -1)
        cnt = sqrt(num);

    if(num <= 1)
        return false;

    if(cnt == 1)
        return true;

    if(num % cnt == 0)
        return false;

    return is_prime(num , cnt-1);
}
 
int count_primes(int start , int end, int acc = 0){
    if(start>end)
        return acc;
    return count_primes(start+1 , end, acc + is_prime(start));
}

int main()
{
    cout<<count_primes(10 , 5000000);
 
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文