primes.pyx 583 Bytes
Newer Older
1 2
def primes(int nb_primes):
    cdef int n, i, len_p
3
    cdef int p[1000]
4 5 6
    if nb_primes > 1000:
        nb_primes = 1000

7
    len_p = 0  # The current number of elements in p.
8
    n = 2
9 10 11 12 13 14
    while len_p < nb_primes:
        # Is n prime?
        for i in p[:len_p]:
            if n % i == 0:
                break

15
        # If no break occurred in the loop, we have a prime.
16 17 18 19
        else:
            p[len_p] = n
            len_p += 1
        n += 1
20

21
    # Let's return the result in a python list:
22 23
    result_as_list  = [prime for prime in p[:len_p]]
    return result_as_list