File tree Expand file tree Collapse file tree 5 files changed +76
-94
lines changed
source-code/cython/Primes Expand file tree Collapse file tree 5 files changed +76
-94
lines changed Original file line number Diff line number Diff line change 1- def primes (int kmax ):
2- cdef int n, k, i
3- cdef int p[1000 ]
4- result = []
5- if kmax > 1000 :
6- kmax = 1000
7- k = 0
8- n = 2
9- while k < kmax:
10- i = 0
11- while i < k and n % p[i] != 0 :
12- i = i + 1
13- if i == k:
14- p[k] = n
15- k = k + 1
16- result.append(n)
17- n = n + 1
18- return result
1+ def primes (int nr_primes ):
2+ cdef int primes[1000 ]
3+ if nr_primes > 1000 :
4+ nr_primes = 1000
5+ cdef int n = 2
6+ cdef int nr_found = 0
7+ while nr_found < nr_primes:
8+ for prime in primes[:nr_found]:
9+ if n % prime == 0 :
10+ break
11+ else :
12+ primes[nr_found] = n
13+ nr_found += 1
14+ n += 1
15+ return [prime for prime in primes[:nr_found]]
Original file line number Diff line number Diff line change 11from libc.stdlib cimport malloc, free
22
33
4- def primes (int kmax ):
5- cdef int n, k, i
6- cdef int * p = < int * > malloc(kmax* sizeof(int ))
7- result = []
8- if kmax > 1000 :
9- kmax = 1000
10- k = 0
11- n = 2
12- while k < kmax:
13- i = 0
14- while i < k and n % p[i] != 0 :
15- i = i + 1
16- if i == k:
17- p[k] = n
18- k = k + 1
19- result.append(n)
20- n = n + 1
21- free(p)
4+ def primes (int nr_primes ):
5+ cdef int nr_bytes = nr_primes* sizeof(int )
6+ cdef int * primes = < int * > malloc(nr_bytes)
7+ if nr_primes > 1000 :
8+ nr_primes = 1000
9+ cdef int n = 2
10+ cdef int nr_found = 0
11+ while nr_found < nr_primes:
12+ for prime in primes[:nr_found]:
13+ if n % prime == 0 :
14+ break
15+ else :
16+ primes[nr_found] = n
17+ nr_found += 1
18+ n += 1
19+ result = [prime for prime in primes[:nr_found]]
20+ free(primes)
2221 return result
Original file line number Diff line number Diff line change 11import cython
22from cython .cimports .libc .stdlib import malloc , free
33
4- def primes (nb_primes : cython .int ):
5- i : cython .int
6- p : cython .p_int = cython .cast (cython . p_int , malloc ( nb_primes * cython . sizeof ( cython . int )))
7-
8-
9- len_p : cython . int = 0 # The current number of elements in p.
4+ def primes (nr_primes : cython .int ):
5+ nr_bytes : cython .int = nr_primes * cython . sizeof ( cython . int )
6+ primes : cython .p_int = cython .cast (
7+ cython . p_int ,
8+ malloc ( nr_bytes )
9+ )
1010 n : cython .int = 2
11- while len_p < nb_primes :
12- # Is n prime?
13- for i in p [: len_p ]:
14- if n % i == 0 :
11+ nr_found : cython . int = 0
12+ while nr_found < nr_primes :
13+ for prime in primes [: nr_found ]:
14+ if n % prime == 0 :
1515 break
16-
17- # If no break occurred in the loop, we have a prime.
1816 else :
19- p [ len_p ] = n
20- len_p += 1
17+ primes [ nr_found ] = n
18+ nr_found += 1
2119 n += 1
22-
23- # Let's copy the result into a Python list:
24- result_as_list = [prime for prime in p [:len_p ]]
25- free (p )
26- return result_as_list
20+ result = [prime for prime in primes [:nr_found ]]
21+ free (primes )
22+ return result
Original file line number Diff line number Diff line change 11import cython
2+ import sys
23
3- def primes (nb_primes : cython .int ):
4- i : cython .int
5- p : cython .int [1000 ]
6-
7- if nb_primes > 1000 :
8- nb_primes = 1000
9-
10- if not cython .compiled : # Only if regular Python is running
11- p = [0 ] * 1000 # Make p work almost like a C array
12-
13- len_p : cython .int = 0 # The current number of elements in p.
4+ def primes (nr_primes : cython .int ):
5+ primes : cython .int [1000 ]
6+ if nr_primes > 1000 :
7+ nr_primes = 1000
8+ if not cython .compiled :
9+ primes = [0 ] * 1000
10+ print ('fall back on Python' , file = sys .stderr )
1411 n : cython .int = 2
15- while len_p < nb_primes :
16- # Is n prime?
17- for i in p [: len_p ]:
18- if n % i == 0 :
12+ nr_found : cython . int = 0
13+ while nr_found < nr_primes :
14+ for prime in primes [: nr_found ]:
15+ if n % prime == 0 :
1916 break
20-
21- # If no break occurred in the loop, we have a prime.
2217 else :
23- p [ len_p ] = n
24- len_p += 1
18+ primes [ nr_found ] = n
19+ nr_found += 1
2520 n += 1
26-
27- # Let's copy the result into a Python list:
28- result_as_list = [prime for prime in p [:len_p ]]
29- return result_as_list
21+ return [prime for prime in primes [:nr_found ]]
Original file line number Diff line number Diff line change 1- def primes (kmax ):
2- p = [0 ]* 1000
3- result = []
4- if kmax > 1000 :
5- kmax = 1000
6- k = 0
1+ def primes (nr_primes ):
2+ primes = [0 ]* 1000
3+ if nr_primes > 1000 :
4+ nr_primes = 1000
75 n = 2
8- while k < kmax :
9- i = 0
10- while i < k and n % p [ i ] != 0 :
11- i = i + 1
12- if i == k :
13- p [ k ] = n
14- k = k + 1
15- result . append ( n )
16- n = n + 1
17- return result
6+ nr_found = 0
7+ while nr_found < nr_primes :
8+ for prime in primes [: nr_found ] :
9+ if n % prime == 0 :
10+ break
11+ else :
12+ primes [ nr_found ] = n
13+ nr_found += 1
14+ n += 1
15+ return primes [: nr_found ]
You can’t perform that action at this time.
0 commit comments