How many FLOPs are there in calculating a factorial using math.factorial(n) in python
Question: I am trying to understand how many FLOPs are there if I use a certain
algorithm to find the exponential approximated sum, specially If I use
math.factorial(n) in python. I understand FLOPs for binary operation, so is
factorial also a binary operation here within a function? Not being a computer
science major, I have some difficulties with these. My code looks like this:

    
    
    from __future__ import division
    import numpy as np
    import matplotlib.pyplot as plt
    import math
    
    
    
    x = input ("please enter a number for which you want to run the exponential summation e^{x}")
    N = input ("Please enter an integer before which term you want to turncate your summation")
    
    function= math.exp(x)
    exp_sum = 0.0
    abs_err = 0.0
    rel_err = 0.0
    
    
    
    for n in range (0, N):
        factorial = math.factorial(n)            #How many FLOPs here?
        power     = x**n                         # calculates N-1 times
        nth_term  = power/factorial              #calculates N-1 times
        exp_sum   = exp_sum + nth_term           #calculates N-1 times
        abs_err   = abs(function - exp_sum)
        rel_err   = abs(abs_err)/abs(function)
    

Please help me understand this. I might also be wrong about the other FLOPs!


Answer: According to that [SO answer](http://stackoverflow.com/a/9815339/5847976) and
to the [C source
code](https://hg.python.org/cpython/file/2145593d108d/Modules/mathmodule.c#l1121),
in python2.7 `math.factorial(n)` uses a naive algorithm to compute the
factorial so **it computes using about n operations** as
factorial(n)=1*2*3*4*...*n.

A small mistake regarding the rest is that **`for n in range(0,N)` will loop
`N` times** , not `N-1` (from `n=0` to `n=N-1`).

A final note is that counting FLOP may not be representative of the actual
algorithm real world performance especially in python that is an interpretted
language and that it tends to hide most of its inner working behind clever
syntax that links to compiled C code(eg: `exp_sum + nth_term` is actualy
`exp_sum.__add__(nth_term)`).

