相关疑难解决方法(0)

Multithreaded Segmented Sieve of Eratosthenes in Java

I am trying to create a fast prime generator in Java. It is (more or less) accepted that the fastest way for this is the segmented sieve of Eratosthenes: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. Lots of optimizations can be further implemented to make it faster. As of now, my implementation generates 50847534 primes below 10^9 in about 1.6 seconds, but I am looking to make it faster and at least break the 1 second barrier. To increase the chance of getting good …

java arrays primes multithreading sieve-of-eratosthenes

7
推荐指数
1
解决办法
223
查看次数

埃拉托色尼的 c++ 筛我的代码很慢

我试图找到低于 4 亿的质数,但即使只有 4000 万,我的代码也需要 8 秒才能运行。我究竟做错了什么?

我该怎么做才能让它更快?

#include<iostream>
#include<math.h>
#include<vector>
using namespace std;
int main()
{
    vector<bool> k;                         
    vector<long long int> c;                
    for (int i=2;i<40000000;i++)
    {
        k.push_back(true);                  
        c.push_back(i);
    }

    for ( int i=0;i<sqrt(40000000)+1;i++)                            
    {                                                               
        if (k[i]==true)                                              
       {                                                            
           for (int j=i+c[i];j<40000000;j=j+c[i])                  
           {                                                       
               k[j]=false; 
           }
       }
    }
    vector <long long int> arr;
    for ( int i=0;i<40000000-2;i++)
    {
        if (k[i]==true)
        {
            arr.push_back(c[i]);
        }
    }
    cout << arr.size() << endl ;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ time-complexity sieve-of-eratosthenes

4
推荐指数
1
解决办法
376
查看次数

找到给定素数后的n个素数,而不使用任何检查素数的函数

如何编写一个程序来查找给定数字后的n个素数?例如,在100之后的前10个素数,或在1000之后的前25个素数.编辑:下面是我尝试的.我正在以这种方式获得输出,但是我们可以在不使用任何素性测试函数的情况下进行输出吗?

#include<stdio.h>
#include<conio.h>
int isprime(int);
main()
{
    int count=0,i;
    for(i=100;1<2;i++)
    {
        if(isprime(i))
        {
            printf("%d\n",i);
            count++;
            if(count==5)
                break;
        }
    }
    getch();
}
int isprime(int i)
{
    int c=0,n;
    for(n=1;n<=i/2;n++)
    {
        if(i%n==0)
        c++;
    }
    if(c==1)
        return 1;
    else
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

c primes sieve-of-eratosthenes

0
推荐指数
2
解决办法
4050
查看次数