fork(1) download
  1. using static System.Console;
  2. using static System.Math;
  3.  
  4. public class Program {
  5. public static void Main() {
  6. for (int i = 0; i < 20; i++) WriteLine($"{Fib(i)}, {FibIte(i)}, {FibRec(i)}");
  7. }
  8. static int Fib(int n) {
  9. double sqrt5 = Sqrt(5);
  10. double p1 = (1 + sqrt5) / 2;
  11. double p2 = -1 * (p1 - 1);
  12. return (int)((Pow(p1, n) - Pow(p2, n)) / sqrt5);
  13. }
  14. static int FibIte(int n) {
  15. int a = 0;
  16. int b = 1;
  17. for (int i = 0; i < n; i++) {
  18. int temp = a;
  19. a = b;
  20. b = temp + b;
  21. }
  22. return a;
  23. }
  24. static int FibRec(int n) => n < 2 ? n : FibRec(n - 1) + FibRec(n - 2);
  25. }
  26.  
  27. //https://pt.stackoverflow.com/q/54456/101
Success #stdin #stdout 0.02s 15940KB
stdin
Standard input is empty
stdout
0, 0, 0
1, 1, 1
1, 1, 1
2, 2, 2
3, 3, 3
5, 5, 5
8, 8, 8
13, 13, 13
21, 21, 21
34, 34, 34
55, 55, 55
89, 89, 89
144, 144, 144
233, 233, 233
377, 377, 377
610, 610, 610
987, 987, 987
1597, 1597, 1597
2584, 2584, 2584
4181, 4181, 4181