Concurrent Prime Generator

后端 未结 10 668
终归单人心
终归单人心 2020-12-20 20:32

I\'m going through the problems on projecteuler.net to learn how to program in Erlang, and I am having the hardest time creating a prime generator that can create all of the

相关标签:
10条回答
  • 2020-12-20 21:17

    Project Euler problems (I'd say most of the first 50 if not more) are mostly about brute force with a splash of ingenuity in choosing your bounds.

    Remember to test any if N is prime (by brute force), you only need to see if its divisible by any prime up to floor(sqrt(N)) + 1, not N/2.

    Good luck

    0 讨论(0)
  • 2020-12-20 21:19

    I love Project Euler.

    On the subject of prime generators, I am a big fan of the Sieve of Eratosthenes.

    For the purposes of the numbers under 2,000,000 you might try a simple isPrime check implementation. I don't know how you'd do it in erlang, but the logic is simple.

    For Each NUMBER in LIST_OF_PRIMES
         If TEST_VALUE % NUMBER == 0
              Then FALSE
    END
    TRUE
    
    if isPrime == TRUE add TEST_VALUE to your LIST_OF_PRIMES
    
    iterate starting at 14 or so with a preset list of your beginning primes. 
    

    c# ran a list like this for 2,000,000 in well under the 1 minute mark

    Edit: On a side note, the sieve of Eratosthenes can be implemented easily and runs quickly, but gets unwieldy when you start getting into huge lists. The simplest implementation, using a boolean array and int values runs extremely quickly. The trouble is that you begin running into limits for the size of your value as well as the length of your array. -- Switching to a string or bitarray implementation helps, but you still have the challenge of iterating through your list at large values.

    0 讨论(0)
  • 2020-12-20 21:25

    You can find four different Erlang implementations for finding prime numbers (two of which are based on the Sieve of Eratosthenes) here. This link also contains graphs comparing the performance of the 4 solutions.

    0 讨论(0)
  • 2020-12-20 21:27

    Primes parallel algorithm : http://www.cs.cmu.edu/~scandal/cacm/node8.html

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