On Fri, 12 Jan 2007 16:39:44 -0800, Timothy Clemans <[EMAIL PROTECTED]> wrote:
> Stein, > > I wrote a SAGE function that lists the first n primes. > http://sage.math.washington.edu:8100/nprimes > > def listnprimes(n): > """ > Returns list of the first n prime numbers. > > EXAMPLES: > sage: print listnprimes(5) > [2,3,5,7,11] > sage: print listnprimes(10) > [2,3,5,7,11,13,17,19,23,29] > Written by Timothy Clemans > """ > Z = sage.rings.integer.Integer > n = Z(n) > if n < 1: return > i = 2 > prime = [] > for h in range(n): > prime.append(i) > i = next_prime(i) > return prime > > Any comments would be nice. I would like this to be included in SAGE. This will be much slower than other methods, since next_prime is quite expensive (to put it mildly) compared to using a sieve method. Better would be a function that (1) Use the prime number theorem (or improvements -- see my notes with Mazur) to estimate an integer B such that prime_range(B) has length very close to n, and use an estimate that tends to overestimate. (2) Compute v = prime_range(B) (which is a very fast operation, even for B pretty large). (3) if len(v) >= n return v[:n]. (4) If len(v) < n, use a loop like you wrote above, i.e., use next_prime a few times. William --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/ -~----------~----~----~----~------~----~------~--~---
