Lynch-Bell Numbers
Last Updated :
23 Mar, 2021
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: :
- 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.
- 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
bool checkDivisibility( int n, int digit)
{
return (digit != 0 && n % digit == 0);
}
bool isAllDigitsDivide( int n)
{
int temp = n;
while (temp > 0) {
int digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return false ;
temp /= 10;
}
return true ;
}
bool isAllDigitsDistinct( int n)
{
bool arr[10];
for ( int i = 0; i < 10; i++)
arr[i] = false ;
while (n > 0) {
int digit = n % 10;
if (arr[digit])
return false ;
arr[digit] = true ;
n = n / 10;
}
return true ;
}
bool isLynchBell( int n)
{
return isAllDigitsDivide(n) &&
isAllDigitsDistinct(n);
}
int main()
{
int N = 12;
if (isLynchBell(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
C
#include <stdio.h>
int checkDivisibility( int n, int digit)
{
return (digit != 0 && n % digit == 0);
}
int isAllDigitsDivide( int n)
{
int temp = n;
while (temp > 0)
{
int digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return 0;
temp /= 10;
}
return 1;
}
int isAllDigitsDistinct( int n)
{
int arr[10],i,digit;
for (i = 0; i < 10; i++)
arr[i] = 0;
while (n > 0)
{
digit = n % 10;
if (arr[digit])
return 0;
arr[digit] = 1;
n = n / 10;
}
return 1;
}
int isLynchBell( int n)
{
return isAllDigitsDivide(n) &&
isAllDigitsDistinct(n);
}
int main()
{
int N = 12;
if (isLynchBell(N))
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
class GFG{
static boolean checkDivisibility( int n,
int digit)
{
return (digit != 0 && n % digit == 0 );
}
static boolean isAllDigitsDivide( int n)
{
int temp = n;
while (temp > 0 )
{
int digit = temp % 10 ;
if (!(checkDivisibility(n, digit)))
return false ;
temp /= 10 ;
}
return true ;
}
static boolean isAllDigitsDistinct( int n)
{
boolean arr[] = new boolean [ 10 ];
while (n > 0 )
{
int digit = n % 10 ;
if (arr[digit])
return false ;
arr[digit] = true ;
n = n / 10 ;
}
return true ;
}
static boolean isLynchBell( int n)
{
return isAllDigitsDivide(n) &&
isAllDigitsDistinct(n);
}
public static void main(String[] args)
{
int N = 12 ;
if (isLynchBell(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
import math
def checkDivisibility(n, digit):
return ((digit ! = 0 ) and ((n % digit) = = 0 ))
def isAllDigitsDivide(n):
temp = n
while (temp > = 1 ):
digit = int (temp % 10 )
if (checkDivisibility(n, digit) = = False ):
return 0
temp = temp / 10
return 1
def isAllDigitsDistinct(n):
arr = [ 0 ] * 10
while (n > = 1 ):
digit = int (n % 10 )
if (arr[digit]):
return 0
arr[digit] = 1
n = int (n / 10 )
return 1
def isLynchBell(n):
return (isAllDigitsDivide(n) and
isAllDigitsDistinct(n))
if __name__ = = '__main__' :
N = 12
if isLynchBell(N):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool checkDivisibility( int n,
int digit)
{
return (digit != 0 && n % digit == 0);
}
static bool isAllDigitsDivide( int n)
{
int temp = n;
while (temp > 0)
{
int digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return false ;
temp /= 10;
}
return true ;
}
static bool isAllDigitsDistinct( int n)
{
bool []arr = new bool [10];
while (n > 0)
{
int digit = n % 10;
if (arr[digit])
return false ;
arr[digit] = true ;
n = n / 10;
}
return true ;
}
static bool isLynchBell( int n)
{
return isAllDigitsDivide(n) &&
isAllDigitsDistinct(n);
}
public static void Main()
{
int N = 12;
if (isLynchBell(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function checkDivisibility( n, digit) {
return (digit != 0 && n % digit == 0);
}
function isAllDigitsDivide( n) {
let temp = n;
while (temp > 0) {
let digit = temp % 10;
if (!(checkDivisibility(n, digit)))
return false ;
temp = parseInt(temp/10);
}
return true ;
}
function isAllDigitsDistinct( n) {
let arr = Array(10).fill(0);
while (n > 0) {
let digit = n % 10;
if (arr[digit])
return false ;
arr[digit] = true ;
n = parseInt(n / 10);
}
return true ;
}
function isLynchBell( n) {
return isAllDigitsDivide(n) && isAllDigitsDistinct(n);
}
let N = 12;
if (isLynchBell(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Reference: http://www.numbersaplenty.com/set/Lynch-Bell_number/
Share your thoughts in the comments
Please Login to comment...