Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why will this code will work for a smaller multiple like 10 but time out for anything over 13?

Can this code be refactored to work for larger numbers?

(p.s. I have an alternate solution but wanted to see if this code could be fixed to work for more multiples.)

#2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

multiple = 10
y = multiple
hash = Hash.new(0)

loop do
    if hash.has_value?(multiple) || y == 0
        break
    end
    for x in 1..multiple do
        if y % x == 0
            hash[y] += 1
        end
    end
    y += multiple
end

puts "Smallest number = #{y - multiple}"
hash.has_value?(multiple)
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.