File: primes.pyx

package info (click to toggle)
cython-legacy 0.29.37-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,328 kB
  • sloc: python: 67,540; ansic: 14,965; cpp: 1,340; xml: 1,031; makefile: 364; lisp: 206; sh: 159; sed: 11
file content (23 lines) | stat: -rw-r--r-- 583 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def primes(int nb_primes):
    cdef int n, i, len_p
    cdef int p[1000]
    if nb_primes > 1000:
        nb_primes = 1000

    len_p = 0  # The current number of elements in p.
    n = 2
    while len_p < nb_primes:
        # Is n prime?
        for i in p[:len_p]:
            if n % i == 0:
                break

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

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