Concurrent Prime Generator

后端 未结 10 704
终归单人心
终归单人心 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: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.

提交回复
热议问题