여러 언어를 사용하여 기하 급수의 합을 찾는 방법

여러 언어를 사용하여 기하 급수의 합을 찾는 방법

프로그래밍 기술을 향상시키려는 경우 어느 시점에서 기하학적 시퀀스에 대해 배우고 싶을 것입니다. 기하학적 시퀀스에서 각 항은 이전 항에 상수를 곱하여 찾습니다.





이 기사에서는 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
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;
}

산출:

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

관련된: 'Hello, World!'를 인쇄하는 방법 가장 인기 있는 프로그래밍 언어로

반복을 사용하여 기하 급수의 합을 찾는 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 {
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));

산출:

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
// 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;
}

산출:

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
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;
}

산출:

아이폰 7에서 비디오를 자르는 방법
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를 찾는 방법

안전 모드에서 Outlook을 시작하는 방법

공식을 사용하여 기하 급수의 합을 찾는 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 + '
');
document.write('Common Ratio: ' + commonRatio + '
');
document.write('Number of Terms: ' + noOfTerms + '
');
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
#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;
}

산출:

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

이제 다양한 프로그래밍 언어를 사용하여 기하 급수 합계를 찾는 방법을 알게 되었습니다.

이 기사에서는 반복과 공식이라는 두 가지 접근 방식을 사용하여 기하 급수의 합을 찾는 방법을 배웠습니다. 또한 Python, C++, JavaScript 및 C와 같은 다양한 프로그래밍 언어를 사용하여 이 문제를 해결하는 방법을 배웠습니다.

Python은 코드 가독성에 중점을 둔 범용 프로그래밍 언어입니다. 데이터 과학, 기계 학습, 웹 개발, 이미지 처리, 컴퓨터 비전 등에 Python을 사용할 수 있습니다. Python은 가장 다재다능한 프로그래밍 언어 중 하나입니다. 이 강력한 프로그래밍 언어를 탐색할 가치가 있습니다.

공유하다 공유하다 트위터 이메일 이메일이 진짜인지 가짜인지 확인하는 3가지 방법

다소 의심스러운 이메일을 받았다면 항상 해당 이메일의 진위 여부를 확인하는 것이 가장 좋습니다. 이메일이 진짜인지 확인하는 세 가지 방법이 있습니다.

다음 읽기
관련 항목
  • 프로그램 작성
  • 파이썬
  • 자바스크립트
  • C 프로그래밍
  • 프로그램 작성
저자 소개 유브라지 찬드라(60편 게재)

Yuvraj는 인도 델리 대학교의 컴퓨터 공학 학부생입니다. 그는 풀 스택 웹 개발에 열정적입니다. 그는 글을 쓰지 않을 때 다양한 기술의 깊이를 탐구하고 있습니다.

유브라지 찬드라가 참여한 작품 더보기

뉴스레터 구독

뉴스레터에 가입하여 기술 팁, 리뷰, 무료 전자책 및 독점 거래를 확인하십시오!

구독하려면 여기를 클릭하세요.