2016-07-02 2 views
0

Почему метод include не работает? Оригинальный вопрос - от проблемы Euler project, проблема 23. Я не мог понять, как ее отладить.Ruby Array # include метод не работает должным образом

Мой код:

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 
+0

Ваш 'arr' равен' [12, 18, 20, 24, 30, 36, 40] '. Так оно и должно быть? – Aleksey

+0

@Aleksey да это !! – Haohsuanwang

+0

Почему 'обильный' не использует' proper_divisors'? Эти методы почти одинаковы. – Aleksey

ответ

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 
+1

Спасибо! @soawesomeman – Haohsuanwang

+0

@Haohsuanwang Если этого решения достаточно, подумайте о принятии его в качестве ответа: http://meta.stackexchange.com/a/5235/249307 – SoAwesomeMan

Смежные вопросы