<div class="gmail_quote"><div>Mark,<br><br>You can use lists:append(List, [Term]) to add larger primes to the back of the list, but I'd suggest approaching your problem in a different way: N is prime if the GCD of ((N-1)!, N) is 1.  Write a GCD function and use an accumulator to store the FactorialSoFar.<br>
<br>If you need any hep, let me know.<br><br>Steven<br><br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Message: 2<br>
Date: Thu, 5 Feb 2009 18:50:22 -0800<br>
From: Mark Wagner <<a href="mailto:carnildo@gmail.com">carnildo@gmail.com</a>><br>
Subject: [erlang-questions] beginner: Generating a list of prime<br>
        numbers<br>
To: <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
Message-ID:<br>
        <<a href="mailto:31073ef90902051850h2377c242s9f25d39ff47c6166@mail.gmail.com">31073ef90902051850h2377c242s9f25d39ff47c6166@mail.gmail.com</a>><br>
Content-Type: text/plain; charset=UTF-8<br>
<br>
As a way of learning the basics of Erlang, I'm solving various<br>
problems from Project Euler (<a href="http://projecteuler.net/" target="_blank">http://projecteuler.net/</a>).  Problem #10<br>
involves generating a list of prime numbers.  The naive solution to<br>
this is to generate a list of numbers and test each of them<br>
individually to see if it's prime; a more sophisticated solution uses<br>
the list of primes already found to speed up the testing.  My code for<br>
the sophisticated solution is:<br>
<br>
is_prime(PrimesSoFar, Candidate) -> not lists:any(fun(X) -> Candidate<br>
rem X =:= 0 end, PrimesSoFar).<br>
<br>
list_primes(PrimesSoFar, Max, Candidate) when (Candidate > Max) -> PrimesSoFar;<br>
list_primes(PrimesSoFar, Max, Candidate) -><br>
        case is_prime(PrimesSoFar, Candidate) of<br>
                true -> list_primes([Candidate|PrimesSoFar], Max, Candidate + 2);<br>
                false -> list_primes(PrimesSoFar, Max, Candidate + 2)<br>
        end.<br>
<br>
list_primes(N) when N < 2 -> [];<br>
list_primes(2) -> [2];<br>
list_primes(N) -> list_primes([2], N, 3).<br>
<br>
The problem with this is that the list of prime numbers is ordered<br>
largest-first, so that the small primes (the ones most likely to be<br>
factors of a given number) are tested last.  This makes the<br>
sophisticated solution slower than the naive one, which tests small<br>
factors first.  Any suggestions on how to fix this?<br>
<br>
--<br>
Mark Wagner<br></blockquote></div><br>