Cannot get desired results for sum of cubes of the digits of a number equal to the number

1

My goal for this program is to find the sum of cubes of the digits of a number equal to the number. They are 153, 370, 371, and 407. The following python 3.9 code is my attempt.

num = 1000
for a in range(1,num):
    sum = 0
    test = a
    while test>0:
        digit = test %10
        test //= 10
        cube = digit ** 3
        sum += cube
        if test == sum:
            print(test)

I instead get

1
The thread 'MainThread' (0x1) has exited with code 0 (0x0).
8
1
1
2
27
64
8
8
9
9
The program 'python.exe' has exited with code -1 (0xffffffff).

What can I do to get my desired results? Thank you all who helped in advance, I appreciate it.

python
asked on Stack Overflow Dec 2, 2020 by dankdud

2 Answers

0
Your code should be as below

num = 1000

for a in range(1,num):
    sum = 0
    test = a
    while test>0:
        digit = test %10
        test //= 10
        cube = digit ** 3
        sum += cube
        

    if a == sum:
        print(a)
answered on Stack Overflow Dec 2, 2020 by Joythi S
0
num = 1000
for a in range(1,num):
    sum = 0
    test = a
    while test>0:
        digit = test %10
        test //= 10
        cube = digit ** 3
        sum += cube
    
    if a == sum:
        print(test)
answered on Stack Overflow Dec 2, 2020 by dankdud

User contributions licensed under CC BY-SA 3.0