Generating a Collatz Sequence using a python program

M Nayoumi
2 min readAug 8, 2020

--

From Project Euler, I had written a programs for collatz sequence.
collatz sequence.
Firstly, I like the name of the program, this is the first time I heard about this type of sequence. So, lets jump how the sequence is formed.
1.If the number is even than the number should be divided by 2 or half of the number
2.If number is odd the number should be 3 times of number and added to Example: I would like to start with an example number 17
17 is odd number
(3*17)+1=52
52 is even number
52/2=26
26 is even number
26/2=13
13 is odd number
(3*13)+1=40
40 is even
40/2=2 ………………so on till the number is 1
collatz-sequence:
17 → 52 → 26 → 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

I have put the collatz sequence program in a flow chart

flow chart for collatz sequence
python program for collatz sequence
output for number 17
output for number 34

While programing this I thought it would take a long time, but practicing daily made me write the program in less time.
I had written the program by defining a function and used a while loop for iteration until the number is 1 and generates the sequence for any number. This is a dynamic program

--

--

M Nayoumi
M Nayoumi

Written by M Nayoumi

Studying Masters in data science

No responses yet