Created
August 9, 2016 19:57
-
-
Save ironmanMA/faf615519981621326f023c202e01e70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
__author__ = 'mohammad' | |
_memoization_map={} | |
def try_collatz(num): | |
try: | |
# print num | |
if num == 1: | |
# print "Done" | |
_memoization_map[num]=1 | |
return 1 | |
elif num in _memoization_map: | |
return try_collatz(_memoization_map[num]) | |
elif (num % 2 == 0): | |
_memoization_map[num] = try_collatz(num / 2) | |
return try_collatz(_memoization_map[num]) | |
else: | |
_memoization_map[num] = try_collatz((3 * num + 1) / 2) | |
return try_collatz(_memoization_map[num]) | |
except Exception, ex: | |
raise ex | |
if __name__ == '__main__': | |
for x in range(90000,99500): | |
try_collatz(random.randint(0,9923381239)) | |
print len(_memoization_map) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment