创新互联python教程:
梨林网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。成都创新互联于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
写一个 Python 程序,用一个实际例子求算术级数(A.P .级数)的和。
算术数列是一个术语序列,其中下一个项目是通过将一个共同的差异加到上一个项目上而获得的。或者 A.P .级数是任意两个连续数的差总是相同的一系列数。这种差异称为共同差异。
在计算等差数列的数学中 数列的和:Sn = n/2(2a+(n–1)d) 数列的 Tn 项:Tn = a+(n–1)d
这个 Python 程序允许用户输入第一个值、一个系列中的项目总数以及共同的区别。接下来,Python 找到算术级数的和。
# Python Program to find Sum of Arithmetic Progression Series
a = int(input("Please Enter First Number of an A.P Series: : "))
n = int(input("Please Enter the Total Numbers in this A.P Series: : "))
d = int(input("Please Enter the Common Difference : "))
total = (n * (2 * a + (n - 1) * d)) / 2
tn = a + (n - 1) * d
print("\nThe Sum of Arithmetic Progression Series = " , total)
print("The tn Term of Arithmetic Progression Series = " , tn)
这个 Python 的 A.P 程序的和同上。这里我们用 While Loop 显示 A.P 系列,可选。
# Python Program to find Sum of Arithmetic Progression Series
a = int(input("Please Enter First Number of an A.P Series: : "))
n = int(input("Please Enter the Total Numbers in this A.P Series: : "))
d = int(input("Please Enter the Common Difference : "))
total = (n * (2 * a + (n - 1) * d)) / 2
tn = a + (n - 1) * d
i = a
print("\nThe tn Term of Arithmetic Progression Series = " , tn)
print("The Sum of Arithmetic Progression Series : ")
while(i <= tn):
if(i != tn):
print("%d + " %i, end = " ")
else:
print("%d = %d" %(i, total))
i = i + d
算术级数和输出
Please Enter First Number of an A.P Series: : 2
Please Enter the Total Numbers in this A.P Series: : 6
Please Enter the Common Difference : 4
The tn Term of Arithmetic Progression Series = 22
The Sum of Arithmetic Progression Series :
2 + 6 + 10 + 14 + 18 + 22 = 72
在这个 Python 程序中,我们没有使用任何数学公式。
# Python Program to find Sum of Arithmetic Progression Series
a = int(input("Please Enter First Number of an A.P Series: : "))
n = int(input("Please Enter the Total Numbers in this A.P Series: : "))
d = int(input("Please Enter the Common Difference : "))
total = 0
value = a
print("Arithmetic Progression Series : ", end = " ")
for i in range(n):
print("%d + " %value, end = " ")
total = total + value
value = value + d
print("\nThe Sum of Arithmetic Progression Series upto %d = %d " %(n, total))
Python 算术级数求和输出
Please Enter First Number of an A.P Series: : 1
Please Enter the Total Numbers in this A.P Series: : 4
Please Enter the Common Difference : 5
Arithmetic Progression Series : 1 + 6 + 11 + 16 +
The Sum of Arithmetic Progression Series upto 4 = 34
这个 Python 算术级数和与第一个例子相同。但是,我们使用函数来分离逻辑。
# Python Program to find Sum of Arithmetic Progression Series
def sumofAP(a, n, d):
total = (n * (2 * a + (n - 1) * d)) / 2
return total
a = int(input("Please Enter First Number of an A.P Series: : "))
n = int(input("Please Enter the Total Numbers in this A.P Series: : "))
d = int(input("Please Enter the Common Difference : "))
total = sumofAP(a, n, d)
print("\nThe Sum of Arithmetic Progression Series = " , total)
Python 算术级数求和输出
Please Enter First Number of an A.P Series: : 2
Please Enter the Total Numbers in this A.P Series: : 5
Please Enter the Common Difference : 10
The Sum of Arithmetic Progression Series = 110.0
分享标题:Python程序:计算等差数列和
URL地址:http://www.csdahua.cn/qtweb/news6/540356.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网