# -*- coding: utf-8 -*- # http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2023 # require 'mathn' # 約数関数でnの約数の総和を求める. def d(n) n.prime_division.reduce(1){|mul, pd| mul * ((0 .. pd[1]).reduce(0){|sum, e| sum + (pd[0] ** e) }) } - n end # 28123以下の過剰数の集合を作る. abundant_numbers = (1 .. 28123).select{|i| i < d(i)} # 2つの過剰数の和の集合を作る. abundant_number_sums = abundant_numbers.each_with_index.map do |a, i| (i ... abundant_numbers.size).map do |j| a + abundant_numbers[j] end end.flatten.uniq # 28123以下の正の整数の集合から2つの過剰数の和の集合の差の総和をとる. puts ((1 .. 28123).to_a - abundant_number_sums).reduce(:+)