diff --git a/docs/examples/tutorial/cython_tutorial/primes.py b/docs/examples/tutorial/cython_tutorial/primes.py deleted file mode 100644 index 5e0d32e69320b0c72873421bad89f30855927f4f..0000000000000000000000000000000000000000 --- a/docs/examples/tutorial/cython_tutorial/primes.py +++ /dev/null @@ -1,19 +0,0 @@ - -def primes(kmax): - result = [] - if kmax > 1000: - kmax = 1000 - - p = [0] * 1000 - k = 0 - n = 2 - while k < kmax: - i = 0 - while i < k and n % p[i] != 0: - i += 1 - if i == k: - p[k] = n - k += 1 - result.append(n) - n += 1 - return result diff --git a/docs/examples/tutorial/cython_tutorial/primes_python.py b/docs/examples/tutorial/cython_tutorial/primes_python.py new file mode 100644 index 0000000000000000000000000000000000000000..f6559d51959b840232aac2527e7b0474738fdd94 --- /dev/null +++ b/docs/examples/tutorial/cython_tutorial/primes_python.py @@ -0,0 +1,14 @@ +def primes_python(nb_primes): + p = [] + n = 2 + while len(p) < nb_primes: + # Is n prime? + for i in p: + if n % i == 0: + break + + # If no break occurred in the loop + else: + p.append(n) + n += 1 + return p diff --git a/docs/src/tutorial/cython_tutorial.rst b/docs/src/tutorial/cython_tutorial.rst index 7132b57f6584e6f1cdbd2f6c0ccada417618f965..504cc7131c13201dd7469aa704e3db0ffea2adc6 100644 --- a/docs/src/tutorial/cython_tutorial.rst +++ b/docs/src/tutorial/cython_tutorial.rst @@ -263,23 +263,9 @@ just like Python does. You can deactivate those checks by using the :ref:`compiler directives<compiler-directives>`. Now let's see if, even if we have division checks, we obtained a boost in speed. -Let's write the same program, but Python-style:: - - def primes_python(nb_primes): - p = [] - n = 2 - while len(p) < nb_primes: - # Is n prime? - for i in p: - if n % i == 0: - break - - # If no break occurred in the loop - else: - p.append(n) - n += 1 - return p +Let's write the same program, but Python-style: +.. literalinclude:: ../../examples/tutorial/cython_tutorial/primes_python.py It is also possible to take a plain ``.py`` file and to compile it with Cython. Let's take ``primes_python``, change the function name to ``primes_python_compiled`` and