0

I'm trying to solve a Fibonacci problem and am stumbling into rounding issues.

If i = 8670007398507948658051921 then fib1 = 19386725908489880000000000.0.

My code is below - thanks for any help.

def is_fibonacci?(i)

  fib1 = Math.sqrt(5*(i**2)+4)
  fib2 = Math.sqrt(5*(i**2)-4)

  fib1 == fib1.round || fib2 == fib2.round ? true : false

end
squiguy
  • 32,370
  • 6
  • 56
  • 63
eadon
  • 13
  • 1
  • 4
  • Can you be a little more clear on what happens? In particular, 1. What command do you run using this function? 2. What is the result? and 3. What did you expect the result to be, and why? – Bob Gilmore Mar 28 '13 at 04:12

1 Answers1

1

Doing sqrt like that will not work for such big values, because sqrt returns a Float and its precision will not suffice here. I would advice you to implement your own sqrt function. There are several algorithms out there suggesting how to do that, but I personally thing using a binary search for computing the reverse for a function is the easiest:

def sqrt a
  begv = 1
  endv = a
  while endv > begv + 1
     mid = (endv + begv)/2
     if mid ** 2 <= a
        begv = mid
     else
        endv = mid
     end
  end
  return begv
end

Alternatively you may try using BigDecimal for the sqrt(simply raise to power 0.5), but I like above method better as it does not involve any double calculations.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176