Testing a Collatz Conjecture Conjecture (Python) I had an intuition that the maximal trajectory lengths for collatz conjecture numbers would be produced by numbers of the form (2**n)-1 so I wrote a program to test it (I was wrong). I thought I would put it up for review to catch any coding sins I committed, and see how others would improve my code.
Orginal Code
'''
*    I had a conjecture that maximal trajectory lengths for the collatz conjecture were yielded by numbers of the form (2**n)-1. This program attempts to test it.
*    &quot;conjecture()&quot; finds the number with the highest trajectory lengths in a given range. The ranges are of the form [2**n, 2**(n+1)).
'''

import math, pprint

f = open(&quot;CollatzConjecture.txt&quot;)
text =  f.read()

def key(value, dct):
    for key in dct.keys():
        if dct[key] == value:
            return key
    return None


def conjecture(text):
    traj = {int(j.split(&quot; &quot;)[0]): int(j.split(&quot; &quot;)[1]) for j in text.split(&quot;\n&quot;)}    #Processes the text file into a dict mapping numbers to their trajectory length.
    result = {}
    bound = math.floor(math.log(len(traj),2))
    traj2 = {num: traj[num] for num in list(traj.keys())[:2**bound+1]}    #Slices the dictionary to receive the section of interest.
    for st in (2**x for x in range(1, bound+1)):        #Generator for powers of 2.
        slce = list(traj2.items())[int((math.log(st,2)-1)**2):st]    #Slices &quot;traj2&quot; into powers of 2.
        result[st] = key(max([n[1] for n in slce]), traj2)
    return pprint.pformat(result)

print(conjecture(text))

&quot;CollatzConjecture.txt&quot; is a text file containing number trajectoryLength on separate lines. I wanted to find the number corresponding to the maximum trajectory length between successive powers of 2 in order to test my hypothesis.
def key(value, dct):
    for key in dct.keys():
        if dct[key] == value:
            return key
    return None



This is really inefficient and not how dictionaries should be used.


The point of a dict is O(1) lookup, this is essentially O(n) and it doesn't make sense
You should remove your blockcomments, and create a nice docstring. That will make the purpose of the code clearer
You don't close the file, a nice addition is using the with statement to automatically open/close files PEP343