#ulam.py #Ulam's recursive function def ulam(n, yes_print=True): if yes_print: print(n, end=" ") if n == 1: return 1 elif n%2 == 0: #n even, call again with half of n return ulam(n // 2, yes_print) + 1 #avoid float . else: #n odd, call again with 3n+1 return ulam(3*n+1, yes_print) + 1 ''' ulam(5) print() ulam(12) print() ulam(97) print() ''' end_int = int(input("Ending int to go up to: ")) num_recurses = [0] #one item for i in range(1,end_int+1): # print(i,": ", end="") num_recurses.append(ulam(i,yes_print=False)) # print(num_recurses[i], end=" ") max_calls = max(num_recurses) print("Max calls: ", max_calls) print(" number is", num_recurses.index(max_calls), "(might be dups)") print("Average:",(sum(num_recurses)/len(num_recurses))) ''' N avg max 100 32 119 1000 60 179 10000 85 262 100000 108 351 1000000 132 525 '''