kenaxt #3250(2007/10/05 03:03 GMT) [ Ruby ] Rating-1/1=-1.00
約数のリスト(重複あり)を、Hashでユニークにする、というトリッキーな事をしています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
def yakusu(n) r = [1,1] #--- hash seed s = 2 while n >= s while n%s > 0 if s * s > n #--- can't divide s = n break end s += 1 end r += r.map {|x|x * s} n /= s end Hash[*r].values #--- to uniq end def kanzen(n, m) r = [] (1..n).each {|x| r << x if (m * x == yakusu(x).inject(0){|sum,a|sum + a}) } r end p kanzen(10**2, 2) == [6, 28] # => true p kanzen(10**4, 3) # => [120, 672]
Rating-1/1=-1.00-0+
[ reply ]
kenaxt #3250() [ Ruby ] Rating-1/1=-1.00
Rating-1/1=-1.00-0+
[ reply ]