#------------------------------------------------------------------------------- # Collatz2.py #------------------------------------------------------------------------------- def oddPart(n): """Returns the largest odd number dividing n """ if n%2==1: return n; else: return oddPart(n//2) # recursive call to oddPart() # end oddPart() def abreviatedCollatz(n): """ Prints the Collatz sub-sequence obtained by dividing out all even factors in a single step """ while n != 1: print(n, end=", ") if n % 2 == 0: # n is even n = oddPart(n) else: # n is odd n = 3*n + 1 # end while print(n, end=".\n") #end abreviatedCollatz() # function main() ------------------------------------------------------------- def main(): k = int(input('Enter an integer: ')) print('The Abreviated Collatz sequence starting at', k, 'is:') abreviatedCollatz(k) # end main() # closing conditional --------------------------------------------------------- if __name__=='__main__': main() # end