In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how Python calculates the cube sum of n natural numbers". In daily operation, it is believed that many people have doubts about how Python calculates the cube sum of n natural numbers. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how Python calculates the cube sum of n natural numbers". Next, please follow the editor to study!
Function introduction:
Calculation formula 13 + 23 + 33 + 43 +. . + N3
Meet the requirements:
Input: n = 5
Output: 225
Formula: 13 + 23 + 33 + 43 + 53 = 225
Code implementation
# function def sumSeries (n) that defines the sum of cubes: sum=0 for i in range (1): sum+=i*i*i return sum# calls function n=5print (sumSeries (n))
Output result:
two hundred and twenty five
Program analysis
The python function defined by the behavior of programs 1 to 6 realizes the sum of the cubes of the natural number n.
The for loop is used in the function, for i in range (1 ~ n):, this line implements a loop from 1 to n, then uses the format of sum+, adds the cube to the previous number, stores the result in the sum variable, and finally returns the sum of the cube.
Line 8 defines the value of the variable n, line 9 realizes the function call, passes the value of n into the function, outputs the result through print, and realizes the function of obtaining the sum of cubes through the function call.
If you want to find the sum of cubes of other variables, you just need to change the value of n.
Complete the study of this procedure, can master the use of functions and for loops.
At this point, the study on "how Python calculates the cube sum of n natural numbers" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.