Friday, July 31, 2009

C Programming Language: Using Recursion to Print the Fibonacci Series?

The Fibonacci series





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





begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.





For this problem, we are asked to write a recursive function fib(n) that calculates the nth Fibonacci number. Recursion MUST be used.





In an earlier problem, we where asked to do the exact same thing, except we where to NOT use recursion. For that problem, I used the following code (between the dotted lines):





--------------------------------------...





main()


{


int c=1,f=0,s=1;


int i;


printf("%d%20d\n",f,f);


printf("%d%20d\n",s,s);


for (i=2; i%26lt;5||f%26lt;s; i++)


{


c=f+s;


f=s;


s=c;


printf("%d%20d\n",i,c);


}


}





--------------------------------------...





Which gave the correct output:








0 0


1 1


2 1


3 2


4 3


5 5


6 8


7 13


8 21


9 34


10 55


11 89


12 144


13 233


14 377


15 610


16 987


17 1597


18 2584


19 4181


20 6765


21 10946


22 17711


23 28657


24 46368


25 75025


26 121393


27 196418


28 317811


29 514229


30 832040


31 1346269


32 2178309


33 3524578


34 5702887


35 9227465


36 14930352


37 24157817


38 39088169


39 63245986


40 102334155


41 165580141


42 267914296


43 433494437


44 701408733


45 1134903170


46 1836311903


47 -1323752223














While I was able to come up with a code that works nonrecursively, I am not sure how to write a program that will print out the Fibonacci series by using recursion. Help would be greatly appreciated.

C Programming Language: Using Recursion to Print the Fibonacci Series?
That's interesting. Usually they have students try these methods in the opposite order, to show that recursive algorithms can be transformed into non-recursive ones.





The way to solve this is by going back to the mathematical definition of the Fibonacci series:


f(1) = 1


f(2) = 1


f(n) = f(n-1) + f(n-2)





So, to find a particular number in the Fibonacci series, you just need to calculate the previous two numbers and add them together.





If you take that last formula and simply translate it into a C function, you'll be most of the way there. Don't forget to treat n=1 and n=2 as special cases.
Reply:Simple: this is in my book:





//Begin function Fibonacci





int Fibonacci(int n)


{


if(n==0||n==1)


return 1;


else


return Fibonacci(n-1)+Fibonacci(n-2);


}





//End function Fibonacci
Reply:#include %26lt;iostream%26gt;





int main()


{


//define first two fibonacci series numbers.


int fib1 = 0;


int fib2 = 1;





//declare the variable to store the next number of fibonacci series


int fib3;





//declare the variable to store how many numbers to be printed. Default is 2.


int numbers = 2;





//the counter to keep track how many numbers are printed.


int counter = 2;





//Ask user how many numbers of the fibonacci series need to be printed.


std::cout %26lt;%26lt; "How many Fibonacci number you need ? : " ;





//Store the number.


std::cin %26gt;%26gt; numbers;





//If number entered is less than 3, exit the program.


if (numbers %26lt; 3) return 0;





//Print the first two element.


std::cout %26lt;%26lt; fib1 %26lt;%26lt; "\t" %26lt;%26lt; fib2;





//do-while loop to calculate the new element of the series and printing the same.


do {


counter++;


fib3 = fib1 + fib2;


std::cout %26lt;%26lt; "\t" %26lt;%26lt; fib3;


fib1 = fib2;


fib2 = fib3;


} while (counter %26lt;= numbers);





std::cout %26lt;%26lt; std::endl;


system("pause");


return 0;


}

nobile

No comments:

Post a Comment