Question: PYTHON As far as we know, the collatz sequence starting at any positive number always terminates. This has been tested up to 19 *258. The
PYTHON
As far as we know, the collatz sequence starting at any positive number always terminates. This has been tested up to 19 *258. The following Python program computes Collatz sequences (and their lengths) starting at an arbitrary (positive) number def collatz(n) if n1: roturn (1, 11] next = 3*n+l if n 2 == 1 else n//2 countr 8.eq) _ collatz(next} return 1+count, [n] Eq) oollatseswrito a list conprchoneion hore> for ((n, (count, ea) in collatzes: printif'n). count: (count, seq: seq def argMax (dictionary)I return aax(dietionary, key-ewrite a lanbda expression bere>) print( nargNax(collatzes) When properly completed and run, it produces the following output. 11. count: 15, seq: [11, 34, 17, 52, 28, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] 12. count: 10, seq: [12, 6, 3, 10, 5, 16, 8, 4, 2, 1] 13. count: 10, seq: [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] 14. count: 18, seq: [14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2,1] 15. count: 18, seq: [15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1] 16. count: 5, seq: [16, 8, 4, 2, 1] 17. count: 13, seq: [17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1) 18. count: 21, seq: [18, 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 18, 8, 4, 2, 1] 19. count: 21, seq: [19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] 20. count: 8, seq: [20, 10, 5, 16, 8. 4, 2, 1] 21. count: 8, seq: [21, 64, 32, 16, 8, 4, 2,1] 22. count: 16, seq: [22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] 23. count: 16, seq: [23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1] 24. count: 11, seq [24, 12, 6, 3, 10, 5, 16, 8, 4, 2, 1] (18, (21, [18, 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10. 5, 16, 8, 4, 2,1])) The final line shows that the Collatz sequence starting at 18 contains 21 elements and consists of the numbers shown. Of the numbers tested, it and 19 have the longest Collatz sequences. Complete the program so that it produces the output shown
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
