Python basic algorithm collection (11)-find the number of daffodils

The so-called daffodil number is a given random three-digit number, and the sum of the third power of each digit is equal to itself, such as:

Three digits 153=1^+5^3+3^3

The programming is required to realize the judgment of the number of daffodils within a given range.

The difficulty lies in decomposing three-digit single digits, tens digits and hundreds digits.

Let's use 153 as an example to decompose hundreds, tens and single digits

Decompose hundreds digits: 153//100=1

Decompose the ten digits: 153//10%10=5

Decompose single digits: 153%10=3

In fact, you only need to find a way to get the number to be decomposed to the ones digit, that is, point the decimal point forward. If you want to decompose the hundred digits, you must move 1 to the single digit, that is, point forward. 2 is 1.53, decomposing the tens digit. That is, the decimal point is one digit forward, that is, 15.3 and then the remainder is taken from 10. It is simple to decompose the single digit and take the remainder of 10. code show as below:

for n in range(0,100000000):
    i=n//100#得到百位数
    j=n//10%10#得到十位数
    k=n%10#得到个位数
    if n==i**3+j**3+k**3:
        print('水仙花数:{}'.format(n))

The implementation effect is as follows:

 

Guess you like

Origin blog.csdn.net/weixin_43115314/article/details/114230417