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