fork(1) download
  1. #include <iostream>
  2.  
  3. int numofcalls;
  4.  
  5. int Fibonacci(int nNumber)
  6. {
  7. numofcalls++;
  8. if (nNumber == 0)
  9. return 0;
  10. if (nNumber == 1)
  11. return 1;
  12. return Fibonacci(nNumber-1) + Fibonacci(nNumber-2);
  13. }
  14.  
  15. // And a main program to display the first 13 Fibonacci numbers
  16. int main(void)
  17. {
  18. using namespace std;
  19. int num;
  20. for (int iii=0; iii < 13; iii++) {
  21. numofcalls = 0;
  22. num = Fibonacci(iii);
  23. cout << "The " << iii << "th Fibonacci number is " << num << "; it took " << numofcalls
  24. << " calls to calculate it." << endl;
  25. }
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
The 0th Fibonacci number is 0; it took 1 calls to calculate it.
The 1th Fibonacci number is 1; it took 1 calls to calculate it.
The 2th Fibonacci number is 1; it took 3 calls to calculate it.
The 3th Fibonacci number is 2; it took 5 calls to calculate it.
The 4th Fibonacci number is 3; it took 9 calls to calculate it.
The 5th Fibonacci number is 5; it took 15 calls to calculate it.
The 6th Fibonacci number is 8; it took 25 calls to calculate it.
The 7th Fibonacci number is 13; it took 41 calls to calculate it.
The 8th Fibonacci number is 21; it took 67 calls to calculate it.
The 9th Fibonacci number is 34; it took 109 calls to calculate it.
The 10th Fibonacci number is 55; it took 177 calls to calculate it.
The 11th Fibonacci number is 89; it took 287 calls to calculate it.
The 12th Fibonacci number is 144; it took 465 calls to calculate it.