
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Inverse Factorial in Python
Suppose we have a number a, we have to find n, such that factorial of n (n!) is same as a. As we know, the factorial n = n * (n - 1) * (n - 2) * ... * 1. If there is no such integer n then return -1.
So, if the input is like a = 120, then the output will be 5.
To solve this, we will follow these steps −
- i := 0, num := 1
- L:= a new list
- while i < a, do
- i := factorial of num
- insert i at the end of L
- num := num + 1
- if a is in L, then
- return the (index of a in L) +1
- otherwise,
- return -1
Let us see the following implementation to get better understanding −
Example
import math class Solution: def solve(self, a): i,num=0,1 L=[] while i < a : i=math.factorial(num) L.append(i) num+=1 if a in L : return L.index(a)+1 else : return -1 ob = Solution() print(ob.solve(120))
Input
120
Output
5
- Related Articles
- factorial() in Python
- Calculate Factorial in Python
- Clumsy Factorial in Python
- Get the Trigonometric inverse sin in Python
- Get the Trigonometric inverse cosine in Python
- Compute the inverse Hyperbolic sine in Python
- Compute the inverse Hyperbolic cosine in Python
- Get the Trigonometric inverse tangent in Python
- Compute the inverse Hyperbolic tangent in Python
- Python – Inverse Dictionary Values List
- Compute the inverse cosine with scimath in Python
- Compute the inverse sine with scimath in Python
- Check if N is a Factorial Prime in Python
- Python Program for factorial of a number
- Compute the multiplicative inverse of a matrix in Python

Advertisements