I am trying to write a lambda function to do the same program which I already implemented using a while loop, but I am not able to figure out how to correct my current program using the lambda function. As here, I want to multiply 4 with decreased value of n. For example, 3 should work like 4*3=12 and then 12*2=24, but here it's multiplying 4 always with 3 if n=3. Using a while loop, I wrote this program like below given in foo function.
It's not factorial; basically, I want to print this series for different values of n like:
n=1 ans=4,
n=2 ans=8,
n=3 ans=24,
n=4 ans=96,
n=5 ans= 480.
The logic in foo function is generating this output.
foo= lambda n: 4*n**~-n
def foo(n):
a=4
i=1
while i<=n:
a*=i
i+=1
return a
print(foo(7)) #20160
from math import factorial; foo = lambda n: 4 * factorial(n)? Even better, define a function. Namedlambda's are against their purpose.