首页 > 其他 > 详细

a questions

时间:2015-06-09 11:39:41      阅读:156      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!