Is there a simple algorithm that can determine if X is prime?

后端 未结 16 1490
离开以前
离开以前 2020-12-01 00:32

I have been trying to work my way through Project Euler, and have noticed a handful of problems ask for you to determine a prime number as part of it.

  1. I kno

相关标签:
16条回答
  • 2020-12-01 00:53

    I am working thru the Project Euler problems as well and in fact just finished #3 (by id) which is the search for the highest prime factor of a composite number (the number in the ? is 600851475143).

    I looked at all of the info on primes (the sieve techniques already mentioned here), and on integer factorization on wikipedia and came up with a brute force trial division algorithm that I decided would do.

    So as I am doing the euler problems to learn ruby I was looking into coding my algorithm and stumbled across the mathn library which has a Prime class and an Integer class with a prime_division method. how cool is that. i was able to get the correct answer to the problem with this ruby snippet:

    require "mathn.rb"
    puts 600851475143.prime_division.last.first
    

    this snippet outputs the correct answer to the console. of course i ended up doing a ton of reading and learning before i stumbled upon this little beauty, i just thought i would share it with everyone...

    0 讨论(0)
  • 2020-12-01 00:56

    I'd recommend Fermat's primality test. It is a probabilistic test, but it is correct surprisingly often. And it is incredibly fast when compared with the sieve.

    0 讨论(0)
  • 2020-12-01 00:57

    For Project Euler, having a list of primes is really essential. I would suggest maintaining a list that you use for each problem.

    I think what you're looking for is the Sieve of Eratosthenes.

    0 讨论(0)
  • 2020-12-01 00:57

    The AKS prime testing algorithm:

    Input: Integer n > 1  
    
    
    if (n is has the form ab with b > 1) then output COMPOSITE  
    
    r := 2  
    while (r < n) {  
        if (gcd(n,r) is not 1) then output COMPOSITE  
        if (r is prime greater than 2) then {  
            let q be the largest factor of r-1  
            if (q > 4sqrt(r)log n) and (n(r-1)/q is not 1 (mod r)) then break  
        }  
        r := r+1  
    }  
    
    for a = 1 to 2sqrt(r)log n {  
        if ( (x-a)n is not (xn-a) (mod xr-1,n) ) then output COMPOSITE  
    }  
    
    output PRIME;   
    
    0 讨论(0)
  • 2020-12-01 00:58

    Keeping in mind the following facts (from MathsChallenge.net):

    • All primes except 2 are odd.
    • All primes greater than 3 can be written in the form 6k - 1 or 6k + 1.
    • You don't need to check past the square root of n

    Here's the C++ function I use for relatively small n:

    bool isPrime(unsigned long n)
    {
        if (n == 1) return false; // 1 is not prime
        if (n < 4) return true; // 2 and 3 are both prime
        if ((n % 2) == 0) return false; // exclude even numbers
        if (n < 9) return true; //we have already excluded 4, 6, and 8.
        if ((n % 3) == 0) return false; // exclude remaining multiples of 3
    
        unsigned long r = floor( sqrt(n) );
        unsigned long f = 5;
        while (f <= r)
        {
            if ((n % f) == 0)  return false;
            if ((n % (f + 2)) == 0) return false;
            f = f + 6;
        }
        return true; // (in all other cases)
    }
    

    You could probably think of more optimizations of your own.

    0 讨论(0)
  • 2020-12-01 00:59

    another way in python is:

    import math
    
    def main():
        count = 1
        while True:
            isprime = True
    
            for x in range(2, int(math.sqrt(count) + 1)):
                if count % x == 0: 
                    isprime = False
                    break
    
            if isprime:
                print count
    
    
            count += 2
    
    
    if __name__ == '__main__':
        main()  
    
    0 讨论(0)
提交回复
热议问题