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 4
In this tutorial, we will learn to count the total number of rotations divisible by 4 for the given number.
Problem statement ? We have given a number value. We need to rotate numbers in clockwise or anti-clockwise directions and count the total number of rotations divisible by 4.
Here, we will learn two different approaches to counting rotations divisible by 4.
Understanding Number Rotation
When we rotate a number, we move digits from one position to another. For example, rotating 1234 clockwise gives us: 4123, 3412, 2341, and back to 1234. Each rotation creates a new number that we can test for divisibility by 4.
Method 1: Rotate the Number and Check Divisibility
In this approach, we convert the number to a string first. We can make n rotations for the string of length n. We remove the first character of the string and add it at the last of the string. After that, we check if the new number generated by rotation is divisible by 4.
Syntax
for (let i = 0; iIn the above syntax, the parseInt() method converts the string to a number, and the substring() method rotates the string.
Algorithm
Step 1 ? Use the toString() method and convert the number to the string.
Step 2 ? Use the for loop to make total 'n' rotations for the string of the length 'n'.
Step 3 ? Use the parseInt() method to convert the string to a number and check whether the number is divisible by 4. If the number is divisible by 4, increase the value of the count variable by 1.
Step 4 ? Use the substring() method to get the substring from 1st index. Furthermore, append the string's first character at the end of the substring. This way, we can rotate the string and generate a new number.
Example
In the example below, we have defined the countRotations() function, which implements the above algorithm and returns the total number of rotations divisible by 4.
<html>
<body>
<h3>Program to find the total number of rotations divisible by 4</h3>
<div id="output"></div>
<script>
let output = document.getElementById('output');
// JavaScript program to find the total count of rotations divisible by 4
let countRotations = (number) => {
let numStr = number.toString();
let len = numStr.length;
let count = 0;
// Loop to traverse the string
for (let i = 0; i < len; i++) {
// Check if the string is divisible by 4
if (parseInt(numStr) % 4 == 0) {
count++;
}
// Rotate the string
numStr = numStr.substring(1, len) + numStr[0];
}
return count;
}
let number = 1234;
output.innerHTML = "Total count of rotations divisible by 4 of " + number + " is " + countRotations(number);
</script>
</body>
</html>
Total count of rotations divisible by 4 of 1234 is 2
Method 2: Check Every Pair of 2 Digits for Divisibility by 4
If the last 2 digits of any number are divisible by 4, then the entire number is divisible by 4. While rotating the number, every pair of two digits comes at the end of the number. So, if any pair of two digits is divisible by 4, we can say one rotation related to that pair is divisible by 4.
Syntax
let lastDigit = num % 10;
num = Math.floor(num / 10);
let secondLastDigit = num % 10;
if ((secondLastDigit * 10 + lastDigit) % 4 == 0) {
count++;
}
In the above syntax, we fetch the last and second last digit from the number. After that, we create a number of two digits using both and check if it is divisible by 4.
Algorithm
Step 1 ? If the number is of a single digit, check if it is divisible by 4. If yes, return 1; else, return 0.
Step 2 ? If the number contains two or more digits, initialize the 'count' variable with 0.
Step 3 ? Create a pair using the last digit and first digit of the number. Get the last digit using the modulo operator and the first using Math.log() method.
Step 4 ? Multiply the first digit by 10 and add the last digit. Check if the result is divisible by 4. If yes, increase the count by 1.
Step 5 ? Use the while loop to check for other pairs of adjacent digits. Get consecutive digit pairs and check if each pair is divisible by 4.
Example
In this example, the countRotations() function counts the number of pairs of two digits divisible by 4.
<html>
<body>
<h3>Program to find the total number of rotations divisible by 4</h3>
<div id="output"></div>
<script>
let output = document.getElementById('output');
function countRotations(number) {
// If single digit, check if divisible by 4
if (number < 10) {
return number % 4 == 0 ? 1 : 0;
} else {
let count = 0;
let num = number;
// Check for the last digit and the first digit
let lastDigit = number % 10;
// Get the first digit from the number
let firstDigit = Math.floor(number / Math.pow(10, Math.floor(Math.log10(number))));
// If the pair of last and first digit is divisible by 4
if ((firstDigit * 10 + lastDigit) % 4 == 0) {
count++;
}
// Check all consecutive digit pairs
while (num > 9) {
let lastDigit = num % 10;
num = Math.floor(num / 10);
let secondLastDigit = num % 10;
if ((secondLastDigit * 10 + lastDigit) % 4 == 0) {
count++;
}
}
return count;
}
}
let number = 1234;
output.innerHTML = "Total count of rotations divisible by 4 of " + number + " is " + countRotations(number);
</script>
</body>
</html>
Total count of rotations divisible by 4 of 1234 is 2
Comparison
| Method | Time Complexity | Approach |
|---|---|---|
| String Rotation | O(n²) | Generate all rotations and check each |
| Digit Pair Analysis | O(n) | Check digit pairs for divisibility rule |
Conclusion
Both methods solve the rotation divisibility problem effectively. The string rotation method is straightforward but less efficient, while the digit pair method leverages the divisibility rule of 4 for better performance. Choose based on your requirements for simplicity versus efficiency.
