Due: October 28, 2024
Points: 30
Define the function:
The Collatz conjecture says that, if you iterate this sequence for any initial value of n, then eventually the sequence will reach the number 1.
For example, if n = 29, then the sequence is:
29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Write a recursive function reccol(n) that takes one argument, n, and prints the Collatz sequence that begins with n. Remember, this function must call itself! Also, you do not need any loops — if you have one, you probably did it wrong.
This is a function, so it must take a positive integer as its only parameter. You do not need to write a wrapping program to call it, nor do you need to check that the parameter is a positive integer; you may assume that.
For example, the function call
reccol(6)is to print
6, 3, 10, 5, 16, 8, 4, 2, 1and the function call
reccol(11)is to print
11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
To turn in: Please turn in the program in the file reccol.py.
|
ECS 235A, Computer and Information Security Version of October 17, 2024 at 9:20PM
|
You can also obtain a PDF version of this. |