Skip to content
Related Articles

Related Articles

Lynch-Bell Numbers
  • Last Updated : 23 Mar, 2021
GeeksforGeeks - Summer Carnival Banner

Lynch-Bell Number is a number N if its digits are all distinct and N is divisible by each of it’s digit.
 

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

Given a number N, the task is to check if N is an Lynch-Bell Number or not. If N is an Lynch-Bell Number then print “Yes” else print “No”.
Examples: 
 

Input: N = 384 
Output: Yes 
Explanation: 
384/3 = 128, 384/8 = 48, 384/4 = 96 
and 384 has all distinct digits.
Input: N = 1123 
Output: No 
Explanation: 
 

Approach:
 



  1. The idea is to traverse through every digit of given number and mark the traversed digit as visited. Since total number of digits is 10, we need a boolean array of size only 10 to mark visited digits.
  2. We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128 % d == 0 for d = 1, 2, 8. To do that, we need to iterate over each digit of the number.
  3. Then if its digits are all distinct and N is divisible by each of it’s digit, then print “Yes” else print “No”.

Below is the implementation of the above approach:
 

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>
Output: 
Yes

 

Time Complexity: O(n) 
Reference: http://www.numbersaplenty.com/set/Lynch-Bell_number/
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :