Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions algorithms/arithmetic/fib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

#include <Python.h>

long long
_fib(long long n) {
if (n < 2)
return n;
else
return _fib(n - 1) + _fib(n - 2);
}

static PyObject *
fib(PyObject *self, PyObject *args) {
long long n;
if (!PyArg_ParseTuple(args, "L", &n))
return NULL;
return Py_BuildValue("L", _fib(n));

}

static PyMethodDef fib_methods[] = {
{
"fib_fast", fib, METH_VARARGS, "Calcs fib."
},
{ NULL, NULL, 0, NULL}
};

static struct PyModuleDef fib_def =
{
PyModuleDef_HEAD_INIT,
"algorithms.arithmetic.fib", /* name of module */
"", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
fib_methods
};

PyMODINIT_FUNC PyInit_fib(void) {
Py_Initialize();
return PyModule_Create(&fib_def);
}
8 changes: 8 additions & 0 deletions algorithms/arithmetic/fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Uses python3
def fib_recursive(n):
if n <= 1:
return n

return fib_recursive(n - 1) + fib_recursive(n - 2)


1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest==3.0.3
pytest-cov==2.5.1
python-coveralls==2.9.1
Cython==0.27.3
29 changes: 29 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os

from Cython.Build import cythonize
from setuptools import setup, Extension


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()


ext_modules = [Extension("algorithms.arithmetic.fib", ["algorithms/arithmetic/fib.c"])]
setup(
name="python_algorithms",
version="0.0.1",
author="Arseniy Antonov",
author_email="arseny.antonov@gmail.com",
description=("Python algorithms"),
license="MIT",
keywords="algorithms graph arithmetic cython",
packages=['algorithms', 'tests'],
long_description=read('README.rst'),
ext_modules=cythonize(ext_modules),
classifiers=[
"Development Status :: 3 - Alpha",
"Topic :: Utilities",
"License :: OSI Approved :: MIT License",
],

)