Question: in python def collatz(n): Generate the collatz sequence starting from n. n must be >- 1. if n = 1') if n--1 return [1] next

 in python def collatz(n): Generate the collatz sequence starting from n.

in python

def collatz(n): Generate the collatz sequence starting from n. n must be >- 1. if n = 1') if n--1 return [1] next = 3*n+1 if n % 2 == return [n] + collatz (next) else n//2 # Create a dictionary with key n and value collatz (n) collatzes = # Print out the sequences in order of their lengths. for (n, seq ) in sorted ( collatzes. items(), key= print(f'(n). (seq (length: (len(seg)') The output should look like this, 1. [1] (length: 1) 2. [2, 1] (length: 2) 4. [4, 2, 11 (length: 3) 8. [8, 4, 2, 1] (length: 4) 16. [16, 8, 4, 2, 1] (length: 5) 5. [5, 16, 8, 4, 2, 1] (length: 6) 10. [10, 5, 16, 8, 4, 2, 1] (length: 7) 3. [3, 10, 5, 16, 8, 4, 2, 1] (length: 8) 20. [20, 10, 5, 16, 8, 4, 2, 1] (length: 8) 21. [21, 64, 32, 16, 8, 4, 2, 1] (length: 8) 6. [6, 3, 10, 5, 16, 8, 4, 2, 1] (length: 9) 12. [12, 6, 3, 10, 5, 16, 8, 4, 2, 1] (length: 10) 13. [13, 40, 20, 10, 5, 16, 8, 4, 2, 1] (length: 10) 24. [24, 12, 6, 3, 10, 5, 16, 8, 4, 2, 1] (length: 11) 17. [17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] (length: 13) 11. [11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 11 (length: 15) 22. [22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] (length: 16) 23. [23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1] (length: 16) 7. [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] (length: 17) 14. [14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] (length: 18) 15. [15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1] (length: 18) 9. [9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] (length: 20) 18. [18, 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] (length: 21) 19. [19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 (length: 21)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!