Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript program to generate all rotations of a number
In this article, we will learn to generate all rotations of a number in JavaScript. A rotation involves moving the first digit to the end of the number, creating all possible circular arrangements of the digits.
Problem Statement
Given a number, we need to generate all possible rotations of its digits. A rotation is defined as moving the first digit to the end of the number.
Input:
123
All Rotations:
- 123 (original number)
- 231 (first rotation: move 1 to the end)
- 312 (second rotation: move 2 to the end)
The number of rotations for a number with n digits is equal to n.
Different Approaches
We will explore two different approaches to generate all rotations of a number:
Using String Manipulation
This approach converts the number into a string, performs character shifting operations, and collects all rotations.
Steps for string manipulation approach:
- Convert the number to a string to enable character manipulation
- Use a for loop to iterate through all possible rotations
- In each iteration, move the first character to the end using slice()
- Convert the rotated string back to a number and store it
Example
function generateRotations(num) {
let numStr = num.toString();
let rotations = [];
for (let i = 0; i < numStr.length; i++) {
numStr = numStr.slice(1) + numStr[0];
rotations.push(parseInt(numStr));
}
return rotations;
}
console.log(generateRotations(123));
console.log(generateRotations(4567));
[231, 312, 123] [5674, 6745, 7456, 4567]
Time Complexity: O(n²) where n is the number of digits, due to string slicing operations.
Space Complexity: O(n) for storing the rotations array.
Using Modular Arithmetic
This approach uses mathematical operations like division and modulus to manipulate digits without string conversion.
Steps for modular arithmetic approach:
- Calculate the number of digits and create a divisor using Math.pow()
- Extract the first digit using integer division
- Get remaining digits using modulus operation
- Form the new rotation by shifting digits mathematically
Example
function generateRotationsMath(num) {
let rotations = [];
let count = num.toString().length;
let divisor = Math.pow(10, count - 1);
for (let i = 0; i < count; i++) {
let firstDigit = Math.floor(num / divisor);
let remainingDigits = num % divisor;
num = remainingDigits * 10 + firstDigit;
rotations.push(num);
}
return rotations;
}
console.log(generateRotationsMath(123));
console.log(generateRotationsMath(4567));
[231, 312, 123] [5674, 6745, 7456, 4567]
Time Complexity: O(n) where n is the number of digits, using constant-time arithmetic operations.
Space Complexity: O(n) for storing the rotations array.
Comparison
| Approach | Time Complexity | Space Complexity | Performance |
|---|---|---|---|
| String Manipulation | O(n²) | O(n) | Slower due to string operations |
| Modular Arithmetic | O(n) | O(n) | Faster with mathematical operations |
Conclusion
Both methods successfully generate all rotations of a number. The modular arithmetic approach is more efficient with O(n) time complexity, while string manipulation offers simpler logic but has O(n²) complexity due to string operations.
