Python Programming Exercises,
Gently Explained
122
while num ____ 1:
# If odd, the next number is 3 times the current number plus 1:
if num % 2 == ____:
num = 3 * num + 1
# If even, the next number is half the current number:
else:
num = num // ____
# Record the number in the sequence list:
sequence.append(____)
# Return the sequence of numbers:
return ____
The complete solution for this exercise is given in Appendix A and
https://invpy.com/collatzsequence.py. You can view each step of this program as it
runs under a debugger
at
https://invpy.com/collatzsequence-debug/.
Dostları ilə paylaş: