Skip to content

Instantly share code, notes, and snippets.

@cwsaylor
Created May 20, 2011 18:11
Show Gist options
  • Save cwsaylor/983452 to your computer and use it in GitHub Desktop.
Save cwsaylor/983452 to your computer and use it in GitHub Desktop.
lightbulb.rb
#!/usr/bin/env ruby
def toggle(i)
i == 0 ? 1 :0
end
# turn on every bulb
bulbs = Array.new 100, 1
# Turn off every x bulb
(2..100).each do |i|
bulbs.each_with_index do |bulb, index|
bulb_num = index + 1
bulbs[index] = toggle(bulbs[index]) if bulb_num % i == 0
end
end
# find bulb with light still on
bulbs.each_with_index do |bulb, index|
puts index + 1 if bulbs[index] == 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment