Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python3 Program to Rotate digits of a given number by K
Next article icon

Python3 Program to Generate all rotations of a number

Last Updated : 05 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.
Examples: 
 

Input: n = 123 
Output: 231 312
Input: n = 1445 
Output: 4451 4514 5144 
 


 


Approach: 
 

  • Assume n = 123.
  • Multiply n with 10 i.e. n = n * 10 = 1230.
  • Add the first digit to the resultant number i.e. 1230 + 1 = 1231.
  • Subtract (first digit) * 10k from the resultant number where k is the number of digits in the original number (in this case, k = 3).
  • 1231 – 1000 = 231 is the left shift number of the original number.


Below is the implementation of the above approach: 
 

Python3
# Python3 implementation of the approach  # function to return the count of digit of n def numberofDigits(n):     cnt = 0     while n > 0:         cnt += 1         n //= 10     return cnt      # function to print the left shift numbers def cal(num):     digit = numberofDigits(num)     powTen = pow(10, digit - 1)          for i in range(digit - 1):                  firstDigit = num // powTen                  # formula to calculate left shift          # from previous number         left = (num * 10 + firstDigit -                 (firstDigit * powTen * 10))         print(left, end = " ")                  # Update the original number         num = left          # Driver code num = 1445 cal(num)  # This code is contributed # by Mohit Kumar 

Output
4451 4514 5144

Time Complexity: O(log10(num))
Auxiliary Space: O(1)

Please refer complete article on Generate all rotations of a number for more details!



Next Article
Python3 Program to Rotate digits of a given number by K
author
kartik
Improve
Article Tags :
  • DSA
  • Mathematical
  • Python
  • Python Programs
  • Akamai
  • number-digits
  • rotation
Practice Tags :
  • Mathematical
  • python

Similar Reads

  • Python3 Program to Rotate bits of a number
    Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at th
    3 min read
  • Python3 Program to Rotate digits of a given number by K
    INTRODUCTION:One important point to consider when working with the algorithm to rotate the digits of a given number by k positions is the time complexity. If we were to implement this algorithm using the approach shown in the previous example, the time complexity would be O(n), where n is the number
    4 min read
  • Python3 Program to Print all possible rotations of a given Array
    Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat
    3 min read
  • Python3 Program to Count rotations divisible by 4
    Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8Output: 1Input: 20Output: 1Rotation: 20 is divisible by 4 02 is not divisible by 4Input : 13502Output : 0No rotation is divisible by 4Input : 43292816Output : 55 rotations are
    2 min read
  • Python3 Program to Check if two numbers are bit rotations of each other or not
    Given two positive integers x and y, check if one integer is obtained by rotating bits of other. Input constraint: 0 < x, y < 2^32 Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.More info
    3 min read
  • Python3 Program to Count of rotations required to generate a sorted array
    Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: So
    4 min read
  • Python3 Program for Find element at given index after a number of rotations
    An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1Output : 3Explanation :
    6 min read
  • Python program to right rotate n-numbers by 1
    Given a number n. The task is to print n-integers n-times (starting from 1) and right rotate the integers by after each iteration.Examples: Input: 6 Output : 1 2 3 4 5 6 2 3 4 5 6 1 3 4 5 6 1 2 4 5 6 1 2 3 5 6 1 2 3 4 6 1 2 3 4 5 Input : 3 Output : 1 2 3 2 3 1 3 1 2 Method 1: Below is the implementa
    2 min read
  • Python3 Program for Count rotations divisible by 8
    Given a large positive number as string, count all rotations of the given number which are divisible by 8. Examples: Input: 8Output: 1Input: 40Output: 1Rotation: 40 is divisible by 8 04 is not divisible by 8Input : 13502Output : 0No rotation is divisible by 8Input : 43262488612Output : 4Approach: Fo
    3 min read
  • Python3 Program to Find Mth element after K Right Rotations of an Array
    [GFGTABS] Python3 # Python3 program to implement # the above approach # Function to return Mth element of # array after k right rotations def getFirstElement(a, N, K, M): # The array comes to original state # after N rotations K %= N # If K is greater or equal to M if (K >= M): # Mth element afte
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences