Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 50a3396

Browse files
committed
Added necessary comments on python code.
1 parent f0b91ea commit 50a3396

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

projects/Collatz_Conjecture/collatz.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1+
# Defined a class to generate Collatz Sequence
12
class Collatz:
3+
# Constructor to initialize the number
24
def __init__(self, n):
35
self.n = n
46

7+
# Iteration method
58
def __iter__(self):
69
return self
710

11+
# Next method to generate next number in the sequence
812
def __next__(self):
913
if self.n == 1:
14+
# Stop iteration when the number is 1
1015
raise StopIteration
16+
1117
if self.n % 2 == 0:
1218
self.n = self.n // 2
1319
else:
1420
self.n = 3 * self.n + 1
1521
return self.n
1622

1723

18-
24+
# Main function to test the class
1925
if __name__ == '__main__':
20-
print("To generate a Collatz Sequence, please provide an integer positive number")
26+
print("To generate a Collatz Sequence,")
2127
try:
22-
input_number = int(input("Please enter a number: "))
28+
input_number = int(input("Please enter a positive integer number: "))
2329
for i in Collatz(input_number):
2430
print(i, end=" ")
2531
except StopIteration:

0 commit comments

Comments
 (0)