Is there a way to make this reverse factorial code run more efficiently
I am just starting to learn python and made a program where it calculates the factorial number based on the factorial.
For example if I give the program the number 120 it will tell me it's factorial is 5
anyways my question is how can I make this code more efficient and faster.
Num = int(input()) i=0 for i in range(0,Num): i = i + 1 x = Num/i Num = x if (x==1): print(i)
Multiplications are much faster than divisions. You should try to reach the number with a factorial instead of dividing it iteratively:
def unfactorial(n): f,i = 1,1 while f < n: i += 1 f *= i return i if f == n else None unfactorial(120) # 5
Is it possible to reverse a factorial? : askscience, One of the ways I thought of doing this involved reversing a factorial, but I had no idea Just because there isn't a neat inverse function doesn't mean you can't write code to do it be more efficient than having to calculate two factorials 5 or greater on the machine code level. More posts from the askscience community. Is there any way I can 'undo' the factorial operation? JUst like you can do squares and square roots, can you do factorials and factorial roots (for lack of a better term)? Here is an example: 5! = 120. Is there a way I can work out the number that must be factorialed (??) to give the answer 120?
A few things you can do:
Num = int(input()) i=0 # your for loop will initialize i, you don't need to do this here for i in range(0,Num): i = i + 1 # your for loop will increment i, no need to do this either x = Num/i # you don't need the extra variable 'x' here Num = x if (x==1): print(i)
You can rewrite this to look something like:
for index in range(1, number): # start range at 1 number /= index # this means; number = number / index if number==1: return index
Is there a way to reverse factorials?, You can just divide the "answer" by consecutive positive integers, and when the result is 1, the last number you divided by is the number that the "answer" is� Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek". The factorial is normally used in Combinations and Permutations (mathematics). There are many ways to write the factorial program in java language. Let's see the 2 ways to write the factorial program in java. Factorial Program using loop; Factorial Program using
Compute the factorials in ascending order until you reach (or exceed) the factorial you are looking for, using the previous factorial to efficiently compute the next.
Program for factorial of a number, Factorial of a non-negative integer, is multiplication of all integers This code is contributed by Shivi_Aggarwal Here we have shown the iterative approach using both for and while loop. you find any bug in the above code/algorithm, or find other ways to solve the Most visited in School Programming. The above solutions cause overflow for small numbers. Please refer factorial of large number for a solution that works for large numbers. Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem. Attention reader! Don’t stop learning now.
def reverse_factorial(num): i = 1 while num > 1: i += 1 num /= i return i print(reverse_factorial(int(input())))
Now If we see the result from line_profiler we will see that most %time was spent in multiplication step of the above code i.e result *= x which is almost 98%. Second Improvement : Reduce the number of successive multiplication As the multiplication is very costly especially for a large number , we can use the pattern and reduces the number of
Factorial program in C#. Factorial Program in C#: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!.
- Hint: if a factorial is recursively multiplying, then an inverse factorial is recursively what?
- @wim: There is no recursion going on here.
- @ScottHunter never said there was.
- What if Num isn't a factorial of anything?
- Variable and function names should follow the
- Could you elaborate on you answer a bit more? Maybe by explaining the code?