root / Pi / C / examples / capi / fibmodule.c @ 308
Historique | Voir | Annoter | Télécharger (486 octet)
1 |
#include <Python.h> |
---|---|
2 |
|
3 |
int _fib(int n) |
4 |
{ |
5 |
if (n < 2) |
6 |
return n;
|
7 |
else
|
8 |
return _fib(n-1) + _fib(n-2); |
9 |
} |
10 |
|
11 |
static PyObject* fib(PyObject* self, PyObject* args)
|
12 |
{ |
13 |
int n;
|
14 |
|
15 |
if (!PyArg_ParseTuple(args, "i", &n)) |
16 |
return NULL; |
17 |
|
18 |
return Py_BuildValue("i", _fib(n)); |
19 |
} |
20 |
|
21 |
static PyMethodDef FibMethods[] = {
|
22 |
{"fib", fib, METH_VARARGS, "Calculate the Fibonacci numbers."}, |
23 |
{NULL, NULL, 0, NULL} |
24 |
}; |
25 |
|
26 |
PyMODINIT_FUNC |
27 |
initfib(void)
|
28 |
{ |
29 |
(void) Py_InitModule("fib", FibMethods); |
30 |
} |