숫자 N이 주어지면 그 숫자가 오각형 숫자인지 아닌지를 확인하는 작업입니다. 오각형을 형성하도록 배열할 수 있는 숫자는 오각형을 형성하는 점으로 사용할 수 있는 오각형 수입니다. 예를 들어, 일부 오각형 숫자는 1, 5, 12, 22, 35, 51....
공식을 사용하여 숫자가 오각형 숫자인지 여부를 확인할 수 있습니다.
$$p(n)=\frac{\text{3}*n^2-n}{\text{2}}$$
여기서, n은 오각형이 가질 점의 수입니다.
예시
Input-: n=22 Output-: 22 is pentagonal number Input-: n=23 Output-: 23 is not a pentagonal number
알고리즘
Start Step 1 -> declare function to Check N is pentagonal or not bool check(int n) declare variables as int i = 1, a do set a = (3*i*i - i)/2 set i += 1 while ( a < n ); return (a == n); Step 2 -> In main() Declare int n = 22 If (check(n)) Print is pentagonal End Else Print it is not pentagonal End Stop
예시
#include <iostream>
using namespace std;
// check N is pentagonal or not.
bool check(int n){
int i = 1, a;
do{
a = (3*i*i - i)/2;
i += 1;
}
while ( a < n );
return (a == n);
}
int main(){
int n = 22;
if (check(n))
cout << n << " is pentagonal " << endl;
else
cout << n << " is not pentagonal" << endl;
return 0;
} 출력
22 is pentagonal