C # recursive algorithm for Fibonacci sequence

Source: Internet
Author: User

C # recursive algorithm for Fibonacci sequence

The famous Fibonacci sequence is defined as follows:

F (1) =1,f (2) =1,f (n) =f (n-1) +f (n-2), n>2

In words, the Fibonacci sequence starts with 0 and 1, and then the Fibonacci Fibonacci coefficients are added by the previous two numbers. The first few Fibonacci Fibonacci coefficients are:

0, 1, 1, 2, 3, 5, 8, 13, 21

The Fibonacci sequence is implemented using two methods, one of which uses recursive loops.


C # code

Using system;using system.collections.generic;using system.linq;using system.text;using  System.Threading.Tasks;namespace Fibonacci{    class Program     {        /// <summary>         /// c# recursive algorithm for  fibonacci sequence         ///   The famous Fibonacci series, defined as follows         /// f (1) =1,f (2) =1,f (n) =f (n-1) +f ( n-2),n>2        ///  in words, the Fibonacci sequence consists of  0  and  1  First, the Fibonacci Fibonacci coefficients are added by the previous two numbers. The first few Fibonacci Fibonacci coefficients are:         /// 1, 1, 2, 3, 5, 8,  13, 21        /// </summary>         /// <param name= "args" ></param>        ///         public const int  maxn = 64;        static int[] fib =  new int[maxn];        static void main (string[]  args)         {             /**** the first method of               int count, n, t1=0, t2=1, display;             console.write ("enter number of terms: ");             n = int32.parse (Console.ReadLine ());             console.write ("\nFibonacci Series:  {0} {1}  ", &NBSP;T1,&NBSP;T2);            count = 2;             while  (count <= n)              {                 display = t1 + t2;                 t1 = t2;                 t2 = display;                 ++count;                 console.write ("{0}   ",  display);            }             */             Console.Write ("\n "  );                         //***** second approach, using recursive loops to resolve              int[] fi=new int[20];              //fibonacci f = new fibonacci ();              for (int i=0;i<8;i++) {                 fi[i] =  fibonacci_series (i);                  //system.out.print (fi[i]+ "\ T");               &nbsP;  console.write (fi[i] +  "\ T");             }            console.readline ();         }        static  Int fibonacci_series (int n)         {             if  (n == 0)                  return 0;             else if  (n == 1)                  return 1;             else             return fibOnacci_series (n-1)  + fibonacci_series (n-2);        }     }}



Resutl:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/1A/wKioL1R4K_ag86DkAADnwwnRBpg639.jpg "title=" result "alt=" Wkiol1r4k_ag86dkaadnwwnrbpg639.jpg "/>


C # recursive algorithm for Fibonacci sequence

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.