嵌套的每个语句都不起作用
Nested each statements aren't working
我正在尝试在 Ruby 中编写一个程序来查找给定数字的质因数,而不使用 Ruby 的 .Prime class。我写的代码对我来说似乎是合乎逻辑的(如果是迂回的话),但出于某种原因,最后一行代码似乎不起作用,我也不知道为什么。该代码仍然只是 returns 给定数字的所有因数,而不是素数。
def prime(n)
array = []
r = Range.new(2, n - 1)
r.each { |x| array << x if n % x == 0 }
array.each { |x| puts x if (2..x - 1).each { |p| x % p != 0 }}
end
您想检查 (2..x - 1)
中的 .all?
个数字,而不是 .each
:
def prime(n)
array = []
r = Range.new(2, n - 1)
r.each { |x| array << x if n % x == 0 }
array.each { |x| puts x if (2..x - 1).all? { |p| x % p != 0 }}
end
顺便说一句,我也建议给你的变量起一个有意义的名字,因为现在你的代码真的很难阅读,这样的实现更清晰:
def prime?(num)
return false if num < 2
(2..Math.sqrt(num).to_i).each do |i|
return false if num % i == 0
end
true
end
def prime_factors(num)
(2..num - 1).inject([]) do |factors, i|
(num % i == 0) ? factors << i : factors
end.select { |factor| prime?(factor) }
end
p prime_factors(44)
将这个单一功能拆分为三个 factors n
、prime n
和 prime_factors n
,然后将它们组合在一起似乎更合乎逻辑。这种设计更加模块化,更适合测试各个部分。例如如果所有的逻辑都放在一起,你怎么能肯定你的程序正确地找到了素数?
def factors n
(2..(n/2).floor).select {|x| n % x == 0 }
end
def prime? n
return false unless n >= 2
(2..Math.sqrt(n).floor).all? {|x| n % x != 0 }
end
def prime_factors n
factors(n).select {|x| prime? x }
end
def prime_factors(n)
# It's clear what factors will hold.
# It helps you when you or someone else read it out
# after a while.
factors = []
# Keep the r. No problem.
r = Range.new(2, n-1)
# Array#select is nice for this task.
# select is good in case where you need to filter out certain
# values out of a list. It nicely separates the selection task.
factors = r.select { |x| n % x == 0 }
# It's better not to go for double block on a single line.
# It will become more readable this way.
# Also it's better to handle how the result is displayed outside
# the function and just to return the computed values.
factors.select do |x|
(2..x - 1).all? { |p| x % p != 0 }
end
end
我正在尝试在 Ruby 中编写一个程序来查找给定数字的质因数,而不使用 Ruby 的 .Prime class。我写的代码对我来说似乎是合乎逻辑的(如果是迂回的话),但出于某种原因,最后一行代码似乎不起作用,我也不知道为什么。该代码仍然只是 returns 给定数字的所有因数,而不是素数。
def prime(n)
array = []
r = Range.new(2, n - 1)
r.each { |x| array << x if n % x == 0 }
array.each { |x| puts x if (2..x - 1).each { |p| x % p != 0 }}
end
您想检查 (2..x - 1)
中的 .all?
个数字,而不是 .each
:
def prime(n)
array = []
r = Range.new(2, n - 1)
r.each { |x| array << x if n % x == 0 }
array.each { |x| puts x if (2..x - 1).all? { |p| x % p != 0 }}
end
顺便说一句,我也建议给你的变量起一个有意义的名字,因为现在你的代码真的很难阅读,这样的实现更清晰:
def prime?(num)
return false if num < 2
(2..Math.sqrt(num).to_i).each do |i|
return false if num % i == 0
end
true
end
def prime_factors(num)
(2..num - 1).inject([]) do |factors, i|
(num % i == 0) ? factors << i : factors
end.select { |factor| prime?(factor) }
end
p prime_factors(44)
将这个单一功能拆分为三个 factors n
、prime n
和 prime_factors n
,然后将它们组合在一起似乎更合乎逻辑。这种设计更加模块化,更适合测试各个部分。例如如果所有的逻辑都放在一起,你怎么能肯定你的程序正确地找到了素数?
def factors n
(2..(n/2).floor).select {|x| n % x == 0 }
end
def prime? n
return false unless n >= 2
(2..Math.sqrt(n).floor).all? {|x| n % x != 0 }
end
def prime_factors n
factors(n).select {|x| prime? x }
end
def prime_factors(n)
# It's clear what factors will hold.
# It helps you when you or someone else read it out
# after a while.
factors = []
# Keep the r. No problem.
r = Range.new(2, n-1)
# Array#select is nice for this task.
# select is good in case where you need to filter out certain
# values out of a list. It nicely separates the selection task.
factors = r.select { |x| n % x == 0 }
# It's better not to go for double block on a single line.
# It will become more readable this way.
# Also it's better to handle how the result is displayed outside
# the function and just to return the computed values.
factors.select do |x|
(2..x - 1).all? { |p| x % p != 0 }
end
end