Starting with any positive integer N, Collatz sequence is defined corresponding to n as the numbers formed by the following operations :
- If n is even, then n = n / 2.
- If n is odd, then n = 3*n + 1.
- Repeat above steps, until it becomes 1.
Examples :
Input : 3 Output : 3, 10, 5, 16, 8, 4, 2, 1 Input : 6 Output : 6, 3, 10, 5, 16, 8, 4, 2, 1
Below is the implementation :
C++
// CPP program to print Collatz sequence #include <bits/stdc++.h> using namespace std; void printCollatz(int n) { // We simply follow steps // while we do not reach 1 while (n != 1) { cout << n << " "; // If n is odd if (n & 1) n = 3*n + 1; // If even else n = n/2; } // Print 1 at the end cout << n; } // Driver code int main() { printCollatz(6); return 0; } |
chevron_right
filter_none
Java
// Java program to print // Collatz sequence import java.io.*; class GFG { static void printCollatz(int n) { // We simply follow steps // while we do not reach 1 while (n != 1) { System.out.print(n + " "); // If n is odd if ((n & 1) == 1) n = 3 * n + 1; // If even else n = n / 2; } // Print 1 at the end System.out.print(n); } // Driver code public static void main (String[] args) { printCollatz(6); } } // This code is contributed // by akt_mit |
chevron_right
filter_none
Python3
# Python 3 program to print # Collatz sequence def printCollatz(n): # We simply follow steps # while we do not reach 1 while n != 1: print(n, end = ' ') # If n is odd if n & 1: n = 3 * n + 1 # If even else: n = n // 2 # Print 1 at the end print(n) # Driver code printCollatz(6) # This code is contributed # by vaibhav29498 |
chevron_right
filter_none
C#
// C# program to print Collatz sequence using System; class GFG { static void printCollatz(int n) { // We simply follow steps // while we do not reach 1 while (n != 1) { Console.Write (n + " "); // If n is odd if ((n & 1) == 1) n = 3 * n + 1; // If even else n = n / 2; } // Print 1 at the end Console.Write (n); } // Driver code static void Main() { printCollatz(6); } } // This code is contributed by // Manish Shaw (manishshaw1) |
chevron_right
filter_none
PHP
<?php // PHP program to print Collatz sequence function printCollatz($n) { // We simply follow steps // while we do not reach 1 while ($n != 1) { echo $n . " "; // If $n is odd if ($n & 1) $n = 3 * $n + 1; // If even else $n = $n / 2; } // Print 1 at the end echo $n; } // Driver code printCollatz(6); // This code is contributed // by ChitraNayal ?> |
chevron_right
filter_none
Output
6 3 10 5 16 8 4 2 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- G-Fact 21 | Collatz Sequence
- Maximum Sequence Length | Collatz Conjecture
- Program to implement Collatz Conjecture
- Print n terms of Newman-Conway Sequence
- Print first N terms of Lower Wythoff sequence
- Print N terms of Withoff Sequence
- Print the two possible permutations from a given sequence
- Print the sequence of size N in which every term is sum of previous K terms
- Program to find sum of the given sequence
- Program to find Nth term of the Van Eck's Sequence
- Count Possible Decodings of a given Digit Sequence
- Look-and-Say Sequence
- Farey Sequence
- Complete the sequence generated by a polynomial
- Juggler Sequence
- Padovan Sequence
- Aliquot Sequence
- Recaman's sequence
- Thue-Morse sequence
- Sum of the sequence 2, 22, 222, .........
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

