码迷,mamicode.com
首页 > 其他好文 > 详细

a questions

时间:2015-06-09 11:39:53      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

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

 

a questions

标签:

原文地址:http://www.cnblogs.com/yz00/p/4562707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!