Cách tìm tổng của một chuỗi hình học bằng nhiều ngôn ngữ

Cách tìm tổng của một chuỗi hình học bằng nhiều ngôn ngữ

Khi muốn nâng cao kỹ năng lập trình của mình, có thể bạn sẽ muốn tìm hiểu về chuỗi hình học tại một số điểm. Trong một dãy hình học, mỗi số hạng được tìm bằng cách nhân số hạng trước với một hằng số.





Trong bài viết này, bạn sẽ học cách tìm tổng của chuỗi hình học bằng Python, C ++, JavaScript và C.





Chuỗi hình học là gì?

Tổng các số hạng của một dãy hình học vô hạn được gọi là một chuỗi hình học. Dãy hình học hoặc tiến trình hình học được biểu thị như sau:





quay video trên windows media player
a, ar, ar², ar³, ...

ở đâu,

a = First term
r = Common ratio

Báo cáo vấn đề

Bạn đã đưa ra số hạng đầu tiên, tỷ lệ chung và không. của các điều khoản của chuỗi hình học. Bạn cần tìm tổng của chuỗi hình học. Thí dụ : Cho firstTerm = 1, commonRatio = 2, và noOfTerms = 8. Chuỗi hình học: 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 Tổng của chuỗi hình học: 255 Như vậy, kết quả đầu ra là 255.



Phương pháp tiếp cận lặp đi lặp lại để tìm tổng của một chuỗi hình học

Đầu tiên, chúng ta hãy xem xét cách lặp lại để tìm tổng của một chuỗi hình học. Bạn sẽ tìm hiểu cách thực hiện việc này với từng ngôn ngữ lập trình chính bên dưới.

Chương trình C ++ để tìm tổng của một chuỗi hình học bằng cách sử dụng lặp lại

Dưới đây là chương trình C ++ để tìm tổng của một chuỗi hình học bằng cách sử dụng phép lặp:





// C++ program to find the sum of geometric series
#include
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 {
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;
}

Đầu ra:

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

Chương trình Python để tìm tổng của một chuỗi hình học bằng cách sử dụng lặp lại

Dưới đây là chương trình Python để tìm tổng của một chuỗi hình học bằng cách sử dụng phép lặp:





# 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))

Đầu ra:

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

Có liên quan: Làm thế nào để in 'Hello, World!' trong các ngôn ngữ lập trình phổ biến nhất

Chương trình JavaScript để tìm tổng của một chuỗi hình học bằng cách sử dụng lặp lại

Dưới đây là chương trình JavaScript để tìm tổng của một chuỗi hình học bằng cách sử dụng phép lặp:

// 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 {
result = result + firstTerm;
firstTerm = firstTerm * commonRatio;
}
return result;
}

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

Đầu ra:

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

Chương trình C để tìm tổng của một chuỗi hình học bằng cách sử dụng lặp lại

Dưới đây là chương trình C để tìm tổng của một chuỗi hình học bằng cách sử dụng phép lặp:

// C program to find the sum of geometric series
#include
// Function to find the sum of geometric series
float sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms)
{
float result = 0;
for (int i=0; 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;
}

Đầu ra:

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

Một cách tiếp cận hiệu quả để tìm tổng của một chuỗi hình học bằng cách sử dụng công thức

Bạn có thể sử dụng công thức sau để tìm tổng của chuỗi hình học:

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

ở đâu,

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

Chương trình C ++ để tìm tổng của một chuỗi hình học sử dụng công thức

Dưới đây là chương trình C ++ để tìm tổng của một chuỗi hình học bằng công thức:

// C++ program to find the sum of geometric series
#include
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;
}

Đầu ra:

cách tạo hình ảnh 300dpi
First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255

Chương trình Python để tìm tổng của một chuỗi hình học bằng công thức

Dưới đây là chương trình Python để tìm tổng của một chuỗi hình học bằng công thức:

# 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))

Đầu ra:

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

Liên quan: Cách tìm LCM và GCD của hai số bằng nhiều ngôn ngữ

làm thế nào để tìm ra số điện thoại đã gọi cho bạn

Chương trình JavaScript để tìm tổng của một chuỗi hình học bằng công thức

Dưới đây là chương trình JavaScript để tìm tổng của một chuỗi hình học bằng công thức:

// 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 + '
');
document.write('Common Ratio: ' + commonRatio + '
');
document.write('Number of Terms: ' + noOfTerms + '
');
document.write('Sum of the geometric series: ' + sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));

Đầu ra:

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

Liên quan: Cách đếm số lần xuất hiện của một ký tự đã cho trong một chuỗi

C Chương trình tìm tổng của một chuỗi hình học bằng công thức

Dưới đây là chương trình C để tìm tổng của một chuỗi hình học bằng công thức:

// C program to find the sum of geometric series
#include
#include
// 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;
}

Đầu ra:

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

Bây giờ bạn biết cách tìm tổng chuỗi hình học bằng cách sử dụng các ngôn ngữ lập trình khác nhau

Trong bài viết này, bạn đã học cách tìm tổng của chuỗi hình học bằng hai cách tiếp cận: lặp và công thức. Bạn cũng đã học cách giải quyết vấn đề này bằng các ngôn ngữ lập trình khác nhau như Python, C ++, JavaScript và C.

Python là một ngôn ngữ lập trình có mục đích chung, tập trung vào khả năng đọc mã. Bạn có thể sử dụng Python cho khoa học dữ liệu, học máy, phát triển web, xử lý hình ảnh, thị giác máy tính, v.v. Đây là một trong những ngôn ngữ lập trình linh hoạt nhất. Rất đáng để khám phá ngôn ngữ lập trình mạnh mẽ này.

Đăng lại Đăng lại tiếng riu ríu E-mail 3 cách để kiểm tra xem một email là thật hay giả

Nếu bạn nhận được một email có vẻ hơi khó hiểu, tốt nhất bạn nên kiểm tra tính xác thực của nó. Dưới đây là ba cách để biết một email có phải là thật hay không.

Đọc tiếp
Chủ đề liên quan
  • Lập trình
  • Python
  • JavaScript
  • Lập trình C
  • Lập trình
Giới thiệu về tác giả Yuvraj Chandra(60 bài báo đã xuất bản)

Yuvraj là sinh viên ngành Khoa học Máy tính tại Đại học Delhi, Ấn Độ. Anh ấy đam mê Phát triển Web Full Stack. Khi không viết, anh ấy đang khám phá chiều sâu của các công nghệ khác nhau.

Xem thêm từ Yuvraj Chandra

Theo dõi bản tin của chúng tôi

Tham gia bản tin của chúng tôi để biết các mẹo công nghệ, đánh giá, sách điện tử miễn phí và các ưu đãi độc quyền!

Bấm vào đây để đăng ký