Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Collatz Sequence/Collatz Sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def collatz_steps(n):
times = 0
while n != 1:
if n % 2 == 0:
print(f"{n} / 2", end=" ")
n = n // 2 # "//" is a floor division where it rounds down the result
else:
print(f"{n} * 3 + 1", end=" ")
n = 3 * n + 1
print(f"= {n}")
times += 1
print(f"The number of times to reach 1 is {times}")

def main():
again = "y"
while again != "n":
n = int(input("Input a number: "))
collatz_steps(n)
while True:
again = str(input("Want to input again? y/n: "))
if again != "n" and again != "y":
print("Incorrect Input.")
elif again == "n":
print("Thank You! Goodbye.")
break
else:
break

main()