Skip to content

Commit d65cbc6

Browse files
Merge pull request #2844 from FROGROWL/master
Frogrowl 🐸
2 parents c39c63e + 364f946 commit d65cbc6

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Collatz Sequence/Collatz Sequence.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
else:
27+
break
28+
29+
main()

0 commit comments

Comments
 (0)