The easiest way is to use math.factorial (available in Python 2.6 and above):
import math
math.factorial(1000)
If you want/have to write it yourself, you can use an iterative approach:
def factorial(n):
fact = 1
for num in range(2, n + 1):
fact *= num
return fact
or a recursive approach:
def factorial(n):
if n < 2:
return 1
else:
return n * factorial(n-1)
Note that the factorial function is only defined for positive integers, so you should also check that n >= 0 and that isinstance(n, int). If it's not, raise a ValueError or a TypeError respectively. math.factorial will take care of this for you.
The easiest way is to use math.factorial (available in Python 2.6 and above):
import math
math.factorial(1000)
If you want/have to write it yourself, you can use an iterative approach:
def factorial(n):
fact = 1
for num in range(2, n + 1):
fact *= num
return fact
or a recursive approach:
def factorial(n):
if n < 2:
return 1
else:
return n * factorial(n-1)
Note that the factorial function is only defined for positive integers, so you should also check that n >= 0 and that isinstance(n, int). If it's not, raise a ValueError or a TypeError respectively. math.factorial will take care of this for you.
On Python 2.6 and up, try:
import math
math.factorial(n)
Need Help with Function to Calculate Factorial
Factorials
algorithm - Factorial of a large number in python - Stack Overflow
python - Calculate the factorial of a number - Stack Overflow
Videos
I have an assignment for class about a topic we never went over. Would appreciate help how to do it.
Write a script that inputs a nonnegative integer and computes and displays is facotrial. Try your script on the integers 10, 20, 30 and even larger values. Did you find any integer input for which Python could not produce an integer factorial value?
I have no idea where to start
Multiplying the numbers in sequence,
r = 1
for i in range(1, n + 1):
r *= i
return r
creates a large number (as in tens of thousands of bits) very quickly, and then you have a lot of multiplications of one huge number and one small number. Multiplications where at least one of the factors is huge are slow.
You can speed it up considerably by reducing the number of multiplications involving huge numbers, for example
def range_prod(lo,hi):
if lo+1 < hi:
mid = (hi+lo)//2
return range_prod(lo,mid) * range_prod(mid+1,hi)
if lo == hi:
return lo
return lo*hi
def treefactorial(n):
if n < 2:
return 1
return range_prod(1,n)
produces, timing the computation of 100000! % 100019 (I first tried len(str(fun(100000)), but the conversion to string is abominably slow, so that made the difference seem smaller than it is):
$ python factorial.py
81430
math.factorial took 4.06193709373 seconds
81430
factorial took 3.84716391563 seconds
81430
treefactorial took 0.344486951828 seconds
so a more than 10× speedup for 100000!.
Factorials get very large, so it is often better to deal with logarithms of the number.
Many languages have an lgamma library function which computes the natural logarithm of the factorial of n-1.
This means that you can compute the natural logarithm of factorial(n) via lgamma(n+1).
You can divide by log10 to turn this into a base 10 logarithm.
So if you just want the number of digits, then this Python code will give the answer immediately:
from math import *
print ceil(lgamma(100000+1)/log(10))
Iterative approach:
def calculate_factorial(n):
factorial = 1
for i in range(1, n + 1):
factorial *= i
return factorial
Recursive:
def calculate_factorial(n):
if n == 0:
return 1
else:
return n * calculate_factorial(n-1)
It would be great if you could share the code that you tried.
A possible method using a while loop:
factorial_base = int(input('Enter a positive integer:\n')) # The number to factorialize.
factorial = 1
if factorial_base > 0: # 0! = 1, so if the base is 0, just leave the factorial value as default (1).
while factorial_base != 1:
factorial *= factorial_base
factorial_base -= 1
Essentially, this will multiply 1 by the given number to factorialize (i.e. n), then by (n - 1), then by (n - 2), and so on, until the multiplication factor reaches 1.
Hope this helps.
Does math.factorial(n) simply multiply 1x2x3x4…n ? Or is there some other super fast algorithm I am not aware of? I am trying to write my own fast factorial algorithm and what to know it’s been done