当前分类:300例题>>正文

Python示例:计算等差数列和

来源:互联网   更新时间:2023年6月27日  

Python 示例

写一个 Python示例,用一个实际例子求算术级数(A.P .级数)的和。

Python 自动编程系列

算术数列是一个术语序列,其中下一个项目是通过将一个共同的差异加到上一个项目上而获得的。或者 A.P .级数是任意两个连续数的差总是相同的一系列数。这种差异称为共同差异。

在计算等差数列的数学中 数列的和:Sn = n/2(2a+(n–1)d) 数列的 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 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示例中,我们没有使用任何数学公式。

# 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
本文固定链接:https://6yhj.com/leku-p-4506.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: sublime