Today, we are going to Program a project of Collatz Sequence using Python. This program is for mid-level programmers who have the basic knowledge of Python concepts.
Theory: Program of Collatz Sequence in Python
The Collatz sequence : It is a sequence of number following the below given rule. The first number is input and termed as z.
1) If z is even, then the next number z is z / 2.
2) If z is odd, then the next number z is z * 3 + 1.
3) If z is 1, then it will stop. Otherwise, repeat.
Step by Step Guide to Project:
step 1: Importing file sys, time
sys file imports exit function from the output when ctrl+c is pressed.
Time module is used to provide time gap between output.
step 2: General description of project is provided.
Step 3: Starting number of Collatz sequence is input. Input is checked whether it is a positive natural number. If yes then program goes on other step if it is not then warning is issued.
Step 4: Using if-else condition the number is processed and the next number is provided to while loop again and this loop goes on till the value is not equal to 1.
Source code : Program of Collatz Sequence in Python
import sys, time print('''Collatz Sequence, the 3z + 1 Problem By Archit The Collatz sequence it is a sequence of number following the below given rule. The first number is inputed and termend as z. 1) If z is even, then the next number z is z / 2. 2) If z is odd, then the next number z is z * 3 + 1. 3) If z is 1, then it will stop. Otherwise, repeat. ''') print('Enter a start number (larger than 0) or QUIT:') response = input('> ') if not response.isdecimal() or response == '0': print('You must enter an integer greater than 0.') sys.exit() z = int(response) print(z, end='', flush=True) while z != 1: if z % 2 == 0: # If n is even... z = z // 2 else: # Otherwise, n is odd... z = (3*z) + 1 print(', ' + str(z), end='', flush=True) time.sleep(0.1) print()
Output:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license()” for more information.
=== RESTART: C:/Users/ARCHIT/Desktop/new python progrm/collatz sequence.py ===
Collatz Sequence, or, the 3z + 1 Problem
By Archit
The Collatz sequence it is a sequence of number following the below given rule.
The first number is inputed and termend as z.
1) If z is even, then the next number z is z / 2.
2) If z is odd, then the next number z is z * 3 + 1.
3) If z is 1, then it will stop. Otherwise, repeat.
Enter a start number (larger than 0) or QUIT:
27
27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1
Conclusion :
The program is running fine.
Thanks for reading and copying the Program of Collatz Sequence in Python . This program is for mid-level programmers and Engineering students who want to get placed in reputed firms. For more programs click the below link.
Python Programs Archives – CS Study