forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython-collatz_conjecture.py
More file actions
30 lines (26 loc) · 935 Bytes
/
Python-collatz_conjecture.py
File metadata and controls
30 lines (26 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
The Collatz conjecture is a conjecture in mathematics that concerns a sequence
defined as follows: start with any positive integer n. Then each term is obtained
from the previous term as follows: if the previous term is even, the next term is
one half of the previous term. If the previous term is odd, the next term is 3
times the previous term plus 1. The conjecture is that no matter what value of n,
the sequence will always reach 1.(From Wikipedia)"""
def collatz(number):
if number % 2 ==0:
answer = number //2
else:
answer = 3 *number + 1
print(answer)
return answer
while True:
try:
result = int (input("Enter a number: "))
print(result)
except ValueError:
print("That's not a number.Try again")
continue
while result != 1: #1 is the point we want to reach
result = collatz(result)
quit = input("Do you want to continue? y / n ")
if quit == 'n':
break