7 Replies - 9512 Views - Last Post: 16 May 2011 - 01:34 PM Rate Topic: -----

#1 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Ruby Code Golf 3: Fibonacci Sequence

Post icon  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  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,718
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: Ruby Code Golf 3: Fibonacci Sequence

Posted 09 May 2011 - 03:25 PM

This assumes n is positive.

Spoiler

Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,471
  • 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. :P
Spoiler

Was This Post Helpful? 1
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,718
  • 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?
Was This Post Helpful? 1
  • +
  • -

#6 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • 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

Was This Post Helpful? 1
  • +
  • -

#7 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Ruby Code Golf 3: Fibonacci Sequence

Posted 10 May 2011 - 05:27 AM

View PostCurtis 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.
Was This Post Helpful? 0
  • +
  • -

#8 WaeV  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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. :P

Spoiler

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1