Dec-07-2020, 08:14 AM
Dear forum,
I got a question to my function below:
The problem is the return value of tau() is integer usually, but if I call tau() from test() the return value is None and not writable into my dictionary. Maybe you got an idea on how to solve this problem. Also as I am a python beginner I am open to any tips and considerations for the future.
I got a question to my function below:
The problem is the return value of tau() is integer usually, but if I call tau() from test() the return value is None and not writable into my dictionary. Maybe you got an idea on how to solve this problem. Also as I am a python beginner I am open to any tips and considerations for the future.
def tau(i,count=0): """ This function counts how many times it has to iterate a number to become 1. Its the function of the Collatz-Problem with a memory. """ if i < 1: raise ValueError("n muss >=1 sein") if i in tau.cache: return count + tau.cache[i] else: tau(i // 2 if i % 2 == 0 else i * 3 + 1, count + 1) tau.cache = {1: 0} def test(N): """ This function should write the value between 2 and N and his number of iterations into a dictionairy. """ for k in range(2,N): tau.cache[k] = tau(k) print(tau.cache)