Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

special.factorial

Computes the factorial of a number or array of numbers. It can use exact integer arithmetic or a faster floating-point approximation.

How it works

factorial computes the product of the positive integers through each non-negative input. With exact=False it uses the gamma-function relationship for a faster floating-point result; exact=True uses integer arithmetic and preserves exact values.

Syntax

scipy.special.factorial(n, exact=False)

Parameters

NameTypeDescription
nint or array_like of intsInput value or array of values for which to compute the factorial.
exactbool, optionalIf True, use exact long-integer arithmetic; otherwise use a floating-point approximation.

Returns

float or int or ndarray — Factorial of n, as an integer or float depending on exact.

Key points

  • The function supports scalar values as well as arrays.
  • For a non-negative integer, the result multiplies every positive integer up to n.
  • Set exact=True when integer precision is required.
  • The calculation uses approximate mode unless exact is explicitly enabled.
  • The result representation depends on the exact flag.
  • Approximate mode is implemented through the gamma-function relationship.

Common mistakes

  • Negative inputs produce 0 under the documented behavior.

Example: Exact and approximate factorials

import numpy as np
from scipy.special import factorial

values = np.array([3, 4, 5])
print(factorial(values, exact=True))
print(factorial(5))

Computes exact factorials for an integer array and the default floating-point result for a scalar.

When to use it

Combinatorics

Calculate combinatorial quantities for integer inputs.

Vectorized numerical computation

Apply factorial elementwise to a NumPy array.

Exact arithmetic

Choose exact integer results when rounding errors are unacceptable.

Interview questions

QWhat does scipy.special.factorial compute?

It accepts an integer or an array-like collection of integers and returns the corresponding factorial. For an array input, the result is an array with matching elementwise factorial values.

Follow-up: How can the result type change? With exact=True, results use integer arithmetic; with exact=False, results are floating-point approximations.

QWhat is the purpose of the exact argument?

exact controls the calculation method. True requests exact long-integer arithmetic, while False uses a faster floating-point approximation based on the gamma function.

Follow-up: Why might exact=False be preferable? It is faster and suitable when floating-point output and approximation are acceptable.

QWhat does factorial return for negative inputs and arrays?

The function returns a float, int, or ndarray depending on the input and exact setting. Negative n values return 0, according to the documented behavior.

Follow-up: What happens for an array containing negative values? Each negative element follows the same documented rule and contributes 0 to the output array.

  • scipy.special.gamma

Source: scipy.special.factorial — SciPy v1.2.3 Reference Guide. Checked 2026-10-15.