fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. namespace ForProgCS
  8. {
  9. class Program
  10. {
  11. static int fib_rec(int n)
  12. {
  13. if (n == 0) return 0;
  14. if (n > 0 && n < 3) return 1;
  15. else return fib_rec(n - 1) + fib_rec(n - 2);
  16. }
  17.  
  18. static int fib_iter(int n)
  19. {
  20. int a = 0;
  21. int b = 1;
  22. for (int i = 0; i < n; i++)
  23. {
  24. b += a;
  25. a = b - a;
  26. }
  27. return a;
  28. }
  29.  
  30. static void Main(string[] args)
  31. {
  32. var stoper = new Stopwatch();
  33. Console.WriteLine("Recursive start: {0}", stoper.ElapsedMilliseconds);
  34. stoper.Start();
  35. Console.WriteLine("Result: {0}", fib_rec(40));
  36. stoper.Stop();
  37. Console.WriteLine("Recursive stop: {0}", stoper.ElapsedMilliseconds);
  38.  
  39. stoper.Reset();
  40. Console.WriteLine("\nIternation start: {0}", stoper.ElapsedMilliseconds);
  41. stoper.Start();
  42. Console.WriteLine("Result: {0}", fib_iter(40));
  43. stoper.Stop();
  44. Console.WriteLine("Iteration stop: {0}", stoper.ElapsedMilliseconds);
  45. }
  46. }
  47. }
  48.  
Success #stdin #stdout 0.59s 29680KB
stdin
Standard input is empty
stdout
Recursive start: 0
Result: 102334155
Recursive stop: 586

Iternation start: 0
Result: 102334155
Iteration stop: 0