Euler problem 21.2.2

import Data.Array ( (!), accumArray )

-- ...
    
p21 :: Integer -> Integer
p21 max_ = sum $ filter isAmicable [1 .. max_ - 1]
  where     
    gen n | n > max_ = []
          | otherwise = [(i*n, n) | i <- [2 .. max_ `div` n]] ++ gen (n+1)
    arr = accumArray (+) 0 (0, max_) (gen 1)
    arrb b | b < max_ = arr!b
           | otherwise = sumProperDivisors b
    isAmicable a = let b = (arr!a)
                     in b /= a && arrb b == a

λ> :set +s
λ> p21 10000
31626
(0.06 secs, 71,654,248 bytes)
λ> p21 100000
852810
(0.69 secs, 911,377,712 bytes)
λ> p21 1000000
27220963
(9.84 secs, 11,333,936,728 bytes)
λ> p21 10000000
649734295
(134.77 secs, 139,491,977,584 bytes)
λ> 

— Me@2024-08-18 07:32:17 AM

.

.

2024.08.18 Sunday (c) All rights reserved by ACHK