Find the factorial , of a positive integer .
The factorial is defined as:
This is a fairly simple problem which can be trivially solved using loops.
fact = 1 for i in range(1, n+1): fact *= i print(fact)
We can approach the same thing using recursion also, by exploiting the fact that .
def fact(n): if n > 1: return n*fact(n-1) return 1 print(fact(n))
Now coming to the main part, we can get our one-liner using the recursive approach.
def fact(n): return n*fact(n-1) if n > 1 else 1 print(fact(n))