您的位置:首页 > 产品设计 > UI/UE

a questions

2015-06-09 10:13 417 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: