我想知道这一行:
{ |i| (num % i) == 0}.length == 0
有人可以解释length
在块上使用会发生什么吗?我试图回答一个关于素数的问题:
divisors = Array.new
for d in 2..(num)
divisors << d if (num % d) == 0
end
primes = divisors.select do |num|
(2..(num-1)).select { |i| (num % i) == 0}.length == 0
end