Because the main post would've been too crowded otherwise, the Python code is hidden here. Obviously, this isn't production-quality code -- no comments and shorthand for if statements! -- but it's still fun to play with. :)
def f(n):
"""
Return the number of recursive calls of this function on n needed to
produce a value of 0.
"""
return _f(n, 0)
def _f(n, calls):
result = (n / 2) if n % 2 == 0 else ((3 * n) + 1)
calls += 1
return calls if result == 1 else _f(result, calls)
if __name__ == '__main__':
for n in range(1, 1000):
print "n = %d: %d" % (n, f(n))
No comments:
Post a Comment