So, I have to run a recursive function times but it's going to take too long to actually print out all the values. Thus, my professor recommended using Excel but I don't know Excel at all. I need help converting the code into Excel.
It's probably easy for someone who knows Excel.
def a(n):
k=3.8
if n==0:
return .5
else:
return k*a(n-1)*(1-a(n-1))
for i in range(100):
print(a(i))
All i know is that you use the lambda() function in excel
This is the lambda formulation in Excel:
=LAMBDA(n,IF(n=0,0.5,LET(prev,a(n-1),3.8*prev*(1-prev))))

which can be tested as follows:

The available workspace for recursion is actually very limited in Excel and you will soon run out of resources unless you adjust the formula in a similar way to the Python solution.
In Excel you also have the option of referring to the previous cell so if you wanted to stay with the original formulation, with 0.5 in C1, you could enter this in C2 and drag down:
=3.8*C1*(1-C1)
You don't need to use excel. You just need to use a better algorithm. The easiest way to prevent the exponential time complexity is don't re-calculate the same value twice:
def a(n):
k = 3.8
if n==0:
return .5
else:
x = a(n - 1)
return k*x*(1-x)
for i in range(100):
print(a(i))
In Python, you should avoid recursion, though, since Python doesn't optimize recursion and you will run out of stack space. This is easy to convert to an iterative algorithm, though:
def b(n):
k = 3.8
prev = curr = 0.5
for i in range(1, n + 1):
curr = k * prev * (1 - prev)
prev = curr
return curr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With