using static System.Console; using static System.Math; public class Program { public static void Main() { for (int i = 0; i < 20; i++) WriteLine($"{Fib(i)}, {FibIte(i)}, {FibRec(i)}"); } static int Fib(int n) { double sqrt5 = Sqrt(5); double p1 = (1 + sqrt5) / 2; double p2 = -1 * (p1 - 1); return (int)((Pow(p1, n) - Pow(p2, n)) / sqrt5); } static int FibIte(int n) { int a = 0; int b = 1; for (int i = 0; i < n; i++) { int temp = a; a = b; b = temp + b; } return a; } static int FibRec(int n) => n < 2 ? n : FibRec(n - 1) + FibRec(n - 2); } //https://pt.stackoverflow.com/q/54456/101
Standard input is empty
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