Output method: First use the for statement to loop through all the numbers from 100 to 999, and assign it to the variable n; then decompose the variable n to obtain the ones digit k, tens digit j and hundreds digit i ; Finally, determine whether the cube sum of i, j and k numbers is equal to n. If so, just use the "print(n)" statement to output.
The operating environment of this tutorial: windows7 system, python3.7 version, DELL G3 computer
The so-called "daffodil number" refers to a three-dimensional A number of digits whose sum of cubes is equal to the number itself. For example: 153 is a "daffodil number" because 153=1 cubed + 5 cubed + 3 cubed.
python outputs the number of all daffodils
for n in range(100,1000): i = n // 100 j = n // 10 % 10 k = n % 10 if n == i ** 3 + j ** 3 + k ** 3: print (n)
Output:
153 370 371 407
Detailed explanation
Program analysis: using for Loop control 100-999 numbers, each number is decomposed into units, tens and hundreds.
First line:
for n in range(100,1000):
Because the narcissus number is a three-digit number, we loop through all the numbers from 100 to 999 and assign it to n
Second line:
i = n // 100
Divide 100 by n, and what you get is actually a three-digit hundred digit. Assign it to i
Third line:
j = n // 10 % 10
Divide 10 by n, and you will get a two-digit number consisting of hundreds and tens. Then divide this number by 10 to find the remainder. You will get our tens number, and assign it to j
The fourth line:
k = n % 10
Divide n by 10 and find the remainder. What you get is the single digit number of n. Assign it to k. At this time, the hundreds, tens and tens digits of the three-digit n are We have already obtained the single digits and assigned them to i, j, k respectively
The fifth line:
if n == i ** 3 + j ** 3 + k ** 3:
Judgment: If n is equal to the cube of its hundreds digits and ten digits If the cube is a single digit cube, then it is the narcissus number. At this time, print out the n that meets the conditions, otherwise it will enter the next loop
[Related recommendations: Python3 video tutorial 】
The above is the detailed content of How to output the number of all daffodils in python. For more information, please follow other related articles on the PHP Chinese website!

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

The methods to debug the shebang problem include: 1. Check the shebang line to make sure it is the first line of the script and there are no prefixed spaces; 2. Verify whether the interpreter path is correct; 3. Call the interpreter directly to run the script to isolate the shebang problem; 4. Use strace or trusts to track the system calls; 5. Check the impact of environment variables on shebang.

Pythonlistscanbemanipulatedusingseveralmethodstoremoveelements:1)Theremove()methodremovesthefirstoccurrenceofaspecifiedvalue.2)Thepop()methodremovesandreturnsanelementatagivenindex.3)Thedelstatementcanremoveanitemorslicebyindex.4)Listcomprehensionscr

Pythonlistscanstoreanydatatype,includingintegers,strings,floats,booleans,otherlists,anddictionaries.Thisversatilityallowsformixed-typelists,whichcanbemanagedeffectivelyusingtypechecks,typehints,andspecializedlibrarieslikenumpyforperformance.Documenti

Pythonlistssupportnumerousoperations:1)Addingelementswithappend(),extend(),andinsert().2)Removingitemsusingremove(),pop(),andclear().3)Accessingandmodifyingwithindexingandslicing.4)Searchingandsortingwithindex(),sort(),andreverse().5)Advancedoperatio

Create multi-dimensional arrays with NumPy can be achieved through the following steps: 1) Use the numpy.array() function to create an array, such as np.array([[1,2,3],[4,5,6]]) to create a 2D array; 2) Use np.zeros(), np.ones(), np.random.random() and other functions to create an array filled with specific values; 3) Understand the shape and size properties of the array to ensure that the length of the sub-array is consistent and avoid errors; 4) Use the np.reshape() function to change the shape of the array; 5) Pay attention to memory usage to ensure that the code is clear and efficient.

BroadcastinginNumPyisamethodtoperformoperationsonarraysofdifferentshapesbyautomaticallyaligningthem.Itsimplifiescode,enhancesreadability,andboostsperformance.Here'showitworks:1)Smallerarraysarepaddedwithonestomatchdimensions.2)Compatibledimensionsare

ForPythondatastorage,chooselistsforflexibilitywithmixeddatatypes,array.arrayformemory-efficienthomogeneousnumericaldata,andNumPyarraysforadvancednumericalcomputing.Listsareversatilebutlessefficientforlargenumericaldatasets;array.arrayoffersamiddlegro


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
