def collatz_conjecture(n):
if n <= 0:
print("Please enter a positive integer.")
return
print(f"Starting number: {n}")
while n != 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
print(n, end=" ")
print("\nReached 1!")
try:
number = int(input("Enter a positive integer to test the Collatz conjecture: "))
collatz_conjecture(number)
except ValueError:
print("Invalid input! Please enter a valid integer.")
#source code --> clcoding.com
0 Comments:
Post a Comment