Skip to content

Instantly share code, notes, and snippets.

@Tarliton
Created April 26, 2017 02:49
Show Gist options
  • Save Tarliton/df8f7782d53f97c6e7280ed9e36e6d18 to your computer and use it in GitHub Desktop.
Save Tarliton/df8f7782d53f97c6e7280ed9e36e6d18 to your computer and use it in GitHub Desktop.
#include <Python.h>
int Cfib(int n){
if (n < 2)
return n;
return Cfib(n-1) + Cfib(n-2);
}
static PyObject* fib(PyObject* self, PyObject* args){
int n;
if (!PyArg_ParseTuple(args, "i", &n))
return NULL;
return Py_BuildValue("i", Cfib(n));
}
static PyObject* version(PyObject* self){
return Py_BuildValue("s", "Version 1.0");
}
static PyMethodDef myMethods[] = {
{"fib", fib, METH_VARARGS, "Calculates the fibbonacci number."},
{"version", (PyCFunction)version, METH_NOARGS, "oops."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef myModuleModuleDef = {
PyModuleDef_HEAD_INIT,
"myModule",
"",
-1,
myMethods
};
PyMODINIT_FUNC PyInit_myModule(void){
return PyModule_Create(&myModuleModuleDef);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment