-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheven_better_fibonacci.cpp
More file actions
47 lines (37 loc) · 942 Bytes
/
Copy patheven_better_fibonacci.cpp
File metadata and controls
47 lines (37 loc) · 942 Bytes
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
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <map>
#include <sstream>
using namespace std;
map<unsigned int, unsigned long> counter;
unsigned long fibonacci(unsigned int number)
{
if(number == 0){
throw invalid_argument("number");
}
//cache[1] = 1;
//cache[2] = 1;
static map<unsigned int, unsigned long> cache = {
{1,0},
{2,1}
};
for(int i = 3; i <= number; i++)
{
cache[i] = cache[i-1] + cache[i-2];
}
return cache[number];
}
int main(int argc , char** args)
{
if(argc < 2) {
cout << "Pass atleast one argument, N";
return -1;
}
stringstream ss;
ss << args[1];
unsigned long input_number;
ss >> input_number;
cout<< input_number << "th fobonacci number is " << fibonacci(input_number) <<endl;
for(auto item : counter){
cout << "Fibonacchi of " << item.first << " is called " << item.second << " times" << endl;
}
}