Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

C++에서 시리즈 9, 45, 243,1377…의 N번째 항 찾기

<시간/>

이 문제에서 정수 값 N이 주어집니다. 우리의 임무는 급수의 n번째 항을 찾는 것입니다 -

9, 45, 243, 1377, 8019, …

문제를 이해하기 위해 예를 들어보겠습니다.

Input : N = 4
Output : 1377

솔루션 접근 방식

문제를 찾는 간단한 솔루션은 관찰 기술을 사용하여 N번째 항을 찾는 것입니다. 시리즈를 관찰하면 다음과 같이 공식화할 수 있습니다. -

(1 1 + 2 1 )*3 1 + (1 2 + 2 2 )*3 2 + (1 3 + 2 3 )*3 3 … + (1 n + 2 n )*3 n

예시

솔루션 작동을 설명하는 프로그램

#include <iostream>
#include <math.h>
using namespace std;
long findNthTermSeries(int n){
   return ( ( (pow(1, n) + pow(2, n)) )*pow(3, n) );
}
int main(){
   int n = 4;
   cout<<n<<"th term of the series is "<<findNthTermSeries(n);
   return 0;
}

출력

4th term of the series is 1377