#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Posts: 17Member
anybody can help me how to show the first 30 numbers of a Fibonacci sequence?the output must be 1 1 2 3 5 8 and so on

• Posts: 190Member
Yes, but I don't write homework code for other people.
The Fibonacci sequence can be solved recursively or iteratively.
I presume you are meant to write a function that takes the term to find as a parameter and returns the relevant Fibonacci value. So, to provide you with some C-ish pseudocode for a RECURSIVE solution:

Fib(integer n) returns integer
{
if (n < 1)
return -1 // Error, no zeroth term.
if (n <= 2) // First two terms are 1.
return 1
else
return Fib(n-1) + Fib(n-2) // Else it is sum of prev. 2 terms.
}

Can you figure out why this works? Also, look into why the recursive solution is far less efficient (try finding the 40th term, you'll be sitting at your computer for a while...) than an iterative solution. Try and write an iterative solution, because any problem that can be solved recursively can be solved iteratively and vice versa. Your key phrase for this problem (the solution I present anyway) is EXPONENTIAL COMPLEXITY. Look this up further.
Hope this helps.

: anybody can help me how to show the first 30 numbers of a Fibonacci sequence?the output must be 1 1 2 3 5 8 and so on
:
• Posts: 17Member
tnks,yeah i think it would better for me to solve this problem by myself
• Posts: 137Member
[red] dwgebbler is correct we are not here to supply u code , but only to help u out of problems in code itself.pseudocode is best we can give u.
[/red]

: tnks,yeah i think it would better for me to solve this problem by myself
:

• Posts: 67Member