# Project Euler 解答

## Project Euler Problem 021

Project Euler Problem 021

```import Control.Monad hiding (join)
import Data.List hiding (union)
--003より
primes :: [Integer]
primes = 2 : ([3,5..] `minus` join [[p*p, p*p+2*p..] | p <- primes'])
where
primes' = 3 : ([5,7..] `minus` join [[p*p, p*p+2*p..] | p <- primes'])
join  ((x:xs):t)    = x : union xs (join (pairs t))
pairs ((x:xs):ys:t) = (x : union xs ys) : pairs t
union (x:xs) (y:ys) = case (compare x y) of
LT -> x : union  xs  (y:ys)
EQ -> x : union  xs     ys
GT -> y : union (x:xs)  ys
union  xs     []    = xs
union  []     ys    = ys
minus (x:xs) (y:ys) = case (compare x y) of
LT -> x : minus  xs  (y:ys)
EQ ->     minus  xs     ys
GT ->     minus (x:xs)  ys
minus  xs     _     = xs
--必要に応じてgroupする
factor :: Integer -> [Integer]
factor n = factorimpl n primes where
factorimpl n pri@(p:xs) =
if p*p>n then [n]
else if n`rem` p == 0 then
p:factorimpl (n `quot` p) pri
else
factorimpl n xs
--等比数列の和
tohi :: Integral a => [a] -> a
tohi [x] = x+1
tohi l@(x:xs) = (x^(length l+1)-1) `quot` (x-1)

dn2 :: Integer -> Integer
dn2 n =  (product. map tohi .group . factor) n - n

--友愛数は対象的なので、両方ループさせる必要は無い
amiables :: Integer -> [(Integer, Integer)]
amiables n = [(l,r)|l<-[2..n],let r= dn2 l,r[x,y]) ans1
```
• はじめに
• プロジェクトオイラー問題
• リンク等