C # calculate the value of the 30th items in the Fibonacci series (recursive and non-recursive ),
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Fibonacci sequence sum {class Program {static void Main (string [] args) {Console. writeLine ("the value of the 30th item is: {0}", Fn (30); Console. writeLine ("the value of the 30th item is: {0}", Fnl (30); Console. writeLine ("the value of the 30th item is: {0}", Fn2 (30); Console. readKey () ;}// recursive algorithm static int Fn (int n) {if (n <= 0) return 0; if (n = 1 | n = 2) return 1; return Fn (n-1) + Fn (n-2);} // generally, static int Fnl (int n) {if (n <= 0) return 0; int a = 1; int B = 1; int c = 1; for (int I = 3; I <= n; I ++) {c = checked (a + B); a = B; B = c;} return c;} // calculate static int Fn2 (int n) using two temporary variables) {if (n <= 0) return 0; int a = 1; int B = 1; for (int I = 3; I <= n; I ++) {B = checked (a + B); a = B-a;} return B ;}}}
View Code
I know a little about the time complexity of the algorithm and how to calculate it. But I still feel that I am not good at understanding it and cannot fully evaluate the efficiency of an algorithm.