fork download
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6. using std::cerr;
  7.  
  8. #include <stdexcept>
  9. using std::out_of_range;
  10.  
  11. #include <limits>
  12.  
  13. class OUTofRage : public out_of_range
  14. {
  15. public:
  16. OUTofRage()
  17. : out_of_range("Out Of Range\n") {}
  18. } ;
  19.  
  20. long long fibonacci(long long target, long long numberOne, long long numberTwo);
  21.  
  22. int main() {
  23. try {
  24. cout << endl << fibonacci(91, 0, 1) << endl << endl;
  25. }
  26. catch( std::exception &ex ) {
  27. std::cout << "error:" << ex.what() << std::endl;
  28. }
  29. return 0;
  30. }
  31.  
  32. long long fibonacci(long long target, long long numberOne, long long numberTwo)
  33. {
  34. if(std::numeric_limits<decltype(numberOne)>::max() - numberOne < numberTwo)
  35. throw OUTofRage();
  36.  
  37. if(target == 0)
  38. return numberOne + numberTwo;
  39.  
  40. return fibonacci(target-1, numberTwo, numberOne + numberTwo);
  41. }
Success #stdin #stdout 0.01s 5500KB
stdin
Standard input is empty
stdout
error:Out Of Range