On Fri Oct 31 12:48:52 2008, Paul Robinson wrote:
> 
> So the deadline has passed. Has anybody got any code they wish to share?

Here’s my mathsy solution, takes 0.224 seconds to calculate the
100,000th number in the sequence. It doesn’t need to calculate the
previous numbers in the sequence to get the next or to test if a
number is in it.


-- 
Fred O. Phillips
http://fophillips.org
BBC7 7572 755F 83E0 3209  504A E4F7 874F 1545 9D41
#!/usr/bin/env ruby
require 'matrix'

def fib n
  (Matrix[[1,1],[1,0]]**(n-1))[0,0]
end

def fibs n = 1.0/0 # infinity
  x = 0
  while x <= n
    print "#{fib(x)}, "
    x+=1
  end
end

class Integer
  def is_fibonacci?
    Math.sqrt(5 * self**2 + 4) % 1 == 0 or Math.sqrt(5 * self**2 - 4) % 1 == 0
  end
end

p (fib 10).is_fibonacci?
fibs 100

Attachment: pgpRXWoZr5ah3.pgp
Description: PGP signature

Reply via email to