File: fibc.c

package info (click to toggle)
python3.13 3.13.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,732 kB
  • sloc: python: 707,177; ansic: 655,263; xml: 31,250; sh: 5,844; cpp: 4,326; makefile: 1,986; objc: 787; lisp: 502; javascript: 213; asm: 75; csh: 12
file content (37 lines) | stat: -rw-r--r-- 670 bytes parent folder | download | duplicates (9)
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
#include <Python.h>

int fib(int n) {
	if (n < 0) {
		return -1; // Raise
	}
	if (n < 2) {
		return 1;
	}
	return fib(n - 1) + fib(n - 2);
}

static PyObject * fibc_fib(PyObject *self, PyObject *args) {
	int n;

	if (!PyArg_ParseTuple(args, "i", &n))
		return NULL;
	int r = fib(n);
	return PyLong_FromLong(r);
}

static PyMethodDef FibcMethods[] = {
	{"fib",  fibc_fib, METH_VARARGS, "Calculate a fibonacci sequence value."},
	{NULL, NULL, 0, NULL},
};

static PyModuleDef fibcmodule = {
	PyModuleDef_HEAD_INIT,
	"fibc",
	NULL,  // documentation
	-1,  // state in global vars
	FibcMethods
};

PyMODINIT_FUNC PyInit_fibc(void) {
	return PyModule_Create(&fibcmodule);
};