diff --git a/algorithms/arithmetic/fib.c b/algorithms/arithmetic/fib.c new file mode 100644 index 0000000..7c111ea --- /dev/null +++ b/algorithms/arithmetic/fib.c @@ -0,0 +1,40 @@ + +#include + +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); +} \ No newline at end of file diff --git a/algorithms/arithmetic/fibonacci.py b/algorithms/arithmetic/fibonacci.py new file mode 100644 index 0000000..b0f1d1f --- /dev/null +++ b/algorithms/arithmetic/fibonacci.py @@ -0,0 +1,8 @@ +# Uses python3 +def fib_recursive(n): + if n <= 1: + return n + + return fib_recursive(n - 1) + fib_recursive(n - 2) + + diff --git a/requirements.txt b/requirements.txt index e302695..207a964 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pytest==3.0.3 pytest-cov==2.5.1 python-coveralls==2.9.1 +Cython==0.27.3 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..af4da12 --- /dev/null +++ b/setup.py @@ -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", + ], + +)