Program to print Collatz Sequence

22 Jun, 2022
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.
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++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    printCollatz(6);    return 0;}
// Java program to print// Collatz sequenceimport 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
# 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 codeprintCollatz(6) # This code is contributed# by vaibhav29498
// C# program to print Collatz sequenceusing 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)
<?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 codeprintCollatz(6); // This code is contributed// by ChitraNayal?>
<script>     // Javascript program to print Collatz sequence         function printCollatz(n)    {        // We simply follow steps        // while we do not reach 1        while (n != 1)        {            document.write(n + " ");             // If n is odd            if ((n & 1) != 0)                n = 3*n + 1;             // If even            else                n = parseInt(n/2, 10);        }         // Print 1 at the end        document.write(n);    }     printCollatz(6);  </script>
Output  
6 3 10 5 16 8 4 2 1
Time Complexity: O(log n) since n is halved in while loop  
Auxiliary Space: O(1) 
manishshaw1
jit_t
vaibhav29498
ukasp
divyeshrabadiya07
kumargaurav97520
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Find minimum number of coins that make a given value
Algorithm to solve Rubik's Cube
Minimum number of jumps to reach end
Modulo 10^9+7 (1000000007)
The Knight's tour problem | Backtracking-1