Dec-07-2020, 04:09 PM
Aside from not returning a value from your recursion, you are doing the counting backward. You should return a count from tau, not pass a count to tau.
def tau(i): """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 tau.cache[number] return tau(i // 2 if i % 2 == 0 else i * 3 + 1) + 1I do not like how your cache works either. You only cache final results, so the cache doesn't provide much of a benefit. The tau function may make several recursive function calls, each calculating a count for some number. All of these partial results should be added tot he cache. That way each time you calculate tau() you will be making the next call to tau() run faster.