How can this code that does factorial to the user input be more optimized?

75 Views Asked by At
a=1
for _ in range(int(input("Enter: "))):
    a=a*(_+1); print(a)

This code does the factorial and prints the values as the process goes. An input of 5 would print: 1 2 6 24 120 It takes the past output value and multiplies the next number in input.

I want to get the same output or even better by only printing after the calculation is done. But the most important thing I want to achieve is a better optimized code that uses less lines. One more thing don't use libraries.

3

There are 3 best solutions below

1
Alain T. On BEST ANSWER

You could print only the final result, in a single line using max and a walrus assignment:

print( max(1,a:=1,*(a := a*i for i in range(2,int(input("Enter: "))+1))) )

This even works for 0! = 1

If you want to print all the values in one line, you can write it without the max():

print( a:=1, *(a := a*i for i in range(2,int(input("Enter: "))+1)) )
1
Jab On

I guess another way of doing this would be a comprehension but this makes the code hard to read:

a=1
print(*(a:=a*(_+1) for _ in range(int(input("Enter: ")))), sep="\n")

I would suggest though a more readable way of doing this:

n = int(input("Enter: "))
a = 1
products = [a]

for i in range(n):
    a *= i
    products.append(a)

print(*products, sep="\n")
1
MachineLearner On

The standard implementation of the factorial function is given by this function.

def factorial(n: int) -> int:
    product: int = 1
    for number in range(1, n + 1):
        product = product * number
    return product

Another less efficient but easier to understand way is to use recursion

def factorial_recursive(n: int) -> int:
    if n == 1:
        return 1
    return n * factorial_recursive(n - 1)

Another alternative method is to use the reduce function.

from functools import reduce 

def factorial_reduce(n: int) -> int:
     numbers: tuple = tuple(range(1, n + 1))
     initial_value: int = 1
     return reduce(lambda current, product: current * product, numbers, initial_value)