Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- print ("This program looks for secquences of consecutive numbers whose Collatz chains do not end in the cycle 5 > 16 > 8 > 4 > 2 > 1.")
- print("""Please input a top bound and a sequence length.
- The program will print out all sequences of numbers at least as long as the sequence length
- found below the boundary number.
- For the love of God either have a low top bound or a high sequence length.""")
- topbound = int(input('Please input the top bound \n > '))
- seqlength = int(input('Please input the minimum sequence length: \n > '))
- adecouslist = []
- for i in range (1, topbound + 1): # This runs each number within the bound through its Collatz steps. If it doesn't have a 5 in its history, it gets appended to magiclist.
- currentnum = i
- pathlist = []
- pathlist.append(currentnum) #start with the starting number--starts with 1)
- while currentnum != 1: # all collatz chains end in 1, or so the conjecture says, so you stop there
- if currentnum % 2 == 0: # if it's even...
- currentnum = int( currentnum / 2) # divide it by two
- pathlist.append(currentnum) # and add the new number to the list
- else: # otherwise, it's going to be odd
- currentnum = (currentnum * 3) + 1 # multiply by three and add one
- pathlist.append(currentnum)
- if 5 not in pathlist and (pathlist[0] & (pathlist[0]-1) != 0):
- adecouslist.append(pathlist[0])
- else: pass
- seqlist = []
- seqdict = {}
- seqlengthnum = [] # this is a list of all numbers that *begin* a sequence of at least n long
- for i in range(0, len(adecouslist)-1):
- if adecouslist[i+1] - adecouslist[i] == 1: # if 85-84 == 1, which it does: ...if 113 - 85 is 1, which it isn't...
- seqlist.append(adecouslist[i]) # append 84
- elif adecouslist[i+1] - adecouslist[i] != 1 and adecouslist [i] - adecouslist [i-1] == 1: # and... 113-85 ain't 1, but
- seqlist.append(adecouslist[i])
- if len(seqlist) >= seqlength:
- seqdict[seqlist[0]] = len(seqlist) # Creates an entry in dictionary seqdict.
- # The key in seqdict is the first number of the sequence, and the value is the length.
- seqlengthnum.append(seqlist[0])
- seqlist.clear()
- seqlengthval = [] # the lengths of sequences
- for number in (seqlengthnum):
- seqlengthval.append(seqdict[number])
- sortedlengths = seqlengthval.copy()
- sortedlengths.sort() # these are the lengths in ascending order
- toplength = max(sortedlengths) + 1
- bottomlength = min(sortedlengths)
- for k in range(bottomlength, toplength):
- for j in seqlengthnum:
- if seqdict[j] == k:
- print(f'There is an adecous sequence of length {k} starting at {j}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement