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 Count Rotations Divisible by 8
In this tutorial, we'll learn how to count the number of rotations of a given number that are divisible by 8. A rotation means moving digits from one end to another - for example, rotating 123 gives us 231, 312, and back to 123.
We'll explore two different approaches: the straightforward rotation method and an optimized approach using divisibility rules.
Understanding Number Rotation
When we rotate a number, we take the last digit and move it to the front. For example, rotating 1234 gives us these possibilities:
- 1234 (original)
- 4123 (moved 4 to front)
- 3412 (moved 3 to front)
- 2341 (moved 2 to front)
Method 1: Rotate and Check Each Number
The first approach generates all possible rotations and checks each one for divisibility by 8.
Algorithm
- Convert the number to a string for easier manipulation
- For each rotation, move the last digit to the front
- Convert back to number and check if divisible by 8
- Count all rotations that satisfy the condition
Example 1
<html>
<body>
<h3>Count Rotations Divisible by 8 - Method 1</h3>
<div id="output1"></div>
<script>
const rotationsDivisibleBy8Method1 = (number) => {
let count = 0;
let str = number.toString();
// Check all possible rotations
for (let i = 0; i < str.length; i++) {
// Get the last character and move to front
let lastDigit = str[str.length - 1];
str = lastDigit + str.substring(0, str.length - 1);
// Convert to number and check divisibility
let rotatedNum = parseInt(str);
if (rotatedNum % 8 === 0) {
count++;
}
}
return count;
}
let number = 3224;
let result = rotationsDivisibleBy8Method1(number);
document.getElementById('output1').innerHTML =
`Number: ${number}<br>Rotations divisible by 8: ${result}`;
</script>
</body>
</html>
Number: 3224 Rotations divisible by 8: 2
Method 2: Using Divisibility Rule for 8
A key mathematical insight: a number is divisible by 8 if its last three digits are divisible by 8. This allows us to check only three-digit combinations instead of full rotations.
Algorithm
- Handle special cases for 1-digit and 2-digit numbers
- For numbers with 3+ digits, check consecutive three-digit substrings
- Also check wraparound combinations (last digits + first digits)
- Count all combinations divisible by 8
Example 2
<html>
<body>
<h3>Count Rotations Divisible by 8 - Method 2 (Optimized)</h3>
<div id="output2"></div>
<script>
const rotationsDivisibleBy8Method2 = (number) => {
let count = 0;
let numStr = number.toString();
let n = numStr.length;
if (n === 1) {
return number % 8 === 0 ? 1 : 0;
} else if (n === 2) {
// Check original number
if (number % 8 === 0) count++;
// Check rotated number
let rotated = numStr[1] + numStr[0];
if (parseInt(rotated) % 8 === 0) count++;
return count;
} else {
// Check consecutive 3-digit combinations
for (let i = 0; i < n - 2; i++) {
let threeDigit = numStr.substring(i, i + 3);
if (parseInt(threeDigit) % 8 === 0) {
count++;
}
}
// Check last two digits + first digit
let lastTwoFirstOne = numStr.substring(n - 2) + numStr[0];
if (parseInt(lastTwoFirstOne) % 8 === 0) {
count++;
}
// Check last digit + first two digits
let lastOneFirstTwo = numStr[n - 1] + numStr.substring(0, 2);
if (parseInt(lastOneFirstTwo) % 8 === 0) {
count++;
}
return count;
}
}
let number = 1024;
let result = rotationsDivisibleBy8Method2(number);
document.getElementById('output2').innerHTML =
`Number: ${number}<br>Rotations divisible by 8: ${result}`;
</script>
</body>
</html>
Number: 1024 Rotations divisible by 8: 3
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Rotate and Check | O(n²) | O(n) | Small numbers, easy to understand |
| Divisibility Rule | O(n) | O(1) | Large numbers, better performance |
Example with Detailed Output
<html>
<body>
<h3>Detailed Example: Finding All Rotations</h3>
<div id="detailed"></div>
<script>
const findRotationsWithDetails = (number) => {
let count = 0;
let str = number.toString();
let rotations = [];
for (let i = 0; i < str.length; i++) {
let lastDigit = str[str.length - 1];
str = lastDigit + str.substring(0, str.length - 1);
let rotatedNum = parseInt(str);
let isDivisible = rotatedNum % 8 === 0;
rotations.push({
rotation: rotatedNum,
divisible: isDivisible
});
if (isDivisible) count++;
}
return { count, rotations };
}
let number = 1208;
let result = findRotationsWithDetails(number);
let output = `Original number: ${number}<br><br>`;
result.rotations.forEach((rot, index) => {
output += `Rotation ${index + 1}: ${rot.rotation} - `;
output += rot.divisible ? "? Divisible by 8" : "? Not divisible by 8";
output += "<br>";
});
output += `<br><strong>Total rotations divisible by 8: ${result.count}</strong>`;
document.getElementById('detailed').innerHTML = output;
</script>
</body>
</html>
Original number: 1208 Rotation 1: 8120 - ? Divisible by 8 Rotation 2: 0812 - ? Divisible by 8 Rotation 3: 2081 - ? Not divisible by 8 Rotation 4: 1208 - ? Divisible by 8 Total rotations divisible by 8: 3
Conclusion
We've explored two approaches to count rotations divisible by 8. The first method is straightforward but less efficient, while the second method leverages the mathematical property that only the last three digits determine divisibility by 8, making it more efficient for larger numbers.
