Ruby Array #include方法无法正常工作

时间:2016-07-02 23:27:32

标签: ruby methods include

为什么include方法效果不佳?原始问题来自Euler project,问题23.我无法弄清楚如何调试它。

我的代码:

def proper_divisors(n)
  (1...n).select {|x| n % x == 0 }.inject(0){|x,y| x + y}
end 

def abundant?(n)   
  (1...n).select {|x| n % x == 0 }.inject(0){|x,y| x + y} > n
end 

def non_abundant_sums
  s = 0
  arr = (12..40).select { |n| n if abundant?(n) }
  p arr

  (1..40).each do |x|
    p x unless arr.include?(proper_divisors(x) - x)
    s = s + x unless arr.include?(proper_divisors(x) - x)
  end
  s
end

p non_abundant_sums

在上面的代码中使用p x unless arr.include?(proper_divisors(x) - x)打印1到40:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40

我想要打印的是1到39:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 31, 33, 34, 35, 37, 39

1 个答案:

答案 0 :(得分:0)

基于示例中原始方法的解决方案。

在档案problem_23.rb中:

def proper_divisors(n)
  (1...n).select {|x| n % x == 0 }.inject(0) {|x,y| x + y}
end

def abundant?(n)
  proper_divisors(n) > n
end

def non_abundant_sum(low_n, high_n, debug=false)
  puts "get all the abundant numbers within range #{low_n} to #{high_n}" if debug
  arr = (low_n..high_n).select {|n| n if abundant?(n)}
  puts arr.sort.inspect if debug
  # http://ruby-doc.org/core-2.1.2/Array.html#method-i-repeated_combination
  puts "all combinations of two abundant numbers" if debug
  arr = arr.repeated_combination(2).to_a 
  puts arr.inspect if debug

  puts "all unique sums of two abundant number combinations" if debug
  arr = arr.map {|x| x[0] + x[1]}.uniq
  puts arr.sort.inspect if debug

  puts "only select numbers within range" if debug
  arr = arr.select {|x| low_n <= x && x <= high_n}
  puts arr.inspect if debug

  puts "all positive integers within range" if debug
  arr2 = (low_n..high_n).map {|i| i}
  puts arr2.inspect if debug

  puts "all positive integers less all the sums of two abundant numbers" if debug
  arr = arr2 - arr
  puts arr.inspect if debug

  puts "sum of all the positive integers which cannot be written as the sum of two abundant numbers within range #{low_n} to #{high_n}" if debug
  arr.inject(0) {|sum,n| sum + n}
end
puts non_abundant_sum(12, 40, true)

运行代码:

$ ruby problem_23.rb 
get all the abundant numbers within range 12 to 40
[12, 18, 20, 24, 30, 36, 40]
all combinations of two abundant numbers
[[12, 12], [12, 18], [12, 20], [12, 24], [12, 30], [12, 36], [12, 40], [18, 18], [18, 20], [18, 24], [18, 30], [18, 36], [18, 40], [20, 20], [20, 24], [20, 30], [20, 36], [20, 40], [24, 24], [24, 30], [24, 36], [24, 40], [30, 30], [30, 36], [30, 40], [36, 36], [36, 40], [40, 40]]
all unique sums of two abundant number combinations
[24, 30, 32, 36, 38, 40, 42, 44, 48, 50, 52, 54, 56, 58, 60, 64, 66, 70, 72, 76, 80]
only select numbers within range
[24, 30, 32, 36, 38, 40]
all positive integers within range
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
all positive integers less all the sums of two abundant numbers
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 31, 33, 34, 35, 37, 39]
sum of all the positive integers which cannot be written as the sum of two abundant numbers within range 12 to 40
554