C # calculate the value of the 30th items in the Fibonacci series (recursive and non-recursive ),

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.