Home > Web Front-end > JS Tutorial > JavaScript program to calculate number of rotations divisible by 8

JavaScript program to calculate number of rotations divisible by 8

PHPz
Release: 2023-09-03 10:05:08
forward
1345 people have browsed it

JavaScript 程序计算可被 8 整除的旋转次数

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.

Rotate the number and check if the rotation is 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.

grammar

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++;
   }
}
Copy after login

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.

algorithm

  • 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.

Example 1

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.

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 8 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      const rotationsDivisibleBy8 = (number) => {
         let count = 0;
         //Count rotations divisible by 8 by rotating numbers
         let str = number.toString();
         for (let i = 0; i < str.length; i++) {
         
            //Get the last character of the string
            let lastDigit = str[str.length - 1];
            
            // rotating number
            str = lastDigit + str.substring(0, str.length - 1);
            
            // convert string to integer
            let num = parseInt(str);
            
            //Check if num is divisible by 8
            if (num % 8 == 0) {
               count++;
            }
         }
         return count;
      }
      let number = 90645232432;
      output.innerHTML = "Total count of rotations divisible by 8 of " + number + " is " + rotationsDivisibleBy8(number);
   </script>
</body>
</html>
Copy after login

Check whether the three-digit number is 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.

grammar

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++;
   }
}
Copy after login

In the above syntax, we use the substring() method to get the three-digit pair.

algorithm

  • 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 .

Example 2

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.

<html>
<body>
   <h3> Program to find the total number of rotations divisible by 8 </h3>
   <div id = "output"> </div>
   <script>
      let output = document.getElementById('output');
      const rotationsDivisibleBy8 = (number) => {
         let count = 0;
         let numStr = number.toString();
         let n = numStr.length;
         if (n == 1) {
            // for 1 digit
            return number % 8 == 0 ? 1 : 0;
         }
         else if (n == 2) {
         
            // for 2 digits
            if (number % 8 == 0) {
               count++;
            }
            let temp = numStr.substring(1, 2) + numStr.substring(0, 1);
            if (temp % 8 == 0) {
               count++;
            }
            return count;
         }
         else {
         
            // for 3 digits
            for (let i = 0; i < n - 2; i++) {
               let pairOf3 = numStr.substring(i, i + 3);
               if (pairOf3 % 8 == 0) {
                  count++;
               }
            }
            
            // for last two and first digit
            let lastTwo = numStr.substring(n - 2, n);
            let firstDigit = numStr.substring(0, 1);
            let lastTwoFirstDigit = lastTwo + firstDigit;
            if (lastTwoFirstDigit % 8 == 0) {
               count++;
            }
            
            // for last digit and first two digits
            let lastDigit = numStr.substring(n - 1, n);
            let firstTwo = numStr.substring(0, 2);
            let lastDigitFirstTwo = lastDigit + firstTwo;
            if (lastDigitFirstTwo % 8 == 0) {
               count++;
            }
            return count;
         }
      }
      let number = 104104104104104;
      output.innerHTML = "Total count of rotations divisible by 8 of " + number + " is " + rotationsDivisibleBy8(number);
   </script>
</body>
</html>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template