Programa para comprobar si un número es divisible por alguna de sus cifras

Dado un número entero N donde  1 \leq n \leq 10^{18}                . La tarea es verificar si el número no es divisible por ninguno de sus dígitos. Si el número N dado es divisible por cualquiera de sus dígitos, escriba «SÍ», de lo contrario, escriba «NO».

Ejemplos: 

Input : N = 5115
Output : YES
Explanation: 5115 is divisible by both 1 and 5.
So print YES.

Input : 27
Output : NO
Explanation: 27 is not divisible by 2 or 7

Planteamiento: La idea para resolver el problema es extraer los dígitos del número uno por uno y verificar si el número es divisible por alguno de sus dígitos. Si es divisible por cualquiera de sus dígitos, imprima SÍ; de lo contrario, imprima NO.

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

C++

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if given number is divisible
// by any of its digits
string isDivisible(long long int n)
{
    long long int temp = n;
 
    // check if any of digit divides n
    while (n) {
        int k = n % 10;
 
        // check if K divides N
        if (temp % k == 0)
            return "YES";
 
        n /= 10;
    }
 
    return "NO";
}
 
// Driver Code
int main()
{
    long long int n = 9876543;
 
    cout << isDivisible(n);
 
    return 0;
}

Java

// Java implementation of above approach
 
class GFG
{
 
    // Function to check if given number is divisible
    // by any of its digits
    static String isDivisible(int n)
    {
        int temp = n;
 
        // check if any of digit divides n
        while (n > 0)
        {
            int k = n % 10;
 
            // check if K divides N
            if (temp % k == 0)
            {
                return "YES";
            }
            n /= 10;
        }
 
        return "NO";
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 9876543;
        System.out.println(isDivisible(n));
    }
}
 
// This code is contributed by 29AjayKumar

Python3

# Python program implementation of above approach
 
# Function to check if given number is
# divisible by any of its digits
def isDivisible(n):
    temp = n
 
    # check if any of digit divides n
    while(n):
        k = n % 10
 
        # check if K divides N
        if(temp % k == 0):
            return "YES"
 
        n /= 10;
 
    # Number is not divisible by
    # any of digits
    return "NO"
 
# Driver Code
n = 9876543
print(isDivisible(n))
 
# This code is contributed by
# Sanjit_Prasad

C#

// C# implementation of above approach
using System;
 
class GFG
{
 
    // Function to check if given number is divisible
    // by any of its digits
    static String isDivisible(int n)
    {
        int temp = n;
 
        // check if any of digit divides n
        while (n > 0)
        {
            int k = n % 10;
 
            // check if K divides N
            if (temp % k == 0)
            {
                return "YES";
            }
            n /= 10;
        }
 
        return "NO";
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 9876543;
        Console.WriteLine(isDivisible(n));
    }
}
 
// This code is contributed by PrinciRaj1992

PHP

<?php
// PHP implementation of above approach
 
// Function to check if given number
// is divisible by any of its digits
function isDivisible($n)
{
    $temp = $n;
 
    // check if any of digit divides n
    while ($n)
    {
        $k = $n % 10;
 
        // check if K divides N
        if ($temp % $k == 0)
            return "YES";
 
        $n = floor($n / 10);
    }
 
    return "NO";
}
 
// Driver Code
$n = 9876543;
 
echo isDivisible($n);
 
// This code is contributed by Ryuga
?>

Javascript

<script>
 
// Javascript implementation of above approach
 
// Function to check if given number
// is divisible by any of its digits
function isDivisible(n)
{
    temp = n;
 
    // Check if any of digit divides n
    while (n)
    {
        k = n % 10;
 
        // Check if K divides N
        if (temp % k == 0)
            return "YES";
 
        n = Math.floor(n / 10);
    }
 
    return "NO";
}
 
// Driver Code
let n = 9876543;
 
document.write(isDivisible(n));
 
// This code is contributed by sravan kumar
 
</script>
Producción: 

YES

 

Complejidad de Tiempo : O(log(N)) 
Espacio Auxiliar : O(1), ya que no se ha requerido espacio extra.

Método #2: Usando una string:

  • Tenemos que convertir el número dado en string tomando una nueva variable.
  • Atraviesa la cuerda,
  • Convertir carácter a entero (dígito)
  • Verifique si el número es divisible por cualquiera de sus dígitos, luego imprima SÍ, de lo contrario, imprima NO.

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

C++

//C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
string getResult(int n)
 {
   
    // Converting integer to string
    string st = to_string(n);
     
    // Traversing the string
    for (int i = 0; i < st.length(); i++)
    {
       
        //find the actual digit
        int d = st[i] - 48;
        
      // If the number is divisible by
        // digits then return yes
        if(n % d == 0)
        {
           
            return "Yes";
        }
    }
   
    // If no digits are dividing the
    // number then return no
    return "No";
}
 
// Driver Code
int main()
{
int n = 9876543;
 
// passing this number to get result function
cout<<getResult(n);
 
}
 
// this code is contributed by SoumiikMondal

Java

// JAva implementation of above approach
import java.io.*;
 
class GFG{
static String getResult(int n)
 {
   
    // Converting integer to string
    String st = Integer.toString(n);
     
    // Traversing the string
    for (int i = 0; i < st.length(); i++)
    {
       
        //find the actual digit
        int d = st.charAt(i) - 48;
        
      // If the number is divisible by
        // digits then return yes
        if(n % d == 0)
        {
           
            return "Yes";
        }
    }
   
    // If no digits are dividing the
    // number then return no
    return "No";
}
 
// Driver Code
public static void main(String[] args)
    {
int n = 9876543;
 
// passing this number to get result function
System.out.println(getResult(n));
}
}
 
// this code is contributed by shivanisinghss2110

Python3

# Python implementation of above approach
def getResult(n):
   
    # Converting integer to string
    st = str(n)
     
    # Traversing the string
    for i in st:
       
        # If the number is divisible by
        # digits then return yes
        if(n % int(i) == 0):
            return 'Yes'
           
    # If no digits are dividing the
    # number then return no
    return 'No'
 
 
# Driver Code
n = 9876543
 
# passing this number to get result function
print(getResult(n))
# this code is contributed by vikkycirus

C#

// C# implementation of above approach
using System;
 
public class GFG{
static String getResult(int n)
 {
   
    // Converting integer to string
    string st = n.ToString();
     
    // Traversing the string
    for (int i = 0; i < st.Length; i++)
    {
       
        //find the actual digit
        int d = st[i] - 48;
        
      // If the number is divisible by
        // digits then return yes
        if(n % d == 0)
        {
           
            return "Yes";
        }
    }
   
    // If no digits are dividing the
    // number then return no
    return "No";
}
 
// Driver Code
public static void Main(String[] args)
{
   int n = 9876543;
 
   // passing this number to get result function
   Console.Write(getResult(n));
}
}
 
// this code is contributed by shivanisinghss2110

Javascript

<script>
 
// JavaScript implementation of above approach
 
function getResult(n)
{
    // Converting integer to string
    let st = n.toString();
      
    // Traversing the string
    for (let i = 0; i < st.length; i++)
    {
        
        //find the actual digit
        let d = st[i].charCodeAt(0) - 48;
         
      // If the number is divisible by
        // digits then return yes
        if(n % d == 0)
        {
            
            return "Yes";
        }
    }
    
    // If no digits are dividing the
    // number then return no
    return "No";
}
 
// Driver Code
let n = 9876543;
  
// passing this number to get result function
document.write(getResult(n));
 
// This code is contributed by unknown2108
 
</script>

Producción:

Yes

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(n)

Publicación traducida automáticamente

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