Python exercise question 009: daffodils, python009

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.