Comprobar si el número es divisible 43 o no

Dado un número N , la tarea es verificar si el número es divisible por 43 o no. 
Ejemplos: 
 

Entrada: N = 2795 
Salida: sí 
Explicación: 
43 * 65 = 2795
Entrada: N = 11094 
Salida: sí 
Explicación: 
43 * 258 = 11094 
 

Enfoque: La prueba de divisibilidad de 43 es: 
 

  1. Extraiga el último dígito.
  2. Agregue 13 * último dígito del número restante obtenido después de eliminar el último dígito.
  3. Repita los pasos anteriores hasta obtener un número de dos dígitos, o cero.
  4. Si el número de dos dígitos es divisible por 43, o es 0, entonces el número original también es divisible por 43.

Por ejemplo: 
 

If N = 11739

Step 1:
  N = 11739
  Last digit = 9
  Remaining number = 1173
  Adding 13 times last digit
  Resultant number = 1173 + 13*9 = 1290

Step 2:
  N = 1290
  Since 129 is divisible by 43 as 43 * 3 = 129

Therefore N = 11739 is also divisible by 43

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to check whether a number
// is divisible by 43 or not
 
#include<bits/stdc++.h>
#include<stdlib.h>
 
using namespace std;
// Function to check if the number is  divisible by 43 or not
bool isDivisible(int n) 
{
    int d;
    // While there are at least two digits
    while (n / 100)
    {
  
        // Extracting the last
        d = n % 10;
  
        // Truncating the number
        n /= 10;
  
        // adding thirteen times the last
        // digit to the remaining number
        n = abs(n+(d * 13));
    }
    // Finally return if the two-digit
    // number is divisible by 43 or not
    return (n % 43 == 0) ;
}
 
// Driver Code
int main() {
    int N = 2795;
  
    if (isDivisible(N))
        cout<<"Yes"<<endl ;
    else
        cout<<"No"<<endl ;
    
     return 0;    
}   
 
// This code is contributed by ANKITKUMAR34

Java

// Java program to check whether a number
// is divisible by 43 or not
class GFG
{
 
// Function to check if the number is  divisible by 43 or not
static boolean isDivisible(int n) 
{
    int d;
    // While there are at least two digits
    while ((n / 100) > 0)
    {
   
        // Extracting the last
        d = n % 10;
   
        // Truncating the number
        n /= 10;
   
        // adding thirteen times the last
        // digit to the remaining number
        n = Math.abs(n+(d * 13));
    }
    // Finally return if the two-digit
    // number is divisible by 43 or not
    return (n % 43 == 0) ;
}
  
// Driver Code
public static void main(String[] args) {
    int N = 2795;
   
    if (isDivisible(N))
        System.out.print("Yes");
    else
        System.out.print("No");
     
 }    
}   
  
// This code is contributed by PrinciRaj1992

Python 3

# Python program to check whether a number
# is divisible by 43 or not
 
# Function to check if the number is
# divisible by 43 or not
def isDivisible(n) :
 
    # While there are at least two digits
    while n // 100 :
 
        # Extracting the last
        d = n % 10
 
        # Truncating the number
        n //= 10
 
        # Adding thirteen  times the last
        # digit to the remaining number
        n = abs(n+(d * 13))
 
    # Finally return if the two-digit
    # number is divisible by 43 or not
    return (n % 43 == 0)
 
# Driver Code
if __name__ == "__main__" :
     
    N = 2795
 
    if (isDivisible(N)):
        print("Yes")
    else :
        print("No")

C#

// C# program to check whether a number
// is divisible by 43 or not
using System;
         
class GFG
{
     
// Function to check if the number is divisible by 43 or not
static bool isDivisible(int n)
{
    int d;
     
    // While there are at least two digits
    while (n / 100 > 0)
    {
 
        // Extracting the last
        d = n % 10;
 
        // Truncating the number
        n /= 10;
 
        // adding thirteen times the last
        // digit to the remaining number
        n = Math.Abs(n + (d * 13));
    }
     
    // Finally return if the two-digit
    // number is divisible by 43 or not
    return (n % 43 == 0) ;
}
 
// Driver Code
public static void Main()
{
    int N = 2795;
 
    if (isDivisible(N))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");    
}
}
 
// This code is contributed by AbhiThakur

Javascript

<script>
//javascript program to check whether a number
// is divisible by 43 or not
 
// Function to check if the number is divisible by 43 or not
function isDivisible(n)
{
    let d;
    // While there are at least two digits
    while(parseInt(n/100) > 0)
    {
 
        // Extracting the last
        d = n % 10;
 
        // Truncating the number
    n = parseInt(n / 10)
 
        // adding thirteen times the last
        // digit to the remaining number
        n = Math.abs(n+(d * 13));
    }
    // Finally return if the two-digit
    // number is divisible by 43 or not
    return (n % 43 == 0) ;
}
 
// Driver Code
    let N = 2795;
 
    if (isDivisible(N))
        document.write("Yes");
    else
        document.write("No");
 
// This code is contributed by vaibhavrabadiya117.
</script>
Producción: 

Yes

 

Complejidad del tiempo: O(log 10 N )

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por virusbuddha 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 *