【问题标题】:Ruby Array#include method not working as intendedRuby Array#include 方法未按预期工作
【发布时间】:2016-07-02 23:27:32
【问题描述】:

为什么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

【问题讨论】:

  • 您的arr 等于[12, 18, 20, 24, 30, 36, 40]。是不是应该这样?
  • @Aleksey 是的!
  • 为什么abundant 不使用proper_divisors?这些方法几乎相同。
  • @Aleksey 我不知道如何让它变得简单。你能帮我解决这个问题吗?
  • 坦率地说,我从数学的角度看不懂问题。您的 proper_divisors(x)- x 中没有一个包含在 arr 中。从数学的角度来看是不是错了?

标签: ruby methods include


【解决方案1】:

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

在文件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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 2019-04-22
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
相关资源
最近更新 更多