Generate all cyclic permutations of a number

Given a number N, our task is to generate all the possible cyclic permutations of the number.

A cyclic permutation shifts all the elements of a set by a fixed offset. For a set with elements a_0, a_1, …, a_n, a cyclic permutation of one place to the left would yield a_1, …, a_n, a_0, and a cyclic permutation of one place to the right would yield a_n, a_0, a_1, ….

Examples:

Input :  123
Output : 123
         312
         231

Input :  5674
Output : 5674
         4567
         7456
         6745

The idea is to generate next permutation of a number using below formula.

    rem = num % 10;
    div = num / 10;
    num = (pow(10, n - 1)) * rem + div;

While repeating above steps, if we come back to original number, we stop and return.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to generate all cyclic permutations
// of number
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the total number of digits 
// in a number.
int countdigits(int N)
{
    int count = 0;
    while (N) {
        count++;
        N = N / 10;
    }
    return count;
}
  
// Function to generate all cyclic permutations
// of a number
void cyclic(int N)
{
    int num = N;
    int n = countdigits(N);
  
    while (1) {
        cout << num << endl;
  
        // Following three lines generates a
        // circular pirmutation of a number.
        int rem = num % 10;
        int div = num / 10;
        num = (pow(10, n - 1)) * rem + div;
  
        // If all the permutations are checked
        // and we obtain original number exit
        // from loop.
        if (num == N) 
            break;        
    }
}
  
// Driver Program
int main()
{
    int N = 5674;
    cyclic(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to generate all 
// cyclic permutations of number
class GFG
{
  
    // Function to count the total number
    // of digits in a number.
    static int countdigits(int N)
    {
        int count = 0;
        while (N>0) {
            count++;
            N = N / 10;
        }
        return count;
    }
  
    // Function to generate all cyclic
    // permutations of a number
    static void cyclic(int N)
    {
        int num = N;
        int n = countdigits(N);
  
        while (true) {
            System.out.println(num); 
  
            // Following three lines generates a
            // circular pirmutation of a number.
            int rem = num % 10;
            int dev = num / 10;
            num = (int)((Math.pow(10, n - 1)) *
                                rem + dev);
  
            // If all the permutations are 
            // checked and we obtain original
            // number exit from loop.
            if (num == N) 
                break
        }
    }
  
    // Driver Program
    public static void main (String[] args) {
    int N = 5674;
    cyclic(N);
    }
}
  
/* This code is contributed by Mr. Somesh Awasthi */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to 
# generate all cyclic
# permutations of number
import math
  
# Function to count the 
# total number of digits
# in a number.
def countdigits(N):
    count = 0;
    while (N):
        count = count + 1;
        N = int(math.floor(N / 10));
    return count;
      
# Function to generate 
# all cyclic permutations
# of a number
def cyclic(N):
    num = N;
    n = countdigits(N);
    while (1):
        print(int(num));
          
        # Following three lines 
        # generates a circular 
        # permutation of a number.
        rem = num % 10;
        div = math.floor(num / 10);
        num = ((math.pow(10, n - 1)) * 
                           rem + div);
          
        # If all the permutations 
        # are checked and we obtain 
        # original number exit from loop.
        if (num == N):
            break
              
# Driver Code
N = 5674;
cyclic(N);
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to generate all 
// cyclic permutations of number
using System;
  
class GFG
{
    // Function to count the total number
    // of digits in a number.
    static int countdigits(int N)
    {
        int count = 0;
        while (N > 0) {
            count++;
            N = N / 10;
        }
        return count;
    }
  
    // Function to generate all cyclic
    // permutations of a number
    static void cyclic(int N)
    {
        int num = N;
        int n = countdigits(N);
  
        while (true) {
            Console.WriteLine(num); 
  
            // Following three lines generates a
            // circular permutation of a number.
            int rem = num % 10;
            int dev = num / 10;
            num = (int)((Math.Pow(10, n - 1)) *
                                    rem + dev);
  
            // If all the permutations are 
            // checked and we obtain original
            // number exit from loop.
            if (num == N) 
                break
        }
    }
  
    // Driver Program
    public static void Main () 
    {
      int N = 5674;
      cyclic(N);
    }
}
  
// This code is contributed by nitin mittal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to generate all 
// cyclic permutations of number
  
// Function to count the total 
// number of digits in a number.
function countdigits($N)
{
    $count = 0;
    while ($N
    {
        $count++;
        $N = floor($N / 10);
    }
    return $count;
}
  
// Function to generate all 
// cyclic permutations of a number
function cyclic($N)
{
    $num = $N;
    $n = countdigits($N);
  
    while (1)
    {
        echo ($num);
        echo "\n" ;
          
        // Following three lines generates a
        // circular pirmutation of a number.
        $rem = $num % 10;
        $div = floor($num / 10);
        $num = (pow(10, $n - 1)) * $rem + $div;
  
        // If all the permutations are checked
        // and we obtain original number exit
        // from loop.
        if ($num == $N
            break;     
    }
}
  
    // Driver Code
    $N = 5674;
    cyclic($N);
  
// This code is contributed by nitin mittal
?>

chevron_right



Output:

 5674
 4567
 7456
 6745

This article is contributed by Vineet Joshi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal, Mithun Kumar