Created
August 10, 2021 17:54
-
-
Save ryankrage77/345419fd223a8029699e12ee609dc794 to your computer and use it in GitHub Desktop.
program to check if numbers disprove the Collatz conjecture
This file contains 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 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