如何使用多种语言求算术级数的总和

等差数列是每一项与前一项相差一个常数的序列。并且知道如何找到这些可以帮助您建立您的编程技能集,无论您使用哪种语言。

在本文中,您将学习如何使用 Python、C++、JavaScript 和 C 计算算术级数的和。

什么是算术级数?

一个有限等差数列的各项之和称为等差数列。等差数列表示如下:

 a, a+d, a+2d, a+3d, a+4d, ...

在哪里,

 a = First term
d = Common difference

问题陈述

你得到了第一个术语,共同点,没有。算术级数的项。您需要找到算术级数的总和。示例:让 firstTerm = 1,commonDifference = 2,和 noOfTerms = 5。 算术级数:1 + 3 + 5 + 7 + 9 算术级数之和:25 因此,输出为 25。

求算术级数和的迭代方法

首先,我们来看看迭代方法。您可以在下面找到如何以这种方式为主要编程语言求和。

使用迭代求算术级数之和的 C++ 程序

以下是使用迭代求算术级数之和的 C++ 程序:

 // C++ program to find the sum of arithmetic series
#include <iostream>
using namespace std;
// Function to find the sum of arithmetic series
int sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms)
{
int result = 0;
for (int i=0; i<noOfTerms; i++)
{
result = result + firstTerm;
firstTerm = firstTerm + commonDifference;
}
return result;
}
int main()
{
int firstTerm = 1;
int commonDifference = 2;
int noOfTerms = 5;
cout << "First Term: " << firstTerm << endl;
cout << "Common Difference: " << commonDifference << endl;
cout << "Number of Terms: " << noOfTerms << endl;
cout << "Sum of the arithmetic series: " << sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) << endl;
return 0;
}

输出:

 First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25

使用迭代查找算术系列之和的 Python 程序

下面是使用迭代找到算术级数之和的 Python 程序:

 # Python program to find the sum of arithmetic series
# Function to find the sum of arithmetic series
def sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms):
result = 0
for i in range(noOfTerms):
result = result + firstTerm
firstTerm = firstTerm + commonDifference
return result
firstTerm = 1
commonDifference = 2
noOfTerms = 5
print("First Term:", firstTerm)
print("Common Difference:", commonDifference)
print("Number of Terms:", noOfTerms)
print("Sum of the arithmetic series:", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms))

输出:

 First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25

相关:如何在 Python 中使用 For 循环

使用迭代查找算术系列之和的 JavaScript 程序

下面是使用迭代计算算术级数之和的 JavaScript 程序:

 // JavaScript program to find the sum of arithmetic series
// Function to find the sum of arithmetic series
function sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) {
var result = 0;
for (let i=0; i<noOfTerms; i++)
{
result = result + firstTerm;
firstTerm = firstTerm + commonDifference;
}
return result;
}

var firstTerm = 1;
var commonDifference = 2;
var noOfTerms = 5;
document.write("First Term: " + firstTerm + "<br>");
document.write("Common Difference: " + commonDifference + "<br>");
document.write("Number of Terms: " + noOfTerms + "<br>");
document.write("Sum of the arithmetic series: " + sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));

输出:

 First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25

相关:如何查找数组中所有元素的乘积

使用迭代求算术级数之和的 C 程序

以下是使用迭代求算术级数之和的 C 程序:

 // C program to find the sum of arithmetic series
#include <stdio.h>
// Function to find the sum of arithmetic series
int sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms)
{
int result = 0;
for (int i=0; i<noOfTerms; i++)
{
result = result + firstTerm;
firstTerm = firstTerm + commonDifference;
}
return result;
}
int main()
{
int firstTerm = 1;
int commonDifference = 2;
int noOfTerms = 5;
printf("First Term: %d ⁠n", firstTerm);
printf("Common Difference: %d ⁠n", commonDifference);
printf("Number of Terms: %d ⁠n", noOfTerms);
printf("Sum of the arithmetic series: %d ⁠n", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));
return 0;
}

输出:

 First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25

使用公式求算术级数和的有效方法

您可以使用以下公式来计算算术级数的总和:

 Sum of arithmetic series = ((n / 2) * (2 * a + (n - 1) * d))

在哪里,

 a = First term
d = Common difference
n = No. of terms

使用公式求算术级数之和的 C++ 程序

下面是使用公式计算等差数列之和的 C++ 程序:

 // C++ program to find the sum of arithmetic series
#include <iostream>
using namespace std;
// Function to find the sum of arithmetic series
int sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms)
{
return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference);
}
int main()
{
int firstTerm = 1;
int commonDifference = 2;
int noOfTerms = 5;
cout << "First Term: " << firstTerm << endl;
cout << "Common Difference: " << commonDifference << endl;
cout << "Number of Terms: " << noOfTerms << endl;
cout << "Sum of the arithmetic series: " << sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) << endl;
return 0;
}

输出:

 First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25

使用公式求算术级数之和的 Python 程序

下面是使用公式计算算术级数总和的 Python 程序:

 # Python program to find the sum of arithmetic series
# Function to find the sum of arithmetic series
def sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms):
return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference)
firstTerm = 1
commonDifference = 2
noOfTerms = 5
print("First Term:", firstTerm)
print("Common Difference:", commonDifference)
print("Number of Terms:", noOfTerms)
print("Sum of the arithmetic series:", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms))

输出:

 First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25

相关: 如何在 C、C++、Python 和 JavaScript 中使用递归实现线性搜索

使用公式求算术级数之和的 JavaScript 程序

下面是使用以下公式计算等差数列之和的 JavaScript 程序:

 // JavaScript program to find the sum of arithmetic series
// Function to find the sum of arithmetic series
function sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms) {
return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference);
}

var firstTerm = 1;
var commonDifference = 2;
var noOfTerms = 5;
document.write("First Term: " + firstTerm + "<br>");
document.write("Common Difference: " + commonDifference + "<br>");
document.write("Number of Terms: " + noOfTerms + "<br>");
document.write("Sum of the arithmetic series: " + sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));

输出:

 First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25

使用公式求算术级数之和的 C 程序

下面是使用公式计算等差数列之和的 C 程序:

 // C program to find the sum of arithmetic series
#include <stdio.h>
// Function to find the sum of arithmetic series
int sumOfArithmeticSeries(int firstTerm, int commonDifference, int noOfTerms)
{
return (noOfTerms / 2) * (2 * firstTerm + (noOfTerms - 1) * commonDifference);
}
int main()
{
int firstTerm = 1;
int commonDifference = 2;
int noOfTerms = 5;
printf("First Term: %d ⁠n", firstTerm);
printf("Common Difference: %d ⁠n", commonDifference);
printf("Number of Terms: %d ⁠n", noOfTerms);
printf("Sum of the arithmetic series: %d ⁠n", sumOfArithmeticSeries(firstTerm, commonDifference, noOfTerms));
return 0;
}

输出:

 First Term: 1
Common Difference: 2
Number of Terms: 5
Sum of the arithmetic series: 25

使用不同的编程语言查找算术系列很容易

现在您已经阅读了本文,您知道如何使用每种主要编程语言查找算术级数。

C++ 是“面包和黄油”编程语言之一。它用于开发各种软件,如数据库、操作系统、编译器、Web 浏览器等。如果您想学习 C++,您应该查看一些最好的网站,如 Udemy、edX、LearnCpp 等。