JavaScript Program to Count Rotations Divisible by 8


Problem Statement − We have given a number. We need to rotate the number and need to find the total number of rotations divisible by 8.

Here, we will learn two different approaches to counting rotations divisible by 8.

Rotate the Number and Check if Rotation is Divisible by 8

The first approach is to rotate numbers and get every possible rotation one by one. Also, check if rotation is divisible by 8. If yes, add 1 to the count.

Syntax

Users can follow the syntax below to count rotations divisible by 8 by rotating the numbers.

for ( ) {
   str = lastDigit + str.substring(0, str.length - 1);
   let num = parseInt(str);
   if (num % 8 == 0) {
      count++;
   }
}

In the above syntax, we take the last digit of the number string and append it at the start of the string to rotate the number.

Algorithm

  • Step 1 − Initialize the count variable with 0, representing the zero counts initially.

  • Step 2 − Iterate through the number string using the for loop and make total rotations equal to the length of the number string.

  • Step 3 − In the for loop, get the last digit of the number string. Also, get the substring containing the first n-1 digits.

  • Step 4 − Append the last digit at the start of the substring to rotate the number string.

  • Step 5 − Use the parseInt() method to extract the number from the string.

  • Step 6 − Check if rotation is divisible by 8. If yes, increase the value of the count by 1.

  • Step 7 − Return the value of the count once we check all rotations using the for loop.

Example 1

In the example below, the rotationsDivisibleBy8() function takes a number as a parameter and returns the total number of rotations divisible by 8. Also, we first convert the number to a string using the toString() method and then implement the above algorithm to count 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>

Check if Pair of Three Digits is Divisible by 8

If the last three digits of any number are divisible by 8, we can say that the whole 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 yes, it means rotation containing that three-digit at the end is divisible by 8.

Syntax

Users can follow the syntax below to count rotations divisible by 8.

for ( ) {
   let pairOf3 = numStr.substring(i, i + 3);
   if (pairOf3 % 8 == 0) {
      count++;
   }
}

In the above syntax, we have used the substring() method to get the pair of three digits.

Algorithm

  • Step 1 − Use the toString() method to convert the number to a string.

  • Step 2 − If the length of the number is equal to 1, return 1 if the digit is divisible by 8; Otherwise, return 0.

  • Step 3 − If the length of the number is equal to 2, check how many rotations are divisible by 8 from both possible rotations and return count.

  • Step 4 − For the number with more than 3 digits, use the substring() method and extract the pair of three consecutive digits. After that, check if the pair is divisible by 8, and increase the value of the count.

  • Step 5 − Also, check for pairs containing the last two digits and first digit, last digit and first two digits, and increase the value of the ‘count’ accordingly.

Example 2

In the example below, we use the for loop and substring() method to get the n-2 pairs of three digits and check if it is divisible by 8. In the output, users can observe that the given number contains the total 5 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;
         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>

Users learned two different approaches to counting a total number of rotations divisible by 8. In the first approach, we get all possible rotations and check if it is divisible by 8. In the second approach, we use the property of a number to make it divisible by 8, the last three digits of the number that should be divisible by 8.

Updated on: 20-Apr-2023

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements