Skip to content

Instantly share code, notes, and snippets.

@supertopher
Last active December 16, 2015 01:49
Show Gist options
  • Save supertopher/5358362 to your computer and use it in GitHub Desktop.
Save supertopher/5358362 to your computer and use it in GitHub Desktop.
Large fibonacci check
def is_fibonacci?(i)
require "bigdecimal"
require "bigdecimal/math"
include BigMath
plusfour = 5 * i**2 + 4
minusfour = 5 * i**2 - 4
#second argument is number of decimels for all big num arguments
plusfour = BigDecimal.new(plusfour, 5)
minusfour = BigDecimal.new(minusfour, 5)
plusfour = BigMath.sqrt(plusfour, 5)
minusfour = BigMath.sqrt(minusfour, 5)
plusremainder = plusfour % 1
minusremainder = minusfour % 1
if plusremainder == 0.0 && minusremainder == 0.0
return false
elsif plusremainder == 0.0
return true
elsif minusremainder == 0.0
return true
else
return false
end
end
@supertopher
Copy link
Author

Big num's are needed so as not to truncate bad remainders on very large fibonacci checks… implemented on all numbers for simplicity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment