Python exercise question 009: daffodils, python009
[Python exercise question] print out all the "Daffodils". The so-called "Daffodils" refers to a three-digit number, each of which is equal to the number itself. For example, 153 is a "Daffodils" because 153 = the power of 1 + the power of 5 + the power of 3.
----------------------------------------------------------------------
This is also a question, as long as you can split up hundreds, dozens, and digits of any three digits. Idea: divide any three-digit number by 100 and then round down to get a hundred digits. Remove the 3-digit subtraction (100 bits X) and divide the number by 10 and then round down to get ten digits. Divide this three-digit number by 10 and then multiply it by 10 (get the number of hundreds or dozens of digits). Then, subtract the three-digit number to obtain the single-digit number. The Code is as follows:
Import mathfor I in range (100,100 0): x = math. floor (I/100) # obtain hundreds of digits y = math. floor (I-x * 100)/10) # obtain ten digits z = I-math. floor (I/10) * 10 # obtain the single digit if I = x ** 3 + y ** 3 + z ** 3: print (I, end = ',')
The output result is as follows:
153,370,371,407
++
Source: getting started with programming languages: 100 typical examples [Python]