ピタゴラス数 その2
前回のphは、(3,4,5),(4,3,5),(6,8,10)のように、単に順番を入れ替えただけのやつや、互いに素でないのが混ざっていて楽しくないので、x<=y<=zかつx,y,zの公約数が1のみの組み合わせだけを返すように改造してみた。
factors :: Int -> [Int] factors x = [n | n <- [1..x], x `mod` n == 0] min2 :: [Int] -> Int min2 [x] = x min2 (x:xs) | x < min2 xs = x | otherwise = min2 xs find :: Int -> [Int] -> Bool find n [] = False find n (x:xs) = if n == x then True else find n xs common_factors :: Int -> Int -> Int -> [Int] common_factors x y z = [n|n <- [1..min2[x,y,z] `div` 2], find n (factors x), find n (factors y), find n (factors z)] ph :: Int -> [(Int,Int,Int)] ph n = [(x,y,z)| x<-[1..n], y<-[x..n], z<-[y..n], z^2==x^2+y^2, common_factors x y z == [1]]
min2なんかは、眺めていると動くのが不思議な気がしてくる