Which is the fastest algorithm to find prime numbers?

后端 未结 14 1439
情深已故
情深已故 2020-11-22 06:49

Which is the fastest algorithm to find out prime numbers using C++? I have used sieve\'s algorithm but I still want it to be faster!

相关标签:
14条回答
  • 2020-11-22 07:03

    i wrote it today in C,compiled with tcc, figured out during preparation of compititive exams several years back. don't know if anyone already have wrote it alredy. it really fast(but you should decide whether it is fast or not). took one or two minuts to findout about 1,00,004 prime numbers between 10 and 1,00,00,000 on i7 processor with average 32% CPU use. as you know, only those can be prime which have last digit either 1,3,7 or 9 and to check if that number is prime or not, you have to divide that number by previously found prime numbers only. so first take group of four number = {1,3,7,9}, test it by dividing by known prime numbers, if reminder is non zero then number is prime, add it to prime number array. then add 10 to group so it becomes {11,13,17,19} and repeat the process.

    #include <stdio.h>
    int main() {    
        int nums[4]={1,3,7,9};
        int primes[100000];
        primes[0]=2;
        primes[1]=3;
        primes[2]=5;
        primes[3]=7;
        int found = 4;
        int got = 1;
        int m=0;
        int upto = 1000000;
        for(int i=0;i<upto;i++){
            //printf("iteration number: %d\n",i);
            for(int j=0;j<4;j++){
                m = nums[j]+10;
                //printf("m = %d\n",m);
                nums[j] = m;
                got = 1;
                for(int k=0;k<found;k++){
                    //printf("testing with %d\n",primes[k]);
                    if(m%primes[k]==0){
                        got = 0;
                        //printf("%d failed for %d\n",m,primes[k]);
                        break;
                    }
                }
                if(got==1){
                    //printf("got new prime: %d\n",m);
                    primes[found]= m;
                    found++;
                }
            }
        }
        printf("found total %d prime numbers between 1 and %d",found,upto*10);
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 07:08

    If it has to be really fast you can include a list of primes:
    http://www.bigprimes.net/archive/prime/

    If you just have to know if a certain number is a prime number, there are various prime tests listed on wikipedia. They are probably the fastest method to determine if large numbers are primes, especially because they can tell you if a number is not a prime.

    0 讨论(0)
  • 2020-11-22 07:10
    #include<stdio.h>
    main()
    {
        long long unsigned x,y,b,z,e,r,c;
        scanf("%llu",&x);
        if(x<2)return 0;
        scanf("%llu",&y);
        if(y<x)return 0;
        if(x==2)printf("|2");
        if(x%2==0)x+=1;
        if(y%2==0)y-=1;
        for(b=x;b<=y;b+=2)
        {
            z=b;e=0;
            for(c=2;c*c<=z;c++)
            {
                if(z%c==0)e++;
                if(e>0)z=3;
            }
            if(e==0)
            {
                printf("|%llu",z);
                r+=1;
            }
        }
        printf("|\n%llu outputs...\n",r);
        scanf("%llu",&r);
    }    
    
    0 讨论(0)
  • 2020-11-22 07:12

    It depends on your application. There are some considerations:

    • Do you need just the information whether a few numbers are prime, do you need all prime numbers up to a certain limit, or do you need (potentially) all prime numbers?
    • How big are the numbers you have to deal with?

    The Miller-Rabin and analogue tests are only faster than a sieve for numbers over a certain size (somewhere around a few million, I believe). Below that, using a trial division (if you just have a few numbers) or a sieve is faster.

    0 讨论(0)
  • 2020-11-22 07:12
    #include <iostream>
    
    using namespace std;
    
    int set [1000000];
    
    int main (){
    
        for (int i=0; i<1000000; i++){
            set [i] = 0;
        }
        int set_size= 1000;
        set [set_size];
        set [0] = 2;
        set [1] = 3;
        int Ps = 0;
        int last = 2;
    
        cout << 2 << " " << 3 << " ";
    
        for (int n=1; n<10000; n++){
            int t = 0;
            Ps = (n%2)+1+(3*n);
            for (int i=0; i==i; i++){
                if (set [i] == 0) break;
                if (Ps%set[i]==0){
                    t=1;
                    break;
                }
            }
            if (t==0){
                cout << Ps << " ";
                set [last] = Ps;
                last++;
            }
        }
        //cout << last << endl;
    
    
        cout << endl;
    
        system ("pause");
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 07:13

    He, he I know I'm a question necromancer replying to old questions, but I've just found this question searching the net for ways to implement efficient prime numbers tests.

    Until now, I believe that the fastest prime number testing algorithm is Strong Probable Prime (SPRP). I am quoting from Nvidia CUDA forums:

    One of the more practical niche problems in number theory has to do with identification of prime numbers. Given N, how can you efficiently determine if it is prime or not? This is not just a thoeretical problem, it may be a real one needed in code, perhaps when you need to dynamically find a prime hash table size within certain ranges. If N is something on the order of 2^30, do you really want to do 30000 division tests to search for any factors? Obviously not.

    The common practical solution to this problem is a simple test called an Euler probable prime test, and a more powerful generalization called a Strong Probable Prime (SPRP). This is a test that for an integer N can probabilistically classify it as prime or not, and repeated tests can increase the correctness probability. The slow part of the test itself mostly involves computing a value similar to A^(N-1) modulo N. Anyone implementing RSA public-key encryption variants has used this algorithm. It's useful both for huge integers (like 512 bits) as well as normal 32 or 64 bit ints.

    The test can be changed from a probabilistic rejection into a definitive proof of primality by precomputing certain test input parameters which are known to always succeed for ranges of N. Unfortunately the discovery of these "best known tests" is effectively a search of a huge (in fact infinite) domain. In 1980, a first list of useful tests was created by Carl Pomerance (famous for being the one to factor RSA-129 with his Quadratic Seive algorithm.) Later Jaeschke improved the results significantly in 1993. In 2004, Zhang and Tang improved the theory and limits of the search domain. Greathouse and Livingstone have released the most modern results until now on the web, at http://math.crg4.com/primes.html, the best results of a huge search domain.

    See here for more info: http://primes.utm.edu/prove/prove2_3.html and http://forums.nvidia.com/index.php?showtopic=70483

    If you just need a way to generate very big prime numbers and don't care to generate all prime numbers < an integer n, you can use Lucas-Lehmer test to verify Mersenne prime numbers. A Mersenne prime number is in the form of 2^p -1. I think that Lucas-Lehmer test is the fastest algorithm discovered for Mersenne prime numbers.

    And if you not only want to use the fastest algorithm but also the fastest hardware, try to implement it using Nvidia CUDA, write a kernel for CUDA and run it on GPU.

    You can even earn some money if you discover large enough prime numbers, EFF is giving prizes from $50K to $250K: https://www.eff.org/awards/coop

    0 讨论(0)
提交回复
热议问题