0

I have a method called fibs_rec that results in an unexpected output:

def fibs_rec(n)
  if n == 1 || n == 0 
    return 1 
  else 
    a = fibs_rec(n-1) + fibs_rec(n-2) 
    puts a
    return a 
  end 
end 

fibs_rec(5)

The call fibs_rec(5) should return 1,1,2,3,5 but here is the actual output:

2
3
2
5
2
3
8

Not only is the output incorrect, it lacks a number from the beginning.

Can someone explain why is this happening?

3
  • Welcome to SO! Please read "How to Ask" and the linked pages. Please use proper grammar on SO: "i" => "I", "its" => "it's", "here" => "Here". SO is like an online reference book of programming Q&A, not a message board or forum, so grammar, punctuation, spelling, capitalization are all important. Not taking the time to do so sends a message to those who would help you. Also, please come up with a more descriptive title as that helps others find your question. Commented Mar 22, 2020 at 22:53
  • "fibs_rec(5) should return 1,1,2,3,5" – Ruby methods can only return one value whereas 1,1,2,3,5 seems like multiple values. You could return the values as an array, i.e. [1,1,2,3,5]. Or – a bit more idiomatic – you could yield each value to a block. (maybe returning an enumerator if no block is given) Commented Mar 23, 2020 at 8:13
  • @firesofhell : You are doing a puts only if n > 1, and only for the case n <= 1, fib_rec would return a 1. Hence you never see a 1 in your output. Also, your question is unclear: You are asking about the output of the function, which is what puts is taken care, but then are also mention return a value. Outputting something and returning a value are two different, unrelated things, so please make clear in your posting what you exactly want. Commented Mar 23, 2020 at 8:40

1 Answer 1

1

This is correct since your recursion is splitting into two sub-problems every time it recurses. If you want the series to appear properly then you should try doing this via dynamic programming for O(n) time complexity. As is, the first and second position won’t be printed because of the base case in the recursion.

As for the incorrect answer, it seems you have not accounted for the sequence starting with 0 index. Either find 4 index in the function which will give the fifth element or modify your function to work with position instead of index.

Sign up to request clarification or add additional context in comments.

4 Comments

The function is not using indexes it takes an argument and prints all the fib numbers between the starting and the argument. I kind of dont understand what you are saying since i am not that experienced. Could you clarify and break it down for me?
Try fibs_rec(4) if you want the 5th element in the series. That is pass -1 the element needed in the series.
Hmm, How can i patch up the output? also including the 1s
Not possible with current code implementation. Check out different approaches here geeksforgeeks.org/program-for-nth-fibonacci-number. You'd need approach 2nd or 3rd in this to get the output needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.