fork(1) download
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. constexpr pair<double, double> helper(size_t n, const pair<double, double>& g)
  6. {
  7. return n % 2
  8. ? make_pair(g.second * g.second + g.first * g.first, g.second * g.second + 2 * g.first * g.second)
  9. : make_pair(2 * g.first * g.second - g.first * g.first, g.second * g.second + g.first * g.first);
  10. }
  11.  
  12. constexpr pair<double, double> fibonacciRecursive(size_t n)
  13. {
  14. return n < 2
  15. ? make_pair<double, double>(n, 1)
  16. : helper(n, fibonacciRecursive(n / 2));
  17. }
  18.  
  19. constexpr double fibonacci(size_t n)
  20. {
  21. return fibonacciRecursive(n).first;
  22. }
  23.  
  24.  
  25. int main() {
  26. cout << "start\t " << time(NULL) << endl;
  27. cout << fibonacci(0) << endl;
  28. cout << fibonacci(1) << endl;
  29. cout << fibonacci(2) << endl;
  30. cout << fibonacci(3) << endl;
  31. cout << fibonacci(4) << endl;
  32. cout << fibonacci(5) << endl;
  33. cout << fibonacci(100) << endl;
  34. cout << "finish\t " << time(NULL) << endl;
  35. return 0;
  36. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
start	 1396108588
0
1
1
2
3
5
3.54225e+20
finish	 1396108588