Números de Lynch Bell

El número de Lynch-Bell es un número N si sus dígitos son todos distintos y N es divisible por cada uno de sus dígitos.
 

1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 24… 
 

Dado un número N , la tarea es comprobar si N es un número de Lynch-Bell o no. Si N es un número de Lynch-Bell, escriba «Sí» , de lo contrario, escriba «No» .
Ejemplos: 
 

Entrada: N = 384 
Salida: Sí 
Explicación: 
384/3 = 128, 384/8 = 48, 384/4 = 96 
y 384 tiene todos los dígitos distintos.
Entrada: N = 1123 
Salida: No 
Explicación: 
 

Enfoque:
 

  1. La idea es recorrer cada dígito de un número dado y marcar el dígito recorrido como visitado. Dado que el número total de dígitos es 10, necesitamos una array booleana de tamaño solo 10 para marcar los dígitos visitados.
  2. Queremos probar si cada dígito es distinto de cero y divide el número. Por ejemplo, con 128, queremos probar d != 0 && 128 % d == 0 para d = 1, 2, 8. Para hacer eso, necesitamos iterar sobre cada dígito del número.
  3. Luego, si sus dígitos son todos distintos y N es divisible por cada uno de sus dígitos, entonces imprima «Sí»; de lo contrario, imprima «No» .

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

C++

// C++ implementation for the
// above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check the divisibility
// of the number by its digit.
bool checkDivisibility(int n, int digit)
{
    // If the digit divides the number
    // then return true else return false.
    return (digit != 0 && n % digit == 0);
}
 
// Function to check if all digits
// of n divide it or not
bool isAllDigitsDivide(int n)
{
    int temp = n;
    while (temp > 0) {
 
        // Taking the digit of the
        // number into digit var.
        int digit = temp % 10;
        if (!(checkDivisibility(n, digit)))
            return false;
 
        temp /= 10;
    }
    return true;
}
 
// Function to check if N
// has all distinct digits
bool isAllDigitsDistinct(int n)
{
    // Create an array of size 10 and initialize all
    // elements as false. This array is used to check
    // if a digit is already seen or not.
    bool arr[10];
    for (int i = 0; i < 10; i++)
        arr[i] = false;
 
    // Traverse through all digits of given number
    while (n > 0) {
        // Find the last digit
        int digit = n % 10;
 
        // If digit is already seen, return false
        if (arr[digit])
            return false;
 
        // Mark this digit as seen
        arr[digit] = true;
 
        // REmove the last digit from number
        n = n / 10;
    }
    return true;
}
 
// Function to check Lynch-Bell numbers
bool isLynchBell(int n)
{
    return isAllDigitsDivide(n) &&
           isAllDigitsDistinct(n);
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 12;
 
    // Function Call
    if (isLynchBell(N))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

C

// C implementation for the
// above approach
#include <stdio.h>
 
// Function to check the divisibility
// of the number by its digit.
int checkDivisibility(int n, int digit)
{
     
    // If the digit divides the number
    // then return true else return false.
    return (digit != 0 && n % digit == 0);
}
 
// Function to check if all digits
// of n divide it or not
int isAllDigitsDivide(int n)
{
    int temp = n;
    while (temp > 0)
    {
 
        // Taking the digit of the
        // number into digit var.
        int digit = temp % 10;
        if (!(checkDivisibility(n, digit)))
            return 0;
 
        temp /= 10;
    }
    return 1;
}
 
// Function to check if N
// has all distinct digits
int isAllDigitsDistinct(int n)
{
     
    // Create an array of size 10 and initialize all
    // elements as false. This array is used to check
    // if a digit is already seen or not.
    int arr[10],i,digit;
     
    for(i = 0; i < 10; i++)
        arr[i] = 0;
 
    // Traverse through all digits of given number
    while (n > 0)
    {
         
        // Find the last digit
        digit = n % 10;
 
        // If digit is already seen, return false
        if (arr[digit])
            return 0;
 
        // Mark this digit as seen
        arr[digit] = 1;
 
        // Remove the last digit from number
        n = n / 10;
    }
    return 1;
}
 
// Function to check Lynch-Bell numbers
int isLynchBell(int n)
{
    return isAllDigitsDivide(n) &&
           isAllDigitsDistinct(n);
}
 
// Driver Code
int main()
{
     
    // Given number N
    int N = 12;
 
    // Function call
    if (isLynchBell(N))
        printf("Yes");
    else
        printf("No");
    return 0;
}
 
// This code is contributed by adityakumar27200

Java

// Java program for above approach
class GFG{
 
// Function to check the divisibility
// of the number by its digit.
static boolean checkDivisibility(int n,
                                 int digit)
{
    // If the digit divides the number
    // then return true else return false.
    return (digit != 0 && n % digit == 0);
}
 
// Function to check if all digits
// of n divide it or not
static boolean isAllDigitsDivide(int n)
{
    int temp = n;
    while (temp > 0)
    {
 
        // Taking the digit of the
        // number into digit var.
        int digit = temp % 10;
        if (!(checkDivisibility(n, digit)))
            return false;
 
        temp /= 10;
    }
    return true;
}
 
// Function to check if N
// has all distinct digits
static boolean isAllDigitsDistinct(int n)
{
    // Create an array of size 10 and initialize all
    // elements as false. This array is used to check
    // if a digit is already seen or not.
    boolean arr[] = new boolean[10];
     
 
    // Traverse through all digits of given number
    while (n > 0)
    {
        // Find the last digit
        int digit = n % 10;
 
        // If digit is already seen, return false
        if (arr[digit])
            return false;
 
        // Mark this digit as seen
        arr[digit] = true;
 
        // REmove the last digit from number
        n = n / 10;
    }
    return true;
}
 
// Function to check Lynch-Bell numbers
static boolean isLynchBell(int n)
{
    return isAllDigitsDivide(n) &&
           isAllDigitsDistinct(n);
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Number N
    int N = 12;
 
    // Function Call
    if (isLynchBell(N))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Shubham Prakash

Python3

# Python3 implementation for the
# above approach
import math
 
# Function to check the divisibility
# of the number by its digit.
def checkDivisibility(n, digit):
     
    # If the digit divides the number
    # then return true else return false.
    return ((digit != 0) and ((n % digit) == 0))
 
# Function to check if all digits
# of n divide it or not
def isAllDigitsDivide(n):
     
    temp = n
    while (temp >= 1):
         
        # Taking the digit of the number
        # into digit var.
        digit = int(temp % 10)
        if (checkDivisibility(n, digit) == False):
            return 0
             
        temp = temp / 10
         
    return 1
     
# Function to check if N has all
# distinct digits
def isAllDigitsDistinct(n):
     
    # Create an array of size 10 and
    # initialize all elements as false.
    # This array is used to check if a
    # digit is already seen or not.
    arr = [0] * 10
     
    # Traverse through all digits
    # of given number
    while (n >= 1):
         
        # Find the last digit
        digit = int(n % 10)
         
        # If digit is already seen, return false
        if(arr[digit]):
            return 0
             
        # Mark this digit as seen
        arr[digit] = 1
         
        # Remove the last digit from number
        n = int(n / 10)
         
    return 1
     
# Function to check Lynch-Bell numbers
def isLynchBell(n):
     
    return (isAllDigitsDivide(n) and
            isAllDigitsDistinct(n))
 
# Driver Code
if __name__=='__main__':
     
    # Given number N
    N = 12
     
    # Function call
    if isLynchBell(N):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by adityakumar27200

C#

// C# program for above approach
using System;
class GFG{
 
// Function to check the divisibility
// of the number by its digit.
static bool checkDivisibility(int n,
                              int digit)
{
    // If the digit divides the number
    // then return true else return false.
    return (digit != 0 && n % digit == 0);
}
 
// Function to check if all digits
// of n divide it or not
static bool isAllDigitsDivide(int n)
{
    int temp = n;
    while (temp > 0)
    {
 
        // Taking the digit of the
        // number into digit var.
        int digit = temp % 10;
        if (!(checkDivisibility(n, digit)))
            return false;
 
        temp /= 10;
    }
    return true;
}
 
// Function to check if N
// has all distinct digits
static bool isAllDigitsDistinct(int n)
{
    // Create an array of size 10 and initialize all
    // elements as false. This array is used to check
    // if a digit is already seen or not.
    bool []arr = new bool[10];
     
 
    // Traverse through all digits of given number
    while (n > 0)
    {
        // Find the last digit
        int digit = n % 10;
 
        // If digit is already seen, return false
        if (arr[digit])
            return false;
 
        // Mark this digit as seen
        arr[digit] = true;
 
        // REmove the last digit from number
        n = n / 10;
    }
    return true;
}
 
// Function to check Lynch-Bell numbers
static bool isLynchBell(int n)
{
    return isAllDigitsDivide(n) &&
           isAllDigitsDistinct(n);
}
 
// Driver Code
public static void Main()
{
    // Given Number N
    int N = 12;
 
    // Function Call
    if (isLynchBell(N))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Code_Mech

Javascript

<script>
// Javascript program for above approach
 
 
    // Function to check the divisibility
    // of the number by its digit.
    function checkDivisibility( n, digit) {
        // If the digit divides the number
        // then return true else return false.
        return (digit != 0 && n % digit == 0);
    }
 
    // Function to check if all digits
    // of n divide it or not
    function isAllDigitsDivide( n) {
        let temp = n;
        while (temp > 0) {
 
            // Taking the digit of the
            // number into digit var.
            let digit = temp % 10;
            if (!(checkDivisibility(n, digit)))
                return false;
 
            temp = parseInt(temp/10);
        }
        return true;
    }
 
    // Function to check if N
    // has all distinct digits
    function isAllDigitsDistinct( n) {
        // Create an array of size 10 and initialize all
        // elements as false. This array is used to check
        // if a digit is already seen or not.
        let arr = Array(10).fill(0);
 
        // Traverse through all digits of given number
        while (n > 0) {
            // Find the last digit
            let digit = n % 10;
 
            // If digit is already seen, return false
            if (arr[digit])
                return false;
 
            // Mark this digit as seen
            arr[digit] = true;
 
            // REmove the last digit from number
            n = parseInt(n / 10);
        }
        return true;
    }
 
    // Function to check Lynch-Bell numbers
    function isLynchBell( n) {
        return isAllDigitsDivide(n) && isAllDigitsDistinct(n);
    }
 
    // Driver Code
      
        // Given Number N
        let N = 12;
 
        // Function Call
        if (isLynchBell(N))
            document.write("Yes");
        else
            document.write("No");
 
 
// This code contributed by gauravrajput1
 
</script>
Producción: 

Yes

 

Complejidad de tiempo: O(n)  
Referencia : http://www.numbersaplenty.com/set/Lynch-Bell_number/
 

Publicación traducida automáticamente

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