12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- #include "utils.h"
- #include <limits>
- bool is_size_ok(int size) {
- if (size <= 0 || size > 1024) {
- cout << "size is invalid!" << endl;
- return false;
- }
- else {
- return true;
- }
- }
- vector<u32>* fibon_seq(int size) {
- static vector<u32> elems;
- if (!is_size_ok(size)) {
- return 0;
- }
- int current_size = elems.size();
- if (size > current_size) {
- for (int ix = current_size; ix < size; ++ix) {
- if (ix < 2) {
- elems.push_back(1);
- }
- else {
- int value_ix = elems[ix - 1] + elems[ix - 2];
- elems.push_back(value_ix);
- }
- }
- }
- return &elems;
- }
- bool fibon_elem(int position, u32& elem) {
- vector<u32>* fibon_vector = fibon_seq(position);
- if (fibon_vector) {
- elem = (*fibon_vector)[position-1]; //index overflow
- return true;
- }
- else {
- elem = 0;
- return false;
- }
- }
- int main() {
- u32 elem = 0;
- bool ret = fibon_elem(100, elem);
- if (ret) {
- cout << "elem is" << elem << endl;
- }
- return 0;
- }
|