Reverse Factorial
Easy
math
factorial
number-theory
division
Problem Description
Given a positive integer, determine if it is a factorial of some integer. If yes, return that integer; if no, return -1. A factorial n! = n × (n-1) × (n-2) × ... × 2 × 1.
Input Format
A single positive integer.
Output Format
The integer n such that n! equals the input, or -1 if no such integer exists.
Constraints
• 1 ≤ input ≤ 10^18
• Handle large numbers efficiently
• 0! = 1, 1! = 1
• Factorial grows very quickly
• Use iterative division approach
Sample Input/Output
Input:
120
Output:
5
Explanation
120 = 5! = 5 × 4 × 3 × 2 × 1 = 120. So the answer is 5.
Solution Hints
60
Points
Points
1000ms
Time Limit
Time Limit
64MB
Memory Limit
Memory Limit
Code Editor
Register to Access Code Editor
Create a free account to solve coding problems and track your progress.
Register Now