Finding number with specified collatz sequence length

281 Views Asked by At

I need to make a program that finds a number with specified collatz sequence length. However, there is always a problem that the program is too slow. For example my current best score that i could get was number with collatz sequence length of 1200 (I need to be able to get number with collatz sequence length of 1800).

I tried a lot of diffrent methods, but the best one so far was trying to recreate collatz number tree.Here is an example from wiki. As I said before i need to be able to get a number with collatz sequence length of 1800 but I cant get more than 1200.

That's my current solution (I know it's complicated but other methods I tried so far were able to get collatz sequence length up to 500 only):

A = int(input())

limit = 1000000000000000000

def runCollaz(ciag):

    steps = 0

    while ciag != 1:
        
        if (ciag % 2 == 0):
            ciag /= 2
        else:
            ciag *= 3
            ciag += 1
        steps+=1
    return steps

def makeChainLess(number):
    if (number % 2 == 0):
        return number / 2
    else:
        return ((number * 3) + 1)

collatzTree = [[1, 1]]

finallAns = "None"

def getAns(collatzTree, what):    
    awnser = "None"

    if (collatzTree[0][0] < limit and collatzTree[0][1] == A):
        awnser = collatzTree[0][0]

    while (len(collatzTree) > 250):
        currentHigh = collatzTree[0][0]
        highIndex = 0
        index = 0
        for x in collatzTree:
            if (x[0] > currentHigh):
                currentHigh = x[0]
                highIndex = index
            index += 1
        collatzTree.pop(highIndex)
            

    if (collatzTree[0][0] > 4):
        if (collatzTree[0][0] - 1) % 3 == 0:
            if (collatzTree[0][0] - 1) % 2 != 0:
                collatzTree += [[(collatzTree[0][0] - 1) / 3, int(collatzTree[0][1]) + 1]]
            collatzTree += [[collatzTree[0][0] * 2, int(collatzTree[0][1]) + 1]]
            collatzTree.pop(0)
        else:
            collatzTree += [[collatzTree[0][0] * 2, int(collatzTree[0][1]) + 1]]
            collatzTree.pop(0)

    else:
        collatzTree += [[collatzTree[0][0] * 2, int(collatzTree[0][1]) + 1]]
        collatzTree.pop(0)
    if (what == "C"):
        return collatzTree
    else:
        return awnser

while finallAns == "None":
    finallAns = getAns(collatzTree, "A")
    collatzTree = getAns(collatzTree, "C")
print(int(finallAns))

If anyone could help i would really appricate it.

1

There are 1 best solutions below

4
gerald On

Here is a simple code that takes only a few minutes to run for 10 million. It just needs more computing power, which is kind of the story on Collatz.

'''
This code finds numbers that have the specified Collatz sequence length
(by brute force)
'''
import time
start = time.time()                      #start code timer

n = 1                                        #set low end of range
for number in range (10000000, n-1, -1):   #work down from max range
     num = number
     trial = 0
     while number > 1:                 #set up looping until 1 is reached
            
         if number % 2 == 0:         #set up normal Collatz sequence comp.
              number = number // 2             
                             
         else:
              number = (number * 3) + 1
                           
         trial = trial+1                  #update counter for each loop
         if number == 1 and trial == 500 or trial == 600:  #set target numbers
                  #print all numbers where specified target occured:
              print("Number with sequence "
                    "length of {0}: {1}".format(trial, num))
                                         
if number == n:
     print("end of range")                #tells user that code has ended

end = time.time()
print("elapsed time: ",end - start)