A Recursive approach to the Collatz Sequence in Python

preview_player
Показать описание
Solving the Collatz Sequence problem from "Automate the Boring Stuff" by using a recursive function.
The book is: Automate the Boring Stuff by Al Sweigart
Рекомендации по теме
Комментарии
Автор

the way I did it was :


from replit import clear
retry = True
while retry == True:
num = int(input("any number: "))
print("collatz sequence: ")
while num != 1:
if num % 2 == 0:
num //= 2
print(round(num))
else:
num = ((num * 3) + 1)
print(round(num))
if num == 1:
print("done")
attempt = input("want to retry? Y or N : ").lower()
if attempt == "y":
clear()
retry = True
else:
retry = False
fyi I used replit so the clear module might not work if you use any other ide, simply delete the first line and any other "clear()" commands if that's the case

xenon
Автор

question: why would the collatz sequence requires a 'count' parameter while others such as pentagonal or the triangular number doesn't?

toanphan
Автор

It’s not a series of operations since ur only doing like 3: multiplying 3, adding 1, and dividing by 2…..

gobyg-major
join shbcf.ru