about fibonacci - Programmers Heaven

Howdy, Stranger!

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

Categories

about fibonacci

gajamadagajamada 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

Comments

  • dwgeblerdwgebler 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
    :
  • gajamadagajamada Posts: 17Member
    tnks,yeah i think it would better for me to solve this problem by myself :)
  • interrupthandlinterrupthandl 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 :)
    :

Sign In or Register to comment.