How do I find the factorial of a positive number? The challenge:


  Write a program or a function that inputs a positive number and returns its factorial.


Note: This is a code-trolling question. Please do not take the question and/or answers seriously. More information here. Every code-trolling question is also a popularity-contest question, so the highest voted answer wins.
This is a very simple numerical computing problem that we can solve with Stirling's approximation:



As you can see, that formula features a square root, which we will also need a way to approximate. We will choose the so-called "Babylonian method" for that because it is arguably the simplest one:



Note that computing the square root this way is a good example of recursion.

Putting it all together in a Python program gives us the following solution to your problem:

def sqrt(x, n): # not the same n as below
    return .5 * (sqrt(x, n - 1) + x / sqrt(x, n - 1)) if n &gt; 0 else x

n = float(raw_input())
print (n / 2.718) ** n * sqrt(2 * 3.141 * n, 10)


With a simple modification the above program can output a neat table of factorials:

1! =    0.92215
2! =    1.91922
3! =    5.83747
4! =   23.51371
5! =  118.06923
6! =  710.45304
7! = 4983.54173
8! = 39931.74015
9! = 359838.58817


This method should be sufficiently accurate for most applications.