Hướng dẫn 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

 Live Demo

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

Hướng dẫn inverse factorial in python

Updated on 23-Sep-2020 07:23:39

  • Related Questions & Answers
  • factorial() in Python
  • Clumsy Factorial in Python
  • Calculate 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
  • Inverse operation in JavaScript
  • C# factorial
  • Compute the inverse hyperbolic tangent with scimath in Python

So the question comes like this, I'm new to python:

def factorial_cap(num): For positive integer n, the factorial of n (denoted as n!), is the product of all positive integers from 1 to n inclusive. Implement the function that returns the smallest
positive n such that n! is greater than or equal to argument num. o Assumption: num will always be a positive integer.

# Examples 
# factorial_cap(20) output is 4  since 3!<20 but 4!>20
# factorial_cap(24) output is 4 since 4!=24
# factorial_cap(1) output is 1 since 1!=1

# And here is what I got

def factorial_cap(num):
    n = 1
    for i in range (1,num+1):
        n = n*i    

I'm pretty sure this is the right function for factorial def. But I just couldn't figure out, instead of getting the 'total value', how can I just get the right output as I posted example above?

Btw, should I use 'return' at the end of def, or it does not matter in this case?

Teaching Kids Programming – Two Algorithms to Compute Inverse Factorials

Teaching Kids Programming: Videos on Data Structures and Algorithms

The factorial of a number n is defined as n! = n * (n – 1) * (n – 2) * … * 1. Given a positive integer a, return n such that n! = a. If there is no integer n that is a factorial, then return -1.

Constraints
n < 2 ** 31
Example 1
Input
a = 6
Output
3
Explanation
3! = 6

Example 2
Input
a = 10
Output
-1
Explanation
10 is not any integer factorial.

Inverse Factorial by Dividing

Try dividing N by 2, 3, 4… until we can’t or the dividend is 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def solve(self, a):
        if a == 1: 
            return 1
        if a <= 0:
            return -1
        i = 2
        while a > 1:
            if a % i != 0:
                return -1
            a /= i
            i += 1
        return i - 1

class Solution:
    def solve(self, a):
        if a == 1: 
            return 1
        if a <= 0:
            return -1
        i = 2
        while a > 1:
            if a % i != 0:
                return -1
            a /= i
            i += 1
        return i - 1

Inverse Factorial by Multipication

Try multiply the product by 2, 3, 4, until the number is bigger or equal to N. When it is bigger, the given number is not a factorial number, otherwise it is.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def solve(self, a):
        if a == 1: 
            return 1
        if a <= 0:
            return -1
        s = 1
        i = 2
        while s < a:
            s *= i
            i += 1
        return i - 1 if s == a else -1

class Solution:
    def solve(self, a):
        if a == 1: 
            return 1
        if a <= 0:
            return -1
        s = 1
        i = 2
        while s < a:
            s *= i
            i += 1
        return i - 1 if s == a else -1

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...

343 words
Last Post: Caesar Cipher Algorithm in C++
Next Post: Teaching Kids Programming - Compute the Sum of the Digits using Three Methods

The Permanent URL is: Teaching Kids Programming – Two Algorithms to Compute Inverse Factorials (AMP Version)