Created
March 22, 2013 02:32
-
-
Save kayline/5218514 to your computer and use it in GitHub Desktop.
First attempt at identifying Fibonacci number-foiled by floating point inaccuracy in large values
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) | |
puts "The number to test is #{i}" | |
sum_test = 5 * i ** 2 + 4 | |
diff_test = 5 * i ** 2 - 4 | |
puts "Sum test = #{sum_test} and diff test = #{diff_test}" | |
sum_test_intrt = (sum_test ** 0.5).to_i ** 2 | |
diff_test_intrt = (diff_test ** 0.5).to_i ** 2 | |
puts "Rounded sum root = #{sum_test_intrt} and diff root = #{diff_test_intrt}" | |
if sum_test == sum_test_intrt && diff_test != diff_test_intrt | |
puts "Fibby!" | |
return true | |
elsif sum_test != sum_test_intrt && diff_test == diff_test_intrt | |
puts "Other Fibby!" | |
return true | |
else | |
sum_diff = sum_test - sum_test_intrt | |
diff_diff = diff_test - diff_test_intrt | |
puts "#{sum_diff} #{diff_diff}" | |
puts "Not Fibby" | |
return false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment