【Python 123】Number of daffodils and roses

Three daffodils

description

"Daffodil number" refers to a three-digit integer, the sum of the 3rd power of each digit is equal to the number itself. To
For example: ABC is A "3-digit daffodil number", then: A to the 3rd power + B to the 3rd power + C to the 3rd power = ABC. To To
Please ascending order of all the 3-bit output daffodils, use "comma" separated Output the result.

Input and output example

Indicates format, not right or wrong

enter Output
No input 111,222

Python code

j = 0
for i in range(100, 1000, 1):
    a = i // 100
    b = (i // 10) % 10
    c = i % 10
    sum = pow(a, 3) + pow(b, 3) + pow(c, 3)
    if i == sum:
        if j == 0:
            print(i, end="")
            j = j + 1
        else:
            print(",{}".format(i), end="")
            j = j + 1

result

153 , 370 , 371 , 407 153,370,371,407 153,370,371,407

Four-digit rose

description

The four-digit rose number is a power of four digits. Self-power refers to an n-digit number, and the sum of the n-th power of the digits in each digit is equal to itself. To
For example: when n When it is 3, there is 1^3 + 5^3 + 3^3 = 153, 153 is a power number when n is 3. The power number of 3 digits is called the narcissus number. To
Please output of all 4 The four-digit rose number of digits, in ascending order, one line for each digit.

Input and output example

The output only shows the format, not right or wrong

enter Output
no 1111
2222
3333

Python code

for i in range(1000, 10000, 1):
    a = i // 1000
    b = (i // 100) % 10
    c = (i // 10) % 10
    d = i % 10
    sum = pow(a, 4) + pow(b, 4) + pow(c, 4) + pow(d, 4)
    if i == sum:
        print(i)

result

1634 1634 1634
8208 8208 8208
9474 9474 9474

Guess you like

Origin blog.csdn.net/weixin_43012724/article/details/103426837