divisors n = filter ((==0).mod n) [1..(n `div` 2 + 1)] isDoublePerfectNumber n = (sum.divisors) n == (n*2) main = mapM_ (putStrLn.show) $ filter isDoublePerfectNumber [1..10000]
def divisors(n) (1..(n/2+1)).to_a.select{|x| n % x == 0 } end def double_complete_number?(n) divisors(n).inject(0){|a,b| a+b } == 2 * n end (1..10000).to_a.each do |n| puts n if double_complete_number?(n) end
結果は同じ(あたりまえ)だけど,Rubyのほうが速かった。
^o^ >runhaskell dpn.hs 120 672 ^o^ >ruby dpn.rb 120 672