We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents c39c63e + 364f946 commit d65cbc6Copy full SHA for d65cbc6
Collatz Sequence/Collatz Sequence.py
@@ -0,0 +1,29 @@
1
+def collatz_steps(n):
2
+ times = 0
3
+ while n != 1:
4
+ if n % 2 == 0:
5
+ print(f"{n} / 2", end=" ")
6
+ n = n // 2 # "//" is a floor division where it rounds down the result
7
+ else:
8
+ print(f"{n} * 3 + 1", end=" ")
9
+ n = 3 * n + 1
10
+ print(f"= {n}")
11
+ times += 1
12
+ print(f"The number of times to reach 1 is {times}")
13
+
14
+def main():
15
+ again = "y"
16
+ while again != "n":
17
+ n = int(input("Input a number: "))
18
+ collatz_steps(n)
19
+ while True:
20
+ again = str(input("Want to input again? y/n: "))
21
+ if again != "n" and again != "y":
22
+ print("Incorrect Input.")
23
+ elif again == "n":
24
+ print("Thank You! Goodbye.")
25
+ break
26
27
28
29
+main()
0 commit comments