Contar rotaciones divisible por 8

Dado un gran número positivo como string, cuente todas las rotaciones del número dado que son divisibles por 8.

Ejemplos: 

Input: 8
Output: 1

Input: 40
Output: 1
Rotation: 40 is divisible by 8
          04 is not divisible by 8

Input : 13502
Output : 0
No rotation is divisible by 8

Input : 43262488612
Output : 4

Enfoque: para números grandes, es difícil rotar y dividir cada número por 8. Por lo tanto, se usa la propiedad de ‘divisibilidad por 8’ que dice que un número es divisible por 8 si los últimos 3 dígitos del número son divisibles por 8. Aquí en realidad, no rotamos el número y verificamos la divisibilidad de los últimos 8 dígitos, sino que contamos una secuencia consecutiva de 3 dígitos (en forma circular) que son divisibles por 8.

Ilustración:  

Consider a number 928160
Its rotations are 928160, 092816, 609281, 
160928, 816092, 281609.
Now form consecutive sequence of 3-digits from 
the original number 928160 as mentioned in the 
approach. 
3-digit: (9, 2, 8), (2, 8, 1), (8, 1, 6), 
(1, 6, 0),(6, 0, 9), (0, 9, 2)
We can observe that the 3-digit number formed by 
the these sets, i.e., 928, 281, 816, 160, 609, 092, 
are present in the last 3 digits of some rotation.
Thus, checking divisibility of these 3-digit numbers
gives the required number of rotations. 

C++

// C++ program to count all rotations divisible
// by 8
#include <bits/stdc++.h>
using namespace std;
 
// function to count of all rotations divisible
// by 8
int countRotationsDivBy8(string n)
{
    int len = n.length();
    int count = 0;
 
    // For single digit number
    if (len == 1) {
        int oneDigit = n[0] - '0';
        if (oneDigit % 8 == 0)
            return 1;
        return 0;
    }
 
    // For two-digit numbers (considering all
    // pairs)
    if (len == 2) {
 
        // first pair
        int first = (n[0] - '0') * 10 + (n[1] - '0');
 
        // second pair
        int second = (n[1] - '0') * 10 + (n[0] - '0');
 
        if (first % 8 == 0)
            count++;
        if (second % 8 == 0)
            count++;
        return count;
    }
 
    // considering all three-digit sequences
    int threeDigit;
    for (int i = 0; i < (len - 2); i++) {
        threeDigit = (n[i] - '0') * 100 +
                     (n[i + 1] - '0') * 10 +
                     (n[i + 2] - '0');
        if (threeDigit % 8 == 0)
            count++;
    }
 
    // Considering the number formed by the
    // last digit and the first two digits
    threeDigit = (n[len - 1] - '0') * 100 +
                 (n[0] - '0') * 10 +
                 (n[1] - '0');
 
    if (threeDigit % 8 == 0)
        count++;
 
    // Considering the number formed by the last
    // two digits and the first digit
    threeDigit = (n[len - 2] - '0') * 100 +
                 (n[len - 1] - '0') * 10 +
                 (n[0] - '0');
    if (threeDigit % 8 == 0)
        count++;
 
    // required count of rotations
    return count;
}
 
// Driver program to test above
int main()
{
    string n = "43262488612";
    cout << "Rotations: "
         << countRotationsDivBy8(n);
    return 0;
}

Java

// Java program to count all
// rotations divisible by 8
import java.io.*;
 
class GFG
{
    // function to count of all
    // rotations divisible by 8
    static int countRotationsDivBy8(String n)
    {
        int len = n.length();
        int count = 0;
     
        // For single digit number
        if (len == 1) {
            int oneDigit = n.charAt(0) - '0';
            if (oneDigit % 8 == 0)
                return 1;
            return 0;
        }
     
        // For two-digit numbers
        // (considering all pairs)
        if (len == 2) {
     
            // first pair
            int first = (n.charAt(0) - '0') *
                        10 + (n.charAt(1) - '0');
     
            // second pair
            int second = (n.charAt(1) - '0') *
                         10 + (n.charAt(0) - '0');
     
            if (first % 8 == 0)
                count++;
            if (second % 8 == 0)
                count++;
            return count;
        }
     
        // considering all three-digit sequences
        int threeDigit;
        for (int i = 0; i < (len - 2); i++)
        {
            threeDigit = (n.charAt(i) - '0') * 100 +
                        (n.charAt(i + 1) - '0') * 10 +
                        (n.charAt(i + 2) - '0');
            if (threeDigit % 8 == 0)
                count++;
        }
     
        // Considering the number formed by the
        // last digit and the first two digits
        threeDigit = (n.charAt(len - 1) - '0') * 100 +
                    (n.charAt(0) - '0') * 10 +
                    (n.charAt(1) - '0');
     
        if (threeDigit % 8 == 0)
            count++;
     
        // Considering the number formed by the last
        // two digits and the first digit
        threeDigit = (n.charAt(len - 2) - '0') * 100 +
                    (n.charAt(len - 1) - '0') * 10 +
                    (n.charAt(0) - '0');
        if (threeDigit % 8 == 0)
            count++;
     
        // required count of rotations
        return count;
    }
     
    // Driver program
    public static void main (String[] args)
    {
        String n = "43262488612";
        System.out.println( "Rotations: "
                       +countRotationsDivBy8(n));
         
    }
}
 
// This code is contributed by vt_m.

Python3

# Python3 program to count all
# rotations divisible by 8
 
# function to count of all
# rotations divisible by 8
def countRotationsDivBy8(n):
    l = len(n)
    count = 0
 
    # For single digit number
    if (l == 1):
        oneDigit = int(n[0])
        if (oneDigit % 8 == 0):
            return 1
        return 0
 
    # For two-digit numbers
    # (considering all pairs)
    if (l == 2):
 
        # first pair
        first = int(n[0]) * 10 + int(n[1])
 
        # second pair
        second = int(n[1]) * 10 + int(n[0])
 
        if (first % 8 == 0):
            count+=1
        if (second % 8 == 0):
            count+=1
        return count
 
    # considering all
    # three-digit sequences
    threeDigit=0
    for i in range(0,(l - 2)):
        threeDigit = (int(n[i]) * 100 +
                     int(n[i + 1]) * 10 +
                     int(n[i + 2]))
        if (threeDigit % 8 == 0):
            count+=1
 
    # Considering the number
    # formed by the last digit
    # and the first two digits
    threeDigit = (int(n[l - 1]) * 100 +
                 int(n[0]) * 10 +
                 int(n[1]))
 
    if (threeDigit % 8 == 0):
        count+=1
 
    # Considering the number
    # formed by the last two
    # digits and the first digit
    threeDigit = (int(n[l - 2]) * 100 +
                 int(n[l - 1]) * 10 +
                 int(n[0]))
    if (threeDigit % 8 == 0):
        count+=1
 
    # required count
    # of rotations
    return count
 
 
# Driver Code
if __name__=='__main__':
    n = "43262488612"
    print("Rotations:",countRotationsDivBy8(n))
 
# This code is contributed by mits.

C#

// C# program to count all
// rotations divisible by 8
using System;
 
class GFG {
     
    // function to count of all
    // rotations divisible by 8
    static int countRotationsDivBy8(String n)
    {
        int len = n.Length;
        int count = 0;
     
        // For single digit number
        if (len == 1)
        {
            int oneDigit = n[0] - '0';
            if (oneDigit % 8 == 0)
                return 1;
            return 0;
        }
     
        // For two-digit numbers
        // (considering all pairs)
        if (len == 2)
        {
     
            // first pair
            int first = (n[0] - '0') *
                         10 + (n[1] - '0');
     
            // second pair
            int second = (n[1] - '0') *
                          10 + (n[0] - '0');
     
            if (first % 8 == 0)
                count++;
            if (second % 8 == 0)
                count++;
            return count;
        }
     
        // considering all three -
        // digit sequences
        int threeDigit;
        for (int i = 0; i < (len - 2); i++)
        {
            threeDigit = (n[i] - '0') * 100 +
                         (n[i + 1] - '0') * 10 +
                         (n[i + 2] - '0');
            if (threeDigit % 8 == 0)
                count++;
        }
     
        // Considering the number formed by the
        // last digit and the first two digits
        threeDigit = (n[len - 1] - '0') * 100 +
                     (n[0] - '0') * 10 +
                     (n[1] - '0');
     
        if (threeDigit % 8 == 0)
            count++;
     
        // Considering the number formed
        // by the last two digits and
        // the first digit
        threeDigit = (n[len - 2] - '0') * 100 +
                     (n[len - 1] - '0') * 10 +
                     (n[0] - '0');
        if (threeDigit % 8 == 0)
            count++;
     
        // required count of rotations
        return count;
    }
     
    // Driver Code
    public static void Main ()
    {
        String n = "43262488612";
        Console.Write("Rotations: "
                      +countRotationsDivBy8(n));
         
    }
}
 
// This code is contributed by Nitin Mittal.

PHP

<?php
// PHP program to count all
// rotations divisible by 8
 
// function to count of all
// rotations divisible by 8
function countRotationsDivBy8($n)
{
    $len = strlen($n);
    $count = 0;
 
    // For single digit number
    if ($len == 1)
    {
        $oneDigit = $n[0] - '0';
        if ($oneDigit % 8 == 0)
            return 1;
        return 0;
    }
 
    // For two-digit numbers
    // (considering all pairs)
    if ($len == 2)
    {
 
        // first pair
        $first = ($n[0] - '0') * 10 +
                 ($n[1] - '0');
 
        // second pair
        $second = ($n[1] - '0') * 10 +
                  ($n[0] - '0');
 
        if ($first % 8 == 0)
            $count++;
        if ($second % 8 == 0)
            $count++;
        return $count;
    }
 
    // considering all
    // three-digit sequences
    $threeDigit;
    for ($i = 0; $i < ($len - 2); $i++)
    {
        $threeDigit = ($n[$i] - '0') * 100 +
                      ($n[$i + 1] - '0') * 10 +
                      ($n[$i + 2] - '0');
        if ($threeDigit % 8 == 0)
            $count++;
    }
 
    // Considering the number
    // formed by the last digit
    // and the first two digits
    $threeDigit = ($n[$len - 1] - '0') * 100 +
                  ($n[0] - '0') * 10 +
                  ($n[1] - '0');
 
    if ($threeDigit % 8 == 0)
        $count++;
 
    // Considering the number
    // formed by the last two
    // digits and the first digit
    $threeDigit = ($n[$len - 2] - '0') * 100 +
                  ($n[$len - 1] - '0') * 10 +
                   ($n[0] - '0');
    if ($threeDigit % 8 == 0)
        $count++;
 
    // required count
    // of rotations
    return $count;
}
 
// Driver Code
$n = "43262488612";
echo "Rotations: " .
      countRotationsDivBy8($n);
 
// This code is contributed by mits.
?>

Javascript

<script>
 
// Javascript program to count all
// rotations divisible by 8
 
// Function to count of all
// rotations divisible by 8
function countRotationsDivBy8(n)
{
    let len = n.length;
    let count = 0;
 
    // For single digit number
    if (len == 1)
    {
        let oneDigit = n[0] - '0';
         
        if (oneDigit % 8 == 0)
            return 1;
             
        return 0;
    }
 
    // For two-digit numbers
    // (considering all pairs)
    if (len == 2)
    {
         
        // first pair
        let first = (n[0] - '0') * 10 +
                    (n[1] - '0');
 
        // second pair
        let second = (n[1] - '0') * 10 +
                     (n[0] - '0');
 
        if (first % 8 == 0)
            count++;
        if (second % 8 == 0)
            count++;
             
        return count;
    }
 
    // Considering all
    // three-digit sequences
    let threeDigit;
    for(let i = 0; i < (len - 2); i++)
    {
        threeDigit = (n[i] - '0') * 100 +
                     (n[i + 1] - '0') * 10 +
                     (n[i + 2] - '0');
                      
        if (threeDigit % 8 == 0)
            count++;
    }
 
    // Considering the number
    // formed by the last digit
    // and the first two digits
    threeDigit = (n[len - 1] - '0') * 100 +
                  (n[0] - '0') * 10 +
                  (n[1] - '0');
 
    if (threeDigit % 8 == 0)
        count++;
 
    // Considering the number
    // formed by the last two
    // digits and the first digit
    threeDigit = (n[len - 2] - '0') * 100 +
                 (n[len - 1] - '0') * 10 +
                 (n[0] - '0');
                  
    if (threeDigit % 8 == 0)
        count++;
 
    // Required count
    // of rotations
    return count;
}
 
// Driver Code
let n = "43262488612";
 
document.write("Rotations: " +
               countRotationsDivBy8(n));
 
// This code is contributed by _saurabh_jaiswal
     
</script>

Producción: 

Rotations: 4

Complejidad de tiempo: O(n), donde n es el número de dígitos en el número de entrada.
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por ayushjauhari14 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *