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

在寻求提高您的编程技能时,您可能希望在某个时候了解几何序列。在几何序列中,每一项都是通过将前一项乘以一个常数来找到的。

在本文中,您将学习如何使用 Python、C++、JavaScript 和 C 查找几何级数的和。

什么是几何级数?

无穷等比数列的各项之和称为等比数列。几何序列或几何级数表示如下:

 a, ar, ar², ar³, ...

在哪里,

 a = First term
r = Common ratio

问题陈述

给你第一项,共同比率,和没有。几何级数的项。您需要找到几何级数的总和。示例:让 firstTerm = 1,commonRatio = 2,和 noOfTerms = 8。 几何级数:1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 几何级数的总和:255 因此,输出为 255。

求几何级数和的迭代方法

首先,让我们看一下寻找几何级数总和的迭代方法。您将在下面了解如何使用每种主要编程语言执行此操作。

使用迭代求几何级数和的 C++ 程序

下面是使用迭代求几何级数和的 C++ 程序:

 // C++ program to find the sum of geometric series
#include <iostream>
using namespace std;
// Function to find the sum of geometric series
float sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms)
{
float result = 0;
for (int i=0; i<noOfTerms; i++)
{
result = result + firstTerm;
firstTerm = firstTerm * commonRatio;
}
return result;
}
int main()
{
float firstTerm = 1;
float commonRatio = 2;
int noOfTerms = 8;
cout << "First Term: " << firstTerm << endl;
cout << "Common Ratio: " << commonRatio << endl;
cout << "Number of Terms: " << noOfTerms << endl;
cout << "Sum of the geometric series: " << sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) << endl;
return 0;
}

输出:

 First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255

使用迭代求几何级数总和的 Python 程序

下面是使用迭代求几何级数总和的 Python 程序:

 # Python program to find the sum of geometric series
# Function to find the sum of geometric series
def sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms):
result = 0
for i in range(noOfTerms):
result = result + firstTerm
firstTerm = firstTerm * commonRatio
return result
firstTerm = 1
commonRatio = 2
noOfTerms = 8
print("First Term:", firstTerm)
print("Common Ratio:", commonRatio)
print("Number of Terms:", noOfTerms)
print("Sum of the geometric series:", sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms))

输出:

 First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255

相关:如何打印“你好,世界!”在最流行的编程语言中

使用迭代求几何级数总和的 JavaScript 程序

下面是使用迭代求几何级数总和的 JavaScript 程序:

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

var firstTerm = 1;
var commonRatio = 2;
var noOfTerms = 8;
document.write("First Term: " + firstTerm + "<br>");
document.write("Common Ratio: " + commonRatio + "<br>");
document.write("Number of Terms: " + noOfTerms + "<br>");
document.write("Sum of the geometric series: " + sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));

输出:

 First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255

使用迭代求几何级数总和的 C 程序

下面是使用迭代求几何级数总和的 C 程序:

 // C program to find the sum of geometric series
#include <stdio.h>
// Function to find the sum of geometric series
float sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms)
{
float result = 0;
for (int i=0; i<noOfTerms; i++)
{
result = result + firstTerm;
firstTerm = firstTerm * commonRatio;
}
return result;
}
int main()
{
float firstTerm = 1;
float commonRatio = 2;
int noOfTerms = 8;
printf("First Term: %f ⁠n", firstTerm);
printf("Common Ratio: %f ⁠n", commonRatio);
printf("Number of Terms: %d ⁠n", noOfTerms);
printf("Sum of the geometric series: %f ⁠n", sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));
return 0;
}

输出:

 First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255

使用公式求几何级数和的有效方法

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

 Sum of geometric series = a(1 – rn)/(1 – r)

在哪里,

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

使用公式求几何级数总和的 C++ 程序

下面是使用公式计算几何级数总和的 C++ 程序:

 // C++ program to find the sum of geometric series
#include <bits/stdc++.h>
using namespace std;
// Function to find the sum of geometric series
float sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms)
{
return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio);
}
int main()
{
float firstTerm = 1;
float commonRatio = 2;
int noOfTerms = 8;
cout << "First Term: " << firstTerm << endl;
cout << "Common Ratio: " << commonRatio << endl;
cout << "Number of Terms: " << noOfTerms << endl;
cout << "Sum of the geometric series: " << sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) << endl;
return 0;
}

输出:

 First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255

使用公式求几何级数总和的 Python 程序

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

 # Python program to find the sum of geometric series
# Function to find the sum of geometric series
def sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms):
return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio)
firstTerm = 1
commonRatio = 2
noOfTerms = 8
print("First Term:", firstTerm)
print("Common Ratio:", commonRatio)
print("Number of Terms:", noOfTerms)
print("Sum of the geometric series:", sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms))

输出:

 First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255

相关:如何在多种语言中找到两个数字的 LCM 和 GCD

使用公式求几何级数总和的 JavaScript 程序

下面是使用公式计算几何级数总和的 JavaScript 程序:

 // JavaScript program to find the sum of geometric series
// Function to find the sum of geometric series
function sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) {
return (firstTerm * (1 - Math.pow(commonRatio, noOfTerms))) / (1 - commonRatio);
}

var firstTerm = 1;
var commonRatio = 2;
var noOfTerms = 8;
document.write("First Term: " + firstTerm + "<br>");
document.write("Common Ratio: " + commonRatio + "<br>");
document.write("Number of Terms: " + noOfTerms + "<br>");
document.write("Sum of the geometric series: " + sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));

输出:

 First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255

相关:如何计算字符串中给定字符的出现次数

使用公式求几何级数和的 C 程序

下面是使用公式计算几何级数总和的 C 程序:

 // C program to find the sum of geometric series
#include <stdio.h>
#include <math.h>
// Function to find the sum of geometric series
float sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms)
{
return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio);
}
int main()
{
float firstTerm = 1;
float commonRatio = 2;
int noOfTerms = 8;
printf("First Term: %f ⁠n", firstTerm);
printf("Common Ratio: %f ⁠n", commonRatio);
printf("Number of Terms: %d ⁠n", noOfTerms);
printf("Sum of the geometric series: %f ⁠n", sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));
return 0;
}

输出:

 First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255

现在您知道如何使用不同的编程语言查找几何级数和

在本文中,您学习了如何使用两种方法求几何级数的和:迭代和公式。您还学习了如何使用 Python、C++、JavaScript 和 C 等各种编程语言来解决这个问题。

Python 是一种通用编程语言,专注于代码可读性。您可以将 Python 用于数据科学、机器学习、Web 开发、图像处理、计算机视觉等。它是最通用的编程语言之一。非常值得探索这种强大的编程语言。