reverse factorial
mathematics
number theory
factorial function
mathematical puzzles

Reverse factorial

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A reverse-factorial problem asks the opposite of the usual factorial question: given a number, determine whether it equals n! for some integer n, and if so recover that n. It is a small mathematical puzzle, but it also makes a good programming exercise because the direct integer-based solution is short and reliable.

The Core Observation

A factorial is a product of consecutive integers:

text
11! = 1
22! = 2
33! = 6
44! = 24
55! = 120

To reverse that process, keep dividing by 2, then 3, then 4, and so on. If the divisions stay exact until the value becomes 1, then the original number was a factorial.

For 120:

text
1120 / 2 = 60
260 / 3 = 20
320 / 4 = 5
45 / 5 = 1

Because the chain ends exactly at 1, the answer is 5!.

A Simple Python Solution

The cleanest implementation uses integer division and remainder checks:

python
1def reverse_factorial(value):
2    if value < 1:
3        return None
4
5    n = 1
6    current = value
7
8    while current > 1:
9        n += 1
10        if current % n != 0:
11            return None
12        current //= n
13
14    return n
15
16
17for number in [1, 2, 6, 24, 120, 150]:
18    result = reverse_factorial(number)
19    if result is None:
20        print(number, "is not a factorial")
21    else:
22        print(number, "=", f"{result}!")

This works because factorials are built from consecutive factors. The moment the divisibility chain breaks, the input cannot be a factorial.

The Special Case of 1

The value 1 is special because both 0! and 1! equal 1. Some exercises return 1, while others prefer to mention both possibilities.

If you want to preserve that ambiguity explicitly:

python
1def reverse_factorial_with_one(value):
2    if value == 1:
3        return "0! or 1!"
4
5    n = 1
6    current = value
7
8    while current > 1:
9        n += 1
10        if current % n != 0:
11            return None
12        current //= n
13
14    return f"{n}!"

The correct output depends on how the problem statement defines the expected answer.

Building Upward Instead of Dividing Downward

Another valid strategy is to compute factorials upward until you match or exceed the input:

python
1def reverse_factorial_by_building(value):
2    if value < 1:
3        return None
4
5    factorial = 1
6    n = 1
7
8    while factorial < value:
9        n += 1
10        factorial *= n
11
12    return n if factorial == value else None
13
14
15print(reverse_factorial_by_building(120))
16print(reverse_factorial_by_building(150))

This is also correct. The division-based method is often preferred because it feels like a direct reversal of the factorial operation.

Why Integer Arithmetic Matters

Do not use floating-point division for this problem. Factorials are exact integer products, so the check should stay entirely in integer arithmetic.

Using % and // is the right approach because:

  • divisibility is exact
  • there are no rounding errors
  • the logic remains easy to reason about

That is especially important when the numbers become large.

Performance Notes

Factorials grow very quickly, so the number of loop iterations stays small relative to the size of the number. Python also supports arbitrarily large integers, which makes it a convenient language for this task.

Even for large inputs, you only do one exact-divisibility check per candidate factor. The main cost comes from big-integer arithmetic, not from a huge number of iterations.

Common Pitfalls

The most common mistake is using floating-point math. Reverse factorial checks should stay in integer arithmetic the whole time.

Another mistake is forgetting the special case for 1. If the surrounding exercise cares about 0!, the solution should mention that explicitly.

People also assume a number close to a factorial must itself be one. Values such as 150 fail quickly because the exact division chain breaks.

Finally, do not overcomplicate the problem with prime factorization or advanced number theory unless the task specifically asks for it. The direct iterative method is usually enough.

Summary

  • Reverse factorial means recovering n from a value that may equal n!.
  • A clean solution repeatedly divides by consecutive integers until the value becomes 1 or the division fails.
  • Integer arithmetic is the correct tool for this problem.
  • '1 is a special case because it equals both 0! and 1!.'
  • Python handles this task well because its integers can grow arbitrarily large.

Course illustration
Course illustration

All Rights Reserved.