Last active
December 16, 2015 01:49
-
-
Save supertopher/5358362 to your computer and use it in GitHub Desktop.
Large fibonacci check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Big num's are needed so as not to truncate bad remainders on very large fibonacci checks… implemented on all numbers for simplicity