Created
May 20, 2011 18:11
-
-
Save cwsaylor/983452 to your computer and use it in GitHub Desktop.
lightbulb.rb
This file contains hidden or 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
#!/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