Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include "fibonacci.h"
  2.  
  3. std::map<int, unsigned long long int> initializeMap() {
  4. std::map<int, unsigned long long int> m;
  5. m[0] = 0;
  6. m[1] = 1;
  7. return m;
  8. }
  9.  
  10. std::map<int, unsigned long long int> __fib_result_map = initializeMap();
  11.  
  12. unsigned long long int fibonacci(int seq) {
  13. using namespace std;
  14. map<int, unsigned long long int>& m = __fib_result_map;
  15. if(m[seq] == 0 && seq != 0) {
  16. m[seq] = fibonacci(seq - 2) + fibonacci(seq - 1);
  17. }
  18. return m[seq];
  19. }
  20.  
  21. #ifndef __fibonacci
  22. #define __fibonacci
  23.  
  24. #include <iostream>
  25. #include <map>
  26. #include <vector>
  27. #include <string>
  28. #include <algorithm>
  29.  
  30. unsigned long long int fibonacci(int seq);
  31.  
  32. #endif
  33.  
  34. #include "fibonacci.h"
  35. #include <cstdlib>
  36. using namespace std;
  37.  
  38. int main(int argc, char* argv[]) {
  39.  
  40. if(argc <= 1) {
  41. cerr << "No arguments specified. Usage: " << argv[0] << " <values>n";
  42. return 1;
  43. }
  44. for(int i = 1; i < argc; i++) {
  45. cout << fibonacci(atoi(argv[i])) << endl;
  46. }
  47. return 0;
  48. }
  49.  
  50. https://github.com/dkudriavtsev/fibonacci
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement