{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# 1A.soft - Calcul num\u00e9rique et Cython\n", "\n", "Python est tr\u00e8s lent. Il est possible d'\u00e9crire certains parties en C mais le dialogue entre les deux langages est fastidieux. Cython propose un m\u00e9lange de C et Python qui acc\u00e9l\u00e8re la conception."]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [{"data": {"text/html": ["
\n", ""], "text/plain": ["Generated by Cython 0.29.27
\n", "\n", " Yellow lines hint at Python interaction.
\n", " Click on a line that starts with a \"+
\" to see the C code that Cython generated for it.\n", "
01: cimport cython\n", "
02:
\n", "+03: def cprimes(int kmax):\n", "
/* Python wrapper */\n", "static PyObject *__pyx_pw_46_cython_magic_1d6a283823e92ffa4c0f1198af611a6c_1cprimes(PyObject *__pyx_self, PyObject *__pyx_arg_kmax); /*proto*/\n", "static PyMethodDef __pyx_mdef_46_cython_magic_1d6a283823e92ffa4c0f1198af611a6c_1cprimes = {\"cprimes\", (PyCFunction)__pyx_pw_46_cython_magic_1d6a283823e92ffa4c0f1198af611a6c_1cprimes, METH_O, 0};\n", "static PyObject *__pyx_pw_46_cython_magic_1d6a283823e92ffa4c0f1198af611a6c_1cprimes(PyObject *__pyx_self, PyObject *__pyx_arg_kmax) {\n", " int __pyx_v_kmax;\n", " PyObject *__pyx_r = 0;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"cprimes (wrapper)\", 0);\n", " assert(__pyx_arg_kmax); {\n", " __pyx_v_kmax = __Pyx_PyInt_As_int(__pyx_arg_kmax); if (unlikely((__pyx_v_kmax == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 3, __pyx_L3_error)\n", " }\n", " goto __pyx_L4_argument_unpacking_done;\n", " __pyx_L3_error:;\n", " __Pyx_AddTraceback(\"_cython_magic_1d6a283823e92ffa4c0f1198af611a6c.cprimes\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __Pyx_RefNannyFinishContext();\n", " return NULL;\n", " __pyx_L4_argument_unpacking_done:;\n", " __pyx_r = __pyx_pf_46_cython_magic_1d6a283823e92ffa4c0f1198af611a6c_cprimes(__pyx_self, ((int)__pyx_v_kmax));\n", " int __pyx_lineno = 0;\n", " const char *__pyx_filename = NULL;\n", " int __pyx_clineno = 0;\n", "\n", " /* function exit code */\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "\n", "static PyObject *__pyx_pf_46_cython_magic_1d6a283823e92ffa4c0f1198af611a6c_cprimes(CYTHON_UNUSED PyObject *__pyx_self, int __pyx_v_kmax) {\n", " int __pyx_v_n;\n", " int __pyx_v_k;\n", " int __pyx_v_i;\n", " int __pyx_v_p[0x3E8];\n", " PyObject *__pyx_v_result = NULL;\n", " PyObject *__pyx_r = NULL;\n", " __Pyx_RefNannyDeclarations\n", " __Pyx_RefNannySetupContext(\"cprimes\", 0);\n", "/* \u2026 */\n", " /* function exit code */\n", " __pyx_L1_error:;\n", " __Pyx_XDECREF(__pyx_t_1);\n", " __Pyx_AddTraceback(\"_cython_magic_1d6a283823e92ffa4c0f1198af611a6c.cprimes\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n", " __pyx_r = NULL;\n", " __pyx_L0:;\n", " __Pyx_XDECREF(__pyx_v_result);\n", " __Pyx_XGIVEREF(__pyx_r);\n", " __Pyx_RefNannyFinishContext();\n", " return __pyx_r;\n", "}\n", "/* \u2026 */\n", " __pyx_tuple_ = PyTuple_Pack(7, __pyx_n_s_kmax, __pyx_n_s_kmax, __pyx_n_s_n, __pyx_n_s_k, __pyx_n_s_i, __pyx_n_s_p, __pyx_n_s_result); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 3, __pyx_L1_error)\n", " __Pyx_GOTREF(__pyx_tuple_);\n", " __Pyx_GIVEREF(__pyx_tuple_);\n", "/* \u2026 */\n", " __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_1d6a283823e92ffa4c0f1198af611a6c_1cprimes, NULL, __pyx_n_s_cython_magic_1d6a283823e92ffa4c); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error)\n", " __Pyx_GOTREF(__pyx_t_1);\n", " if (PyDict_SetItem(__pyx_d, __pyx_n_s_cprimes, __pyx_t_1) < 0) __PYX_ERR(0, 3, __pyx_L1_error)\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", "
04: cdef int n, k, i\n", "
05: cdef int p[1000]\n", "
+06: result = []\n", "
__pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 6, __pyx_L1_error)\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __pyx_v_result = ((PyObject*)__pyx_t_1);\n", " __pyx_t_1 = 0;\n", "
+07: if kmax > 1000:\n", "
__pyx_t_2 = ((__pyx_v_kmax > 0x3E8) != 0);\n", " if (__pyx_t_2) {\n", "/* \u2026 */\n", " }\n", "
+08: kmax = 1000\n", "
__pyx_v_kmax = 0x3E8;\n", "
+09: k = 0\n", "
__pyx_v_k = 0;\n", "
+10: n = 2\n", "
__pyx_v_n = 2;\n", "
+11: while k < kmax:\n", "
while (1) {\n", " __pyx_t_2 = ((__pyx_v_k < __pyx_v_kmax) != 0);\n", " if (!__pyx_t_2) break;\n", "
+12: i = 0\n", "
__pyx_v_i = 0;\n", "
+13: while i < k and n % p[i] != 0:\n", "
while (1) {\n", " __pyx_t_3 = ((__pyx_v_i < __pyx_v_k) != 0);\n", " if (__pyx_t_3) {\n", " } else {\n", " __pyx_t_2 = __pyx_t_3;\n", " goto __pyx_L8_bool_binop_done;\n", " }\n", " if (unlikely((__pyx_v_p[__pyx_v_i]) == 0)) {\n", " PyErr_SetString(PyExc_ZeroDivisionError, \"integer division or modulo by zero\");\n", " __PYX_ERR(0, 13, __pyx_L1_error)\n", " }\n", " __pyx_t_3 = ((__Pyx_mod_int(__pyx_v_n, (__pyx_v_p[__pyx_v_i])) != 0) != 0);\n", " __pyx_t_2 = __pyx_t_3;\n", " __pyx_L8_bool_binop_done:;\n", " if (!__pyx_t_2) break;\n", "
+14: i = i + 1\n", "
__pyx_v_i = (__pyx_v_i + 1);\n", " }\n", "
+15: if i == k:\n", "
__pyx_t_2 = ((__pyx_v_i == __pyx_v_k) != 0);\n", " if (__pyx_t_2) {\n", "/* \u2026 */\n", " }\n", "
+16: p[k] = n\n", "
(__pyx_v_p[__pyx_v_k]) = __pyx_v_n;\n", "
+17: k = k + 1\n", "
__pyx_v_k = (__pyx_v_k + 1);\n", "
+18: result.append(n)\n", "
__pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_n); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 18, __pyx_L1_error)\n", " __Pyx_GOTREF(__pyx_t_1);\n", " __pyx_t_4 = __Pyx_PyList_Append(__pyx_v_result, __pyx_t_1); if (unlikely(__pyx_t_4 == ((int)-1))) __PYX_ERR(0, 18, __pyx_L1_error)\n", " __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n", "
+19: n = n + 1\n", "
__pyx_v_n = (__pyx_v_n + 1);\n", " }\n", "
+20: return result\n", "
__Pyx_XDECREF(__pyx_r);\n", " __Pyx_INCREF(__pyx_v_result);\n", " __pyx_r = __pyx_v_result;\n", " goto __pyx_L0;\n", "