<br><div class="gmail_quote">On Fri, Jul 17, 2009 at 11:02 PM, Jack Diederich <span dir="ltr"><<a href="mailto:jackdied@gmail.com">jackdied@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im"><br>
</div>If you use an off-the-shelf prime number generator fucntion[1] that<br>
returns consecutive primes the method collapses into a simple<br>
function.<br>
<br>
def nPrime(n, primes=[], next_prime=eratosthenes().next):<br>
    while len(primes) < n:<br>
        primes.append(next_prime())<br>
    return primes[n]<br>
<br>
-Jack<br>
<br>
[1] <a href="http://www.catonmat.net/blog/solving-google-treasure-hunt-prime-number-problem-four/#comment-3075" target="_blank">http://www.catonmat.net/blog/solving-google-treasure-hunt-prime-number-problem-four/#comment-3075</a><br>

     There is a deque implementation that is faster still but I can't<br>
find a link.</blockquote><div><br>Jack, thanks for the link, I'll definitely look into it.<br><br>The function is currently implemented in a class I wrote myself, which is a generator class that does exactly the same thing in the next() function. I didn't post the code here because it wasn't in the scope of this thread, but I'm sure others have done a lot better and much more efficient algorithms.<br>
<br>The nPrime method uses the nature of the iteration and checks if a value already exists (ie generated before) and return that value; otherwise it iterates again. This way, I don't have to iterate from n=2 and on every time, which makes generation time linear.<br>
<br>However, I just gave the link's code a quick try. It was at least 10x times faster than my code to generate 100000 prime numbers - I'll have a closer look.<br><br>Thanks a great deal.<br><br>Ching-Yun "Xavier" Ho, Technical Artist<br>
<br>Contact Information<br>Mobile: (+61) 04 3335 4748<br>Skype ID: SpaXe85<br>Email: <a href="mailto:contact@xavierho.com">contact@xavierho.com</a><br>Website: <a href="http://xavierho.com/">http://xavierho.com/</a><br> 
<br></div></div><br>