编写一个 Python 程序,通过实际示例查找算术级数(A.P. Series)的总和。
Python A.P. Series
算术级数是项的序列,其中下一项是通过在前一项上添加公差获得的。或者 A.P. 级数是一系列数字,其中任意两个连续数字的差始终相同。这个差称为公差。
算术级数计算背后的数学原理
A.P. 级数之和:Sn = n/2(2a + (n – 1) d)
A.P. 级数的 Tn 项:Tn = a + (n – 1) d
Python 程序查找算术级数示例之和
此 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 程序计算算术级数之和示例 2
此 Python A.P. 总和程序与上面相同。在这里,我们使用 While 循环 来显示 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
Python 算术级数总和输出
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 程序中,我们不使用任何数学公式。
# 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 算术级数总和与第一个示例相同。但是,我们使用 函数将逻辑分开了。
# 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