Fibonacci series => 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...
I`th number in series is sum of (i-1)th & (i-2)th number in series where first two number in series are 0 & 1 ,these are base case for Fibonacci series.
If Fibonacci(n) represent n`th number in Fibonacci series .
then
Fibonacci(0) = 0
Fibonacci(1) = 1
.
.
.
Fibonacci(n) = Fibonacci(n-1)+Fibonacci(n-2)
Using Iteration you can easily find nth Fibonacci number .
Recursive Method for Finding n`th fibonacci Number .
\I`th number in series is sum of (i-1)th & (i-2)th number in series where first two number in series are 0 & 1 ,these are base case for Fibonacci series.
If Fibonacci(n) represent n`th number in Fibonacci series .
then
Fibonacci(0) = 0
Fibonacci(1) = 1
.
.
.
Fibonacci(n) = Fibonacci(n-1)+Fibonacci(n-2)
Using Iteration you can easily find nth Fibonacci number .
Recursive Method for Finding n`th fibonacci Number .
- //C Program
- // Recursive way of finding nth fibonacci number
- // http://beginer2cs.blogspot.com
- #include<stdio.h>
- int fibo_recursion(int n)
- {
- //base case
- if(n==0)
- return 0;
- else if(n==1) //base case
- return 1;
- else
- return (fibo_recursion(n-1)+fibo_recursion(n-2));
- }
- main(){
- int n;
- printf("Enter n: ");
- scanf("%d",&n);
- printf(" %d th Fibonacci number is = %d \n",n,fibo_recursion(n));
- }
This Approach is also called top down approach . this is very inefficient way of calculating n`th Fibonacci number as many calculation is repeated we can see in this graph
Dynamic Programming Approach reduce calculation Very efficiently . In this approach we store result of each calculation for further use in future & We fallow bottom-top approach instead of top-bottom approach (previous approach).
In bottom-top approach we first calulate 1st then 2nd ....... then n`th element .
Now Lets Solve a ProjectEuler question based on This Approach
- Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
- 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
- By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
- //C Program
- //We gonna Use bottom up approch i.e Dynamic Programming aprroch
- //http://beginer2cs.blogspot.com
- //ProjectEuler Q2
- #include<stdio.h>
- main(){
- long int Even_sum=0;
- int arr[10000];
- int i;
- //base case
- arr[0]=1; //requirement of this particuler Question
- arr[1]=1;
- for(i=2;i<10000;i++){
- arr[i]=arr[i-1]+arr[i-2]; //store result in array
- if(arr[i]>=4000000) //i`th index of array represent i`th Fibonacci number
- break;
- else
- if(arr[i]%2==0)
- Even_sum =Even_sum+arr[i];
- }
- printf("Sum of Even Fibbo Number bellow 4 million is = %ld\n",Even_sum);
- }
Output is : Sum of Even Fibbo Number bellow 4 million is = 4613732
HoW number of calculation reduces in this approch
.png)
.png)
Nice post!!thanks for sharing good post. java vogue have good collection for improve program skill visit Programming Questions And Answers
ReplyDelete