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:
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:
Because the chain ends exactly at 1, the answer is 5!.
A Simple Python Solution
The cleanest implementation uses integer division and remainder checks:
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:
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:
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
nfrom a value that may equaln!. - A clean solution repeatedly divides by consecutive integers until the value becomes
1or the division fails. - Integer arithmetic is the correct tool for this problem.
- '
1is a special case because it equals both0!and1!.' - Python handles this task well because its integers can grow arbitrarily large.

