-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.cpp
More file actions
39 lines (31 loc) · 863 Bytes
/
fibonacci.cpp
File metadata and controls
39 lines (31 loc) · 863 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
#include <iostream>
#include <map>
#include <sstream>
using namespace std;
map<unsigned int, unsigned int> counter;
unsigned long fibonacci(unsigned int number)
{
if(number <= 0){
throw invalid_argument("number");
}
if(number == 1) return 0;
if(number == 2) return 1;
++counter[number];
return fibonacci(number-1) + fibonacci(number-2);
}
//0 1 1 2 3 5 8 13 21 34 55 ...
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;
}
}