Problem Statement- We are given a number. We need to rotate this number and need to find the total number of rotations that is divisible by 8.
Here we will learn two different ways to calculate the number of rotations divisible by 8.
The first method is to rotate the number and get each possible rotation one by one. Also, check if the rotation is divisible by 8. If so, add 1 to the count.
Users can follow the syntax below to calculate the number of rotations divisible by 8 by rotating numbers.
for ( ) { str = lastDigit + str.substring(0, str.length - 1); let num = parseInt(str); if (num % 8 == 0) { count++; } }
In the above syntax, we get the last digit of the number string and append it to the beginning of the string to rotate the number.
Step 1- Initialize the count variable to 0, indicating an initial count of zero.
Step 2- Use a for loop to iterate over the number string and make the total rotation equal to the length of the number string.
Step 3- In the for loop, get the last digit of the numeric string. Also, get the substring containing the first n-1 digits.
Step 4- Append the last digit at the beginning of the substring to rotate the numeric string.
Step 5- Extract numbers from the string using parseInt() method.
Step 6- Check if the rotation is divisible by 8. If so, increase the count by 1.
Step 7- Once we check all the rotations using a for loop, return the count value.
In the following example, the rotationsDivisibleBy8() function accepts a number as a parameter and returns the total number of rotations divisible by 8. Additionally, we first convert the number to a string using the toString() method and then implement the above algorithm to calculate the number of rotations divisible by 8.
Program to find the total number of rotations divisible by 8
If the last three digits of any number are divisible by 8, we can say that the entire number is divisible by 8. So, here we can take a pair of three consecutive numbers and check if the pair is divisible by 8. If it is, it means that the rotation containing that three-digit number at the end is divisible by 8.
Users can follow the following syntax to calculate the number of rotations divisible by 8.
for ( ) { let pairOf3 = numStr.substring(i, i + 3); if (pairOf3 % 8 == 0) { count++; } }
In the above syntax, we use the substring() method to get the three-digit pair.
Step 1- Convert the number to a string using the toString() method.
Step 2- If the length of the number is equal to 1, return 1 if the number is divisible by 8; otherwise, return 0.
Step 3- If the length of the number is equal to 2, check the number of spins divisible by 8 from the possible spins and return count.
Step 4- For numbers with more than 3 digits, use substring() method and extract the pair of consecutive three digits. After that, check if the pair is divisible by 8 and increment the count value.
Step 5- Additionally, check for pairs containing the last two digits and the first digit, the last digit and the first two digits, and increment the value of "count" accordingly .
In the following example, we use for loop and substring() method to get n-2 pairs of three-digit numbers and check if it is divisible by 8. In the output, the user can observe that the given number contains a total of 5 spins divisible by 8.
Program to find the total number of rotations divisible by 8
The user learned two different ways to calculate the total number of spins divisible by 8. In the first method, we take all possible rotations and check if it is divisible by 8. In the second method, we use the properties of a number that make it divisible by 8, that is, the last three digits of a number that are divisible by 8.
The above is the detailed content of JavaScript program to calculate number of rotations divisible by 8. For more information, please follow other related articles on the PHP Chinese website!