Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
非常大素数C++的生成_C++_Loops_Primes - Fatal编程技术网

非常大素数C++的生成

非常大素数C++的生成,c++,loops,primes,C++,Loops,Primes,下面是我编写的简单代码,我知道我可以对其进行优化,但我做了一些测试,发现了一个问题: #include <iostream> using namespace std; int main() { int n; int d=3; cout<<"insert a number n: "; cin>>n; while (d<n){ if ((n%d)!=0) { d=d+2;

下面是我编写的简单代码,我知道我可以对其进行优化,但我做了一些测试,发现了一个问题:

#include <iostream>
using namespace std;
int main() {
    int n;
    int d=3;
    cout<<"insert a number n: ";
    cin>>n;
    while (d<n){
        if ((n%d)!=0) {
            d=d+2;
        }
        else
        n=n+1;
    }
    cout<<"the number: "<<n<<" is prime"<<endl;
    system ("PAUSE");
    return 0;
}
基本上,对于足够小的数字(即小于“2147483647”的数字)来说,这是输出中发生的情况的屏幕截图:

我需要生成一个大概有1000位或者更多的素数,但我们在500-1000的范围内
C++能处理吗?另外,我需要程序在我的代码中为我循环它,对如何解决这个问题有什么建议吗?

你应该使用“long”或“long-long”,你也可以使用“unsigned”来表示更大的范围,因为int也使用负数,你不需要得到素数。

你需要一个大的int库,就像这样的大数是不可能的保存在int变量中。你可能想了解一下极限。你有没有做过关于生成大素数的研究?通过快速的谷歌搜索,你可能想在我们的姐妹网站上阅读加密技术。一个常见的技巧是使用模块化算法来完全避免庞大数字的问题。看看你的作业规范是否允许这种或类似的选择。看看:你目前的方法,即使是1000位数字,也需要几年才能给出答案。也许你可以调整链接测试来使用大量的数字。Asker需要大约1000位数字。就我所知,在任何一台计算机上,long都远远不够长。