Divisible By A Part Of Me?

Count the number of 3-digit positive integers such that it is divisible by all of its digits.

For example, 132 is such a number because it is divisible by its digits 1, 3 and 2.

By default, it is impossible to divisible by 0, so the 3-digit integer can't have a digit 0.


The answer is 56.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

5 solutions

Rushikesh Jogdand
May 15, 2016

The catch is that divisibility by 0 0 is undefined. So, define it!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def div(num,dig):
    if dig==0:
        return False
    elif num%dig==0:
        return True
    return False

count=0

for i in range(100,1000):
    if div(i,i%10) and div(i,(i//10)%10) and div(i//100):
        print(i)
        count+=1

There's actually a one-liner python solution to this problem.

1
print(len([i for i in range(100,1000) if ('0' not in str(i) and all(i%int(x)==0 for x in str(i)))]))

This will give you the answer 56 56 to this problem.

In case we want to print out those numbers too, we can modify it as,

1
2
3
4
5
count=0
for item in (i for i in range(100,1000) if ('0' not in str(i) and all(i%int(x)==0 for x in str(i)))):
    count+=1
    print(item)
print('\nAnswer =',count)

Prasun Biswas - 5 years, 1 month ago

you may use also ZeroDivisionError to catch that exception

Vincent Miller Moral - 4 years, 12 months ago
Yuriy Kazakov
Jun 3, 2018
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import math
a=[1,2,3,4,5,6,7,8,9]
w=0
n=0
for i in a:
   for j in a:
     for k in a:
         s=i+j*10+k*100
         n=n+1
         if (math.fmod(s,k)+1)*(math.fmod(s,j)+1)*(math.fmod(s,i)+1)==1:
           w=w+1
           print(s,w)
print (w,n) 

Xuming Liang
Jun 11, 2016

My code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
def self_digit_divisible(n):
    nstring=str(n)
    for i in range(len(str(n))):
        if int(nstring[i])==0:
            return 0
        elif n%int(str(n)[i]) != 0:
            return 0
    else:
        return 1

counting=0

for n in range(100,1000):
    if self_digit_divisible(n)==1:
        counting+=1

print(counting)

Moderator note:

Standard solution. Can you think of a way to cut down on code in writing your function?

Viki Zeta
Jun 3, 2016
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
'POSSIBILITY ESTIMATOR'

nos = range(100, 1000)
possible = list()

for i in nos:
    a = list(map(int, list(str(i))))
    div = True
    if 0 in a:
        continue
    for j in a:
        if i % j != 0:
            div = False
            break

    if div : 
        possible.append(i)

print len(possible)

Simple, that aids 56

Mehdi K.
May 23, 2016

Python

1
2
3
4
5
>>> from math import gcd
>>> def lcm(a,b): return a*b//gcd(a,b)
... 
>>> sum(1 for i in range(1, 10) for j in range(1, 10) for k in range(1, 10) if (100 * i + 10 * j + k) % lcm(i, lcm(j, k)) == 0)
56

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...