ANN: Pyrex - a language for writing Python extension modules
David Eppstein
eppstein at ics.uci.edu
Thu Apr 4 20:19:05 EST 2002
In article <mailman.1017952983.647.clpa-moderators at python.org>,
Greg Ewing <greg at cosc.canterbury.ac.nz> wrote:
> Oh, yeah, here's the example. It finds prime
> numbers at C speed.
>
> def primes(int kmax):
> cdef int n, k, i
> cdef int p[1000]
> result = []
> if kmax > 1000:
> kmax = 1000
> k = 0
> n = 2
> while k < kmax:
> i = 0
> while i < k and n % p[i] <> 0:
> i = i + 1
> if i == k:
> p[k] = n
> k = k + 1
> result.append(n)
> n = n + 1
> return result
Not to criticise your project, but this is not a good example to use --
you've started with a very unnecessarily slow algorithm. For finding
prime numbers, the Sieve of Eratosthenes is much better. See e.g.
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117119> which I
implemented using simple generators so you don't need the kmax parameter.
--
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/
More information about the Python-list
mailing list