Skip to content

Instantly share code, notes, and snippets.

@ryankrage77
Created August 10, 2021 17:54
Show Gist options
  • Save ryankrage77/345419fd223a8029699e12ee609dc794 to your computer and use it in GitHub Desktop.
Save ryankrage77/345419fd223a8029699e12ee609dc794 to your computer and use it in GitHub Desktop.
program to check if numbers disprove the Collatz conjecture
import multiprocessing as mp
def worker(num):
a = num
b=0
#if a becomes smaller than the number we are currently testing, then it is a previously tested number, and so we can discard the test.
while a!=1 and a >= num:
if a%2==0:
a=(a/2)
else:
a=a*3+1
#print(a)
b+=1
#print(str(num) + " failed in " + str(b) + " steps")
def main():
#numbers up to 2^28, or about 2.95^20, have already been tested.
start = 295000000000000000001
#this could be replaced with any range, or an infinite loop.
for i in range(1,100):
#theoretically, larger batch sizes are more efficient, as the pool is recreated less often. Adjust as desired.
stop = start + 100000000
pool = mp.Pool(mp.cpu_count())
pool.map(worker, range(start,stop,2))
pool.close()
print("numbers " + str(start) + " to " + str(stop) + " failed to disprove the collatz conjecture.")
start = stop
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment