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

公司主營業(yè)務:網(wǎng)站建設、成都網(wǎng)站設計、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出扶余免費做網(wǎng)站回饋大家。
寫一個 Python 程序,用實例求等比數(shù)列(G.P .級數(shù))的和。
Python 通用編程系列
等比數(shù)列是一個元素序列,其中下一個項目通過乘以上一個項目的公約數(shù)而獲得。或者 G.P. Series 是一系列數(shù)字,其中任何連續(xù)數(shù)字(項目)的公共比率總是相同的。
G.P 系列之和 Sn = a(rn)/(1-r) Tn = ar(n-1)背后的數(shù)學公式
Python 程序求等比數(shù)列和的例子
這個 Python 程序允許用戶輸入第一個值、一系列項目的總數(shù)和公共定額。接下來,它找到等比數(shù)列的和。
# Python Program to find Sum of Geometric Progression Series
import math
a = int(input("Please Enter First Number of an G.P Series: : "))
n = int(input("Please Enter the Total Numbers in this G.P Series: : "))
r = int(input("Please Enter the Common Ratio : "))
total = (a * (1 - math.pow(r, n ))) / (1- r)
tn = a * (math.pow(r, n - 1))
print("\nThe Sum of Geometric Progression Series = " , total)
print("The tn Term of Geometric Progression Series = " , tn)
不用數(shù)學公式求等比數(shù)列和的程序
在這個 Python 程序中,我們沒有使用任何數(shù)學公式。
# Python Program to find Sum of Geometric Progression Series
a = int(input("Please Enter First Number of an G.P Series: : "))
n = int(input("Please Enter the Total Numbers in this G.P Series: : "))
r = int(input("Please Enter the Common Ratio : "))
total = 0
value = a
print("\nG.P Series :", end = " ")
for i in range(n):
print("%d " %value, end = " ")
total = total + value
value = value * r
print("\nThe Sum of Geometric Progression Series = " , total)等比數(shù)列輸出的 Python 和
Please Enter First Number of an G.P Series: : 1
Please Enter the Total Numbers in this G.P Series: : 5
Please Enter the Common Ratio : 4
G.P Series : 1 4 16 64 256
The Sum of Geometric Progression Series = 341
用函數(shù)計算等比數(shù)列和的 Python 程序
這個 Python 等比數(shù)列程序與第一個示例相同。然而,在這個 Python 程序中,我們使用函數(shù)來分離邏輯。
# Python Program to find Sum of Geometric Progression Series
import math
def sumofGP(a, n, r):
total = (a * (1 - math.pow(r, n ))) / (1- r)
return total
a = int(input("Please Enter First Number of an G.P Series: : "))
n = int(input("Please Enter the Total Numbers in this G.P Series: : "))
r = int(input("Please Enter the Common Ratio : "))
total = sumofGP(a, n, r)
print("\nThe Sum of Geometric Progression Series = " , total)寶潔系列輸出的 Python 和
Please Enter First Number of an G.P Series: : 2
Please Enter the Total Numbers in this G.P Series: : 6
Please Enter the Common Ratio : 3
The Sum of Geometric Progression Series = 728.0 文章名稱:Python程序:計算等比數(shù)列和
本文地址:http://fisionsoft.com.cn/article/ccoepos.html


咨詢
建站咨詢
