# Ruby Code Golf 3: Fibonacci Sequence

Page 1 of 1

## 7 Replies - 5658 Views - Last Post: 16 May 2011 - 01:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=231309&amp;s=d49a62ca4d23c45e2a02a05a33ca8b29&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 xclite

• LIKE A BOSS

Reputation: 766
• Posts: 2,915
• Joined: 12-May 09

# Ruby Code Golf 3: Fibonacci Sequence

Posted 09 May 2011 - 12:23 PM

This is a pretty straight forward problem: Create a method that returns the nth value of the Fibonacci sequence in as few characters as possible. Extra points for not repeating work (i.e. for not implementing the naive recursive solution)!

This post has been edited by xclite: 03 October 2012 - 07:01 AM

Is This A Good Question/Topic? 1

## Replies To: Ruby Code Golf 3: Fibonacci Sequence

### #2 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 3798
• Posts: 6,405
• Joined: 08-June 10

## Re: Ruby Code Golf 3: Fibonacci Sequence

Posted 09 May 2011 - 02:08 PM

Ok, this is me having never ever used Ruby before. Take this for what it's worth:

def f(i)
if(i==0||i==1)
return i
else
return f(i-1)+f(i-2)
end
end

That assumes that i is positive, and f(0) = 0, and f(1) = 1. It's the recursive solution, but like I said, first time with Ruby.

### #3 Skaggles

• I AM THE LAW!

Reputation: 248
• Posts: 637
• Joined: 01-March 09

## Re: Ruby Code Golf 3: Fibonacci Sequence

Posted 09 May 2011 - 03:25 PM

This assumes n is positive.

Spoiler

### #4 macosxnerd101

• Self-Trained Economist

Reputation: 9037
• Posts: 33,523
• Joined: 27-December 08

## Re: Ruby Code Golf 3: Fibonacci Sequence

Posted 09 May 2011 - 03:39 PM

Solving the linear recurrence gives me O(1) time.
Spoiler

### #5 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 3798
• Posts: 6,405
• Joined: 08-June 10

## Re: Ruby Code Golf 3: Fibonacci Sequence

Posted 09 May 2011 - 03:53 PM

I guess the question is, which is more important? The efficiency or the pure character count?

### #6 Nallo

Reputation: 159
• Posts: 241
• Joined: 19-July 09

## Re: Ruby Code Golf 3: Fibonacci Sequence

Posted 10 May 2011 - 02:32 AM

I never used ruby before. This is the shortest I could come up with (not using recursion):
def f(n)
a,b=0,1
n.times{a,b=b,a+b}
a
end

This post has been edited by Nallo: 10 May 2011 - 02:43 AM

### #7 xclite

• LIKE A BOSS

Reputation: 766
• Posts: 2,915
• Joined: 12-May 09

## Re: Ruby Code Golf 3: Fibonacci Sequence

Posted 10 May 2011 - 05:27 AM

Curtis Rutland, on 09 May 2011 - 06:53 PM, said:

I guess the question is, which is more important? The efficiency or the pure character count?

Playing with both is fine - I'd probably do two, one that emphasizes each. Also, I meant to uprep you but in my morning fail-mode I downrepped you so I'm finding two random posts to make up for it.

### #8 WaeV

Reputation: 2
• Posts: 23
• Joined: 05-April 11

## Re: Ruby Code Golf 3: Fibonacci Sequence

Posted 16 May 2011 - 01:34 PM

Based on macosxnerd101's solution.

Spoiler

This post has been edited by WaeV: 16 May 2011 - 01:35 PM

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).