Created
March 7, 2013 08:22
-
-
Save jkaihsu/5106428 to your computer and use it in GitHub Desktop.
Check, if a number i is part of the Fibonacci sequence.
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) | |
sum = [0,1] | |
x = 0 | |
y = 0 | |
while sum.max <= i | |
sum.length.times do |a| | |
x = sum[a] + sum[a-1] | |
end | |
sum = sum.push(x) | |
y +=1 | |
end | |
#puts "#{sum} #{y}" | |
sum.include?(i) | |
puts "#{sum.max} #{sum.include?(i)}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! The variable y is just for observation yes, no real purpose in the algo?