#! /usr/bin/env ruby # Finds answer to following puzzle # # There are one thousand candles on the altar and one thousand priests # in the temple. Their god asks the first priest to go and light all # the candles. Then he has the second priest go to every second candle # and put it out." # # The third goes to every third candle and, if it is not burning, he # lights it, and if it is lit, he puts it out." # # The fourth priest does this to every fourth candle, and so on. After # the process is completed with the thousandth priest, how many # candles are not lit?" candles = [] (1..1000).each do |priest| (0...1000).step(priest) do |candle| candles[candle] = !candles[candle] end end puts "#{candles.select{|c|c}.count} candles lit" puts "#{candles.select{|c|!c}.count} candles unlit"