Advertisement
Guest User

Untitled

a guest
May 9th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. import math
  2. print ("This program looks for secquences of consecutive numbers whose Collatz chains do not end in the cycle 5 > 16 > 8 > 4 > 2 > 1.")
  3. print("""Please input a top bound and a sequence length.
  4. The program will print out all sequences of numbers at least as long as the sequence length
  5. found below the boundary number.
  6. For the love of God either have a low top bound or a high sequence length.""")
  7.  
  8. topbound = int(input('Please input the top bound \n > '))
  9. seqlength = int(input('Please input the minimum sequence length: \n > '))
  10. adecouslist = []
  11.  
  12. 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.  
  13.     currentnum = i
  14.     pathlist = []
  15.     pathlist.append(currentnum) #start with the starting number--starts with 1)
  16.     while currentnum != 1: # all collatz chains end in 1, or so the conjecture says, so you stop there
  17.         if currentnum % 2 == 0: # if it's even...
  18.             currentnum = int( currentnum / 2) # divide it by two
  19.             pathlist.append(currentnum) # and add the new number to the list
  20.         else: # otherwise, it's going to be odd
  21.             currentnum = (currentnum * 3) + 1 # multiply by three and add one
  22.             pathlist.append(currentnum)
  23.  
  24.     if 5 not in pathlist and (pathlist[0] & (pathlist[0]-1) != 0):
  25.         adecouslist.append(pathlist[0])
  26.     else: pass
  27.  
  28. seqlist = []
  29. seqdict = {}
  30. seqlengthnum = [] # this is a list of all numbers that *begin* a sequence of at least n long
  31. for i in range(0, len(adecouslist)-1):
  32.     if adecouslist[i+1] - adecouslist[i] == 1: # if 85-84 == 1, which it does: ...if 113 - 85 is 1, which it isn't...
  33.         seqlist.append(adecouslist[i]) # append 84
  34.     elif adecouslist[i+1] - adecouslist[i] != 1 and adecouslist [i] - adecouslist [i-1] == 1: # and... 113-85 ain't 1, but
  35.         seqlist.append(adecouslist[i])
  36.         if len(seqlist) >= seqlength:
  37.             seqdict[seqlist[0]] = len(seqlist) # Creates an entry in dictionary seqdict.
  38.             # The key in seqdict is the first number of the sequence, and the value is the length.
  39.             seqlengthnum.append(seqlist[0])
  40.         seqlist.clear()
  41.  
  42. seqlengthval = [] # the lengths of sequences
  43.  
  44. for number in (seqlengthnum):
  45.     seqlengthval.append(seqdict[number])
  46.  
  47. sortedlengths = seqlengthval.copy()
  48. sortedlengths.sort() # these are the lengths in ascending order
  49.  
  50. toplength = max(sortedlengths) + 1
  51. bottomlength = min(sortedlengths)
  52. for k in range(bottomlength, toplength):
  53.     for j in seqlengthnum:
  54.         if seqdict[j] == k:
  55.             print(f'There is an adecous sequence of length {k} starting at {j}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement