-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathcFibA.c
More file actions
71 lines (62 loc) · 1.64 KB
/
Copy pathcFibA.c
File metadata and controls
71 lines (62 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#define PPY_SSIZE_T_CLEAN
#include "Python.h"
long fibonacci(long index) {
if (index < 2) {
return index;
}
return fibonacci(index - 2) + fibonacci(index - 1);
}
//long fibonacci(long index) {
// static long *cache = NULL;
// if (!cache) {
// /* FIXME */
// cache = calloc(1000, sizeof(long));
// }
// if (index < 2) {
// return index;
// }
// if (!cache[index]) {
// cache[index] = fibonacci(index - 2) + fibonacci(index - 1);
// }
// return cache[index];
//}
static PyObject *
py_fibonacci(PyObject *Py_UNUSED(module), PyObject *args) {
long index;
if (!PyArg_ParseTuple(args, "l", &index)) {
return NULL;
}
long result = fibonacci(index);
return Py_BuildValue("l", result);
}
//static PyObject *
//py_fibonacci(PyObject *Py_UNUSED(module), PyObject *args, PyObject *kwargs) {
// long index;
//
// static char *keywords[] = {"index", NULL};
// if (!PyArg_ParseTupleAndKeywords(args, kwargs, "l", keywords, &index)) {
// return NULL;
// }
// long result = fibonacci(index);
// return Py_BuildValue("l", result);
//}
//
static PyMethodDef module_methods[] = {
{"fibonacci",
(PyCFunction) py_fibonacci,
METH_VARARGS,
"Returns the Fibonacci value."
},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static PyModuleDef cFibA = {
PyModuleDef_HEAD_INIT,
.m_name = "cFibA",
.m_doc = "Fibonacci in C.",
.m_size = -1,
.m_methods = module_methods,
};
PyMODINIT_FUNC PyInit_cFibA(void) {
PyObject *m = PyModule_Create(&cFibA);
return m;
}