Open In App
Related Articles

Python Program for Check if all digits of a number divide it

Improve Article
Improve
Save Article
Save
Like Article
Like

Given a number n, find whether all digits of n divide it or not.
 Examples: 

Input : 128
Output : Yes
128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Input : 130
Output : No

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. 

python3




# Python 3 program to
# check the number is
# divisible by all
# digits are not.
 
# 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 allDigitsDivide( n) :
     
    temp = n
    while (temp > 0) :
         
        # Taking the digit of
        # the number into digit
        # var.
        digit = temp % 10
        if ((checkDivisibility(n, digit)) == False) :
            return False
 
        temp = temp // 10
     
    return True
 
# Driver function
n = 128
 
if (allDigitsDivide(n)) :
    print("Yes")
else :
    print("No" )
     
# This code is contributed by Nikita Tiwari.


Output

Yes

Time Complexity: O(log10n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Approach : Using str(),list(),map() and len() methods

Python3




# Python 3 program to check the number is
# divisible by all digits are not.
 
n = 128
x = str(n)
a = list(map(int, x))
c = 0
for i in a:
    if(n % i == 0):
        c += 1
if(c == len(a)):
    print("Yes")
else:
    print("No")


Output

Yes

Time Complexity: O(nlogn)

Auxiliary Space: O(n)
Please refer complete article on Check if all digits of a number divide it for more details!

Approach#3: Using math

This approach is to factorize the number and check if each factor divides the number. If any factor of the number is not a digit or does not divide the number, then the answer is ‘No’. Otherwise, the answer is ‘Yes’.

Algorithm

1. Take the input number as n.
2. Factorize the number into a list of prime factors.
3. Traverse each factor of the number.
4. Check if the factor is a digit and divides the number.
5. If any factor is not a digit or does not divide the number, return ‘No’.
6. Otherwise, return ‘Yes’.

Python3




from math import sqrt
 
def factorize(n):
    factors = []
    while n % 2 == 0:
        factors.append(2)
        n = n // 2
    for i in range(3, int(sqrt(n)) + 1, 2):
        while n % i == 0:
            factors.append(i)
            n = n // i
    if n > 2:
        factors.append(n)
    return factors
 
def check_divide(n):
    factors = factorize(n)
    for factor in factors:
        if factor < 1 or factor > 9 or n % factor != 0:
            return 'No'
    return 'Yes'
n=128
print(check_divide(n))


Output

Yes

Time Complexity: O(sqrt(n) log n), where n is the input number.
Auxiliary Space: O(sqrt(n)), where n is the input number.


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials