Approximating factorials with Cython
The last example is about approximating factorials with Cython. We will use two approximation methods. First, we will use the Stirling approximation method (see http://en.wikipedia.org/wiki/Stirling%27s_approximation for more information). The formula for the Stirling approximation is:
Secondly, we will be using the approximation due to Ramanujan, with the following formula:
How to do it...
This section describes how to approximate factorials using Cython. In this recipe, we will be using types, which as you may remember, is optional in Cython. In theory, declaring static types should speed things up. Static typing offers interesting challenges that you may not encounter when writing Python code, but don't worry, we will try to keep it simple.
Write the Cython code.
The Cython code that we will write looks like regular Python code, except that we declare function parameters and a local variable to be an
ndarray
array. In order to get the static types to...