def collatz_max ( n ): #*****************************************************************************80 # ## collatz_max() returns the largest element of a Collatz sequence. # # Discussion: # # The Collatz sequence is defined recursively as follows: # # Let T be the current entry of the sequence, and U the next: # # If T is 1, the sequence terminates (or U = 1, your choice) # Else if T is even, U = T / 2 # Else (if T is odd, and greater than 1) U = 3 * T + 1. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 June 2022 # # Author: # # John Burkardt # # Input: # # integer n, the starting point for the sequence. # # Output: # # integer tmax: the maximum element of the sequence. # tmax = n if ( n <= 0 ): return tmax while ( n != 1 ): if ( ( n % 2 ) == 0 ): n = n // 2 else: n = 3 * n + 1 tmax = max ( tmax, n ) return tmax def collatz_max_test ( ): print ( '' ) print ( 'collatz_max_test ( ):' ) print ( ' Print maximum value in Collatz sequence' ) n = 7 tmax = collatz_max ( n ) print ( '' ) print ( ' Starting at ', n, ' maximum value is ', tmax ) return if ( __name__ == "__main__" ): collatz_max_test ( )