Skip to content

Instantly share code, notes, and snippets.

@dproject21
Created August 11, 2012 15:14
Show Gist options
  • Save dproject21/3325194 to your computer and use it in GitHub Desktop.
Save dproject21/3325194 to your computer and use it in GitHub Desktop.
フィボナッチ数かどうかを検証する。
require './fib_map'
@fib_map = FibMap.new(100)
(0..100).each {|item|
if @fib_map.fibonacci?(item) then
p 'HENTAI!'
else
p item
end
}
class FibMap
@@fibonacci = []
def initialize(num)
@a = 0
@b = 0
0.upto(num) do |item|
if item == 0 then
@@fibonacci << item
@a = 0
@b = 1
elsif item == @a + @b then
@@fibonacci << item
@a = @b
@b = item
end
end
end
def print_fibonacci
@@fibonacci.each do |item|
p item
end
end
def fibonacci?(num)
@@fibonacci.index(num) != nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment