################################################################
#
# HAL 9000's Fibonacci Calculator
#
# Version 1.0
# Author: kaolincash
# Date: 15th July 2015
# Email: kaolin@maia.cash
#
################################################################
def fib(n)
phi = (1 + Math.sqrt(5)) / 2
return "I'm sorry Dave, I'm afraid I can't do that." unless n >= 0
return ((phi**n) / Math.sqrt(5)).round
end
# Original ordinalize code by alterisian
def ordinalise(avaunt)
if (11..13).include?(avaunt % 100)
"#{avaunt}th"
else
case avaunt % 10
when 1; "#{avaunt}st"
when 2; "#{avaunt}nd"
when 3; "#{avaunt}rd"
else "#{avaunt}th"
end
end
end
################################################################
stop = "n"
restart = "Will you stop, Dave? y/n"
loop {
puts "Please select from the following options:"
puts "To see the first x numbers in the Fibonacci sequence, press: 0"
puts "To see the nth number in the Fibonacci sequence, press: 1"
selection = gets.chomp
if selection == "0" or selection == "1"
selected = true
else
selected = false
end
while selected == false do
puts "Just what do you think you're doing, Dave?"
sleep 2
puts
puts "To see the first x numbers in the Fibonacci sequence, press: 0"
puts "To see the nth number in the Fibonacci sequence, press: 1"
selection = gets.chomp
if selection == "0" or selection == "1"
selected = true
else
selected = false
end
end
if selection == "0"
puts "How many numbers of the Fibonacci sequence do you want to see?"
avaunt = gets.chomp.to_i
if avaunt == "0"
puts "I'm sorry Dave, I'm afraid I can't do that."
else
puts
puts "The first #{avaunt} numbers of the Fibonacci sequence are:"
avaunt.times {
|avaunt|
puts fib(avaunt)
}
end
elsif selection == "1"
puts "Which number in the Fibonacci sequence do you want to see?"
avaunt = gets.chomp.to_i
puts "The #{ordinalise(avaunt)} number of the Fibonacci sequence is:"
puts fib(avaunt - 1)
end
puts
puts restart
stop = gets.chomp
stop.downcase!
until stop == "y" or stop == "n" do
puts "Just what do you think you're doing, Dave?"
sleep 2
puts
puts restart
stop = gets.chomp
stop.downcase!
end
if stop == "y"
puts
puts "Thank you for a very enjoyable game."
gets.chomp
break
else
puts
end
}