Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why python math.factorial(x) is very fast? [closed]

Tags:

python

I have a question, why python numeric calculation is very fast? for example the below code runs shorter than one second

import  math
print math.factorial(10000)

why???

like image 559
Pooya Avatar asked Jul 03 '12 15:07

Pooya


People also ask

How does factorial work in Python?

factorial() in Python Finding the factorial of a number is a frequent requirement in data analysis and other mathematical analysis involving python. The factorial is always found for a positive integer by multiplying all the integers starting from 1 till the given number.

Does math module in Python have factorial?

In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math. factorial() function returns the factorial of desired number. Syntax: math.

What symbol does Python use for factorial?

The factorial of a number is the number that you get after multiplying all numbers from 1 to that number. The factorial of a number is denoted by the '! ' symbol.

Does Python recognize factorial?

Python is a very powerful programming tool and can be used for a wide variety of use cases in real life. It offers a direct function that can compute the factorial of a given number without writing the code explicitly.


1 Answers

The math module's functions are implemented in C:

It provides access to the mathematical functions defined by the C standard.

By using an efficient algorithm in C, you get fast results.

If you are asking why this particular operation is so fast, then see Why is math.factorial much slower in Python 2.x than 3.x? and the C code itself.

like image 137
Martijn Pieters Avatar answered Oct 14 '22 05:10

Martijn Pieters