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.

Answer from schnaader on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › factorial-in-python
factorial() in Python - GeeksforGeeks
The factorial of a number n (written as n!) is the product of all positive integers from 1 to n. ... Python provides a function math.factorial() that computes factorial without writing the entire loop manually.
Published   December 18, 2025
Discussions

Need Help with Function to Calculate Factorial
Hello everyone, I’m trying to write a function in Python that calculates the factorial of a given number, but I’m running into some issues. Here’s what I have so far: def factorial(n): if n More on discuss.python.org
🌐 discuss.python.org
0
October 11, 2024
Factorials
Well. What is Factorial? Factorial 5 is 5! = 1 * 2 * 3 * 4 * 5. All you need is a starting variable that is initialized to 1, and then you need to loop over all numbers from 1 (can be optimized to start at 2) to the_factorial_you_want_to_calculate. For every number that you loop over you multiply your starting variable by that number. After you looped over all the numbers, you have the factorial value in your starting variable. More on reddit.com
🌐 r/learnpython
7
2
October 11, 2021
algorithm - Factorial of a large number in python - Stack Overflow
Here's my approach to factorials: def factorial(n): '''Returns factorial of n''' r = 1 for i in range(1, n + 1): r *= i return r I think it's pretty straightforward, though I More on stackoverflow.com
🌐 stackoverflow.com
python - Calculate the factorial of a number - Stack Overflow
Define a function called calculate_factorial that takes in one parameter, number. Inside the function, write code to calculate the factorial of the given number. The factorial of a number is the pr... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › ref_math_factorial.asp
Python math.factorial() Method
The math.factorial() method returns the factorial of a number.
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
2 weeks ago - The intent of the C standard is that fmod(x, y) be exactly (mathematically; to infinite precision) equal to x - n*y for some integer n such that the result has the same sign as x and magnitude less than abs(y). Python’s x % y returns a result with the sign of y instead, and may not be exactly computable for float arguments.
🌐
Python.org
discuss.python.org › python help
Need Help with Function to Calculate Factorial - Python Help - Discussions on Python.org
October 11, 2024 - Hello everyone, I’m trying to write a function in Python that calculates the factorial of a given number, but I’m running into some issues. Here’s what I have so far: def factorial(n): if n < 0: return "Inv…
🌐
Programiz
programiz.com › python-programming › examples › factorial
Python Program to Find the Factorial of a Number
If the number is positive, we use for loop and range() function to calculate the factorial. # Python program to find the factorial of a number provided by the user # using recursion def factorial(x): """This is a recursive function to find the factorial of an integer""" if x == 1 or x == 0: ...
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.special.factorial.html
factorial — SciPy v1.17.0 Manual
Scientific Python Forum · Search Ctrl+K · scipy.special. scipy.special.factorial(n, exact=False, extend='zero')[source]# The factorial of a number or array of numbers. The factorial of non-negative integer n is the product of all positive integers less than or equal to n: n!
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-for-factorial-of-a-number
Factorial of a Number - Python - GeeksforGeeks
This method computes the factorial using Python’s built-in factorial() function, which performs the entire calculation internally without requiring loops or recursion in user code.
Published   November 29, 2025
Top answer
1 of 9
27

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!.

2 of 9
12

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))
🌐
Boot.dev
boot.dev › lessons › ff1b4f58-af4b-40ed-94dd-b83ce0a57279
Learn to Code in Python: Factorial | Boot.dev
A factorial is the product (multiplication result) of all positive integers less than or equal to a given number.
🌐
AlmaBetter
almabetter.com › bytes › articles › factorial-program-in-python
Factorial Program in Python(Factorial of a Number in Python)
October 19, 2024 - The factorial function is a general implementation that uses a for loop inside a function, returning the factorial result. ... If you prefer not to implement your own function, Python's math library provides a built-in function to calculate ...
🌐
Codingem
codingem.com › home › factorial with a while loop in python
Factorial with a While Loop in Python - codingem.com
April 10, 2023 - To find factorial using a while loop in Python, multiply the result (starting at 1) by the number - 1 in a loop until the number reaches 1.
🌐
Vultr
docs.vultr.com › python › examples › find-the-factorial-of-a-number
Python Program to Find the Factorial of a Number | Vultr Docs
December 27, 2024 - Recursive functions are powerful for tasks like finding factorial because the factorial computation follows the definition of recursion (factorial(n) = n * factorial(n-1)). The function calls itself with decreasing values of n until it reaches the base case. Python’s standard library includes a function for computing factorial directly.
🌐
Educative
educative.io › answers › how-to-compute-the-factorial-of-a-number-in-python
How to compute the factorial of a number in Python
11. For each loop, the method multiplies to the base value until the for loop finishes and you get the final factorial value.
🌐
SciPy
docs.scipy.org › doc › scipy-1.16.0 › reference › generated › scipy.special.factorial.html
factorial — SciPy v1.16.0 Manual
Scientific Python Forum · Search Ctrl+K · scipy.special. scipy.special.factorial(n, exact=False, extend='zero')[source]# The factorial of a number or array of numbers. The factorial of non-negative integer n is the product of all positive integers less than or equal to n: n!
🌐
YouTube
youtube.com › wrath of math
Program Factorial in Python (Tutorial) | Python for Math - YouTube
We go over how to program a function that calculates factorials in Python, without recursion. We'll just need the range() function and a for loop. #PythonRec...
Published   July 14, 2022
Views   38K
🌐
YouTube
youtube.com › watch
Recursive Factorial Program Tutorial | Python for Math - YouTube
We program a recursive function that calculates factorials in Python. This is a great introduction exercise for recursive programming. We just need to notice...
Published   July 15, 2022