How to make python code run faster? (For solving the Collatz Conjecture)

104 Views Asked by At
def main_func():

    def infinity():
        n = 295147905179352825856
        while True:
            yield n
            n+=1

    i = 295147905179352825856
    data = 0

    def func(i, num, x, data):
        if x == True:
            print(i, num, "F", data)
            #F means the number that can't be reduced to 1 is found
            i = 0
        else:
            print("Not Successful")

    check = 0

    for i in infinity():
        i+=1
        data+=1
        num=i
        if i > 1:
            while i > 1:

                if i % 2 == 0:
                    i = int(i/2)

                    if i == 1:
                        #print(data, ".", i, num)
                        #print("T")
                        if data == 100000:
                            data = 0
                            check+=1
                            print (check, num)
                            #This is done to make the console print less numbers/text

                        #else:
                            #print ("Not reached 1 Lakh")

                        #T means the number can be reduced to 1

                else:
                    i = int(3*i + 1)

            if i != 1:
                x = bool(True)
                func (i, num, x, data)
                print(i, num, "Eureka!")

        else:
            print("Try Again")

main_func()

I have tried to make a solution for the Collatz Conjecture using Python. This code check all the numbers from 2^68 upto infinity (never ends) whether it can be converted to 1 by using either n/2 or 3n+1. How can I make the process of checking faster?

0

There are 0 best solutions below