Skip to content

Instantly share code, notes, and snippets.

@danielharan
Created January 28, 2009 17:58
Show Gist options
  • Save danielharan/54103 to your computer and use it in GitHub Desktop.
Save danielharan/54103 to your computer and use it in GitHub Desktop.
# We order a LOT of pizza at GiraffeSoft
# (there are 14 empty boxes on the table, and 3 more on the way)
# So, what's the cheapest way to buy them?
#
# in irb,
# include PizzaOptimizer
# compare([[9,11],[12,14],[14, 21]])
# 9" @ 11 => 0.270170428088094
# 12" @ 14 => 0.25788995408409
# 14" @ 21 => 0.331572798108115
module PizzaOptimizer
def cost_per_square_inch(pizza_diameter, cost)
cost.to_f / circle_area(pizza_diameter / 2)
end
def circle_area(radius)
Math::PI * (radius ** 2)
end
def compare(pizzas)
pizzas.each do |diameter, price|
puts "#{diameter}\" @ #{price} => #{cost_per_square_inch(diameter, price)}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment