python – Get all numbers not exceeding a given number N that are divisible without a remainder by all their digits

Question:

I dug up this link on the Internet: https://clck.ru/JccbR It is written in C ++, I tried to translate it into Python, in proportion to my knowledge, I got:

def test(i):
    k = i
    while(k):
        d = k % 10
        k /= 10
        if (d and i % d):
            return False
    return True
n = int(input())
for i in range(1, n + 1):
    if (test(i)):
        print(i)

The program does not display anything, please tell me where I'm wrong?

Answer:

In Python 3.x:

k /= 10

Returns a floating point number.

Use integer division:

k //= 10

def test(i):
    k = i
    while(k):
        d = k % 10
        k //= 10
        if (d == 0 or i % d):
            return False
    return True

examination:

In [174]: for i in range(1, int(input()) + 1):
     ...:     if (test(i)):
     ...:         print(i)
     ...:
42

1
2
3
4
5
6
7
8
9
11
12
15
22
24
33
36
Scroll to Top