新聞中心
創(chuàng)新互聯(lián)python教程:

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括洛川網(wǎng)站建設(shè)、洛川網(wǎng)站制作、洛川網(wǎng)頁(yè)制作以及洛川網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,洛川網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到洛川省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
寫一個(gè) Python 程序計(jì)算數(shù)列 1 +2 +3 +的和……+n 用 For 循環(huán)和函數(shù)舉例。
級(jí)數(shù) 1 +2 +3 +的 Python 和的數(shù)學(xué)公式。+n = ( n (n+1) / 6)
計(jì)算數(shù)列 1 +2 +3 +和的 Python 程序。+n
這個(gè) Python 程序允許用戶輸入任意正整數(shù)。接下來(lái),Python 找到系列 1 +2 +3 +的和……+n 使用上述公式。
# Python Program to calculate Sum of Series 13+23+33+….+n3
import math
number = int(input("Please Enter any Positive Number : "))
total = 0
total = math.pow((number * (number + 1)) /2, 2)
print("The Sum of Series upto {0} = {1}".format(number, total))系列 1 +2 +3 +的 Python 和……+n 使用數(shù)學(xué)功率輸出
Please Enter any Positive Number : 7
The Sum of Series upto 7 = 784.0求和=冪((數(shù)(數(shù)+ 1)) / 2),2) =冪((7 (7 + 1)) / 2),2) 求和=冪((7 * 8) / 2),2) = 784
計(jì)算數(shù)列 1 +2 +3 +和的 Python 程序。+n 例 2
如果想讓 Python 顯示系列 1 +2 +3 +…。+n 訂單,我們必須添加額外的循環(huán)和 If Else 。
import math
number = int(input("Please Enter any Positive Number : "))
total = 0
total = math.pow((number * (number + 1)) /2, 2)
for i in range(1, number + 1):
if(i != number):
print("%d^3 + " %i, end = ' ')
else:
print("{0}^3 = {1}".format(i, total))第 1 系列 python+2г+3℃+nг輸出
Please Enter any Positive Number : 5
1^3 + 2^3 + 3^3 + 4^3 + 5^3 = 225.0計(jì)算數(shù)列 1 +2 +3 +和的 Python 程序。+n 使用函數(shù)
這個(gè)系列 1 +2 +3 +的 Python 和……+n 程序同上。然而,在這個(gè) Python 程序中,我們定義了一個(gè)函數(shù)來(lái)放置邏輯。
import math
def sum_of_cubes_series(number):
total = 0
total = math.pow((number * (number + 1)) /2, 2)
for i in range(1, number + 1):
if(i != number):
print("%d^3 + " %i, end = ' ')
else:
print("{0}^3 = {1}".format(i, total))
num = int(input("Please Enter any Positive Number : "))
sum_of_cubes_series(num)Please Enter any Positive Number : 7
1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 6^3 + 7^3 = 784.0Python 程序求數(shù)列 1 +2 +3 +的和……使用遞歸
這里,我們使用 Python 遞歸函數(shù)來(lái)求數(shù)列 1 +2 +3 +的和。+n。
def sum_of_cubes_series(number):
if(number == 0):
return 0
else:
return (number * number * number) + sum_of_cubes_series(number - 1)
num = int(input("Please Enter any Positive Number : "))
total = sum_of_cubes_series(num)
print("The Sum of Series upto {0} = {1}".format(num, total)) 名稱欄目:Python 程序:計(jì)算數(shù)列13 + 23 + ... + n3
文章轉(zhuǎn)載:http://fisionsoft.com.cn/article/cojhdid.html


咨詢
建站咨詢
