yz00

a questions

1.2520 is the smallest nuber that can be diveded by each of the number from 1 to 10 without any remainder.

what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Use ruby to solve this problem

b=Array.new
c = gets.chomp
a = 1..c.to_i
jc=1
a.each do |elem|
    b<<elem.to_i
end
b.each do |e|
    jc *= e 
end
x=b.max 
sum=1
co = 1*x
x.upto(jc) do |i|
        sum=0
        b.each do |f|
            sum += (i%f)
        end
        if sum == 0
            co=i
            break
    end
end
puts "The common multiple is #{co}" 

 ....

(1..20).to_a.reduce :lcm

 

posted on 2015-06-09 10:13  yz00  阅读(157)  评论(0)    收藏  举报

导航