AlgoMaster Logo

Factorial of a Number

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

The factorial of n is the product of every integer from 1 up to n, and it counts the number of ways to arrange n distinct items in a row. So 5! = 1 x 2 x 3 x 4 x 5 = 120. Each step multiplies the previous result by the next integer: 2! = 2, 3! = 6, 4! = 24, 5! = 120.

Two small inputs need a fixed rule. 1! is 1, the single-element product. 0! is also defined as 1, the empty product, which is the identity for multiplication and keeps n! = n x (n-1)! consistent down to n = 1. An accumulator that starts at 1 returns the right answer for both 0 and 1, because the multiplication loop never executes.

The size of the result needs the most care. Factorials grow faster than exponentials. 12! = 479001600 fits in a signed 32-bit integer, but 13! = 6227020800 exceeds 2^31 - 1 and overflows. A signed 64-bit integer holds values up to 9223372036854775807, covering factorials through 20! = 2432902008176640000. So the accumulator is 64 bits wide in every language: long in Java and C#, long long in C++, int64 in Go, and i64 in Rust.

JavaScript and TypeScript add a tighter limit. Both store numbers as 64-bit floating point, exact only up to Number.MAX_SAFE_INTEGER (9007199254740991, roughly 9 x 10^15). 18! = 6402373705728000 stays exact, but 19! and 20! exceed it and lose precision. To keep every language exact on the same inputs, all test cases stay at n <= 18.

Key Constraints:

  • n can be 00! is defined as 1, the empty product. An accumulator starting at 1 returns 1 here because the multiplication loop runs zero times.
  • n can be 11! is 1. The same accumulator handles it with no special case, since multiplying 1 by nothing leaves it unchanged.
  • The result overflows 32-bit integers past 12!13! exceeds 2^31 - 1. Use a 64-bit accumulator (long, long long, int64, i64) so values up to 20! stay exact.
  • JavaScript and TypeScript numbers are exact only to Number.MAX_SAFE_INTEGER (9007199254740991) → 18! is below that bound and exact, but 19! and 20! are not. All inputs here stay at n <= 18 so every language agrees.

Approach 1: Iterative Product

Intuition

Factorial is a running product, so build it with an accumulator. Start a variable at 1, walk through the integers 2, 3, ..., n, and multiply the accumulator by each one. After the last multiplication, the accumulator holds n!. Starting at 1 rather than 0 matters: 1 is the identity for multiplication, so the first factor lands unchanged, and the loop over 2, ..., n runs zero times for n = 0 and n = 1, leaving the accumulator at 1.

Algorithm

  1. Set an accumulator result to 1.
  2. For each integer i from 2 up to n, multiply result by i.
  3. Return result.

Example Walkthrough

Input:

5
n

The accumulator starts at 1. Multiplying by 2 gives 2, by 3 gives 6, by 4 gives 24, and by 5 gives 120. The loop has reached n, so it stops and the answer is 120.

120
result

Code

Approach 2: Recursion

Intuition

Factorial has a self-referential definition: n! = n x (n-1)!. That translates directly into a function that calls itself. To compute factorial(n), multiply n by factorial(n-1) and let the same function resolve the smaller piece.

A recursion needs a base case that returns without recursing, or it calls itself forever. The base case here is factorial(0) = 1, the empty product. Each call lowers n by one, so the chain reaches 0 for any non-negative input. Once a call sees n = 0, it returns 1, and the pending multiplications unwind back up the chain. This also covers n = 1, since factorial(1) returns 1 x factorial(0) = 1.

Algorithm

  1. If n is 0, return 1. This is the base case that stops the recursion.
  2. Otherwise, return n multiplied by factorial(n-1).

Example Walkthrough

Input:

5
n

The first call is factorial(5), which returns 5 x factorial(4). That waits on factorial(4), which returns 4 x factorial(3), and so on down to factorial(0), which returns 1 from the base case. Now the calls unwind: factorial(1) returns 1 x 1 = 1, factorial(2) returns 2 x 1 = 2, factorial(3) returns 3 x 2 = 6, factorial(4) returns 4 x 6 = 24, and factorial(5) returns 5 x 24 = 120. The base case stopped the descent at 0, and each pending multiplication completed on the way back up.

120
result

Code