-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.4.cpp
37 lines (33 loc) · 828 Bytes
/
2.4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <vector>
#include <iostream>
using namespace std;
inline bool check_validity (int pos)
{ return (pos <= 0 || pos >= 64)? false : true; }
// local static
const vector<int>* pentagonal_series(int pos)
{
static vector<int> _elems;
if (check_validity (pos) && (pos > _elems.size()))
for (int ix = _elems.size()+1; ix <= pos; ++ix)
_elems.push_back( (ix*(3*ix-1))/2 );
return &_elems;
}
bool pentagonal_elem(int pos, int &elem)
{
if (!check_validity(pos)){
cout << "sorry, Invalid position:" << pos << endl;
elem = 0;
return false;
}
const vector<int>* pent = pentagonal_series(pos);
elem = (*pent)[pos-1];
return true;
}
int main()
{
int elem;
if (pentagonal_elem (8, elem))
cout << "element 8 is " << elem << '\n';
if (pentagonal_elem(88,elem))
cout << "element 88 is " << elem << '\n';
}