-- http://www.codewars.com/kata/54d6abf84a35017d30000b26
module Data.Complex.Gaussian.Prime where

import Data.Complex.Gaussian (Gaussian (..), norm)

isGaussianPrime :: Gaussian -> Bool
isGaussianPrime z@(Gaussian x y)
  =  n==2
  || n`mod`4==1 && isPrime n
  || y==0 && abs x `mod` 4==3 && isPrime (abs x)
  || x==0 && abs y `mod` 4==3 && isPrime (abs y)
    where
      n = norm z
      isPrime m = m > 1 && foldr (\p r -> p*p > m || ((m `rem` p) /= 0 && r)) True primes
      primes = 2 : filter isPrime [3,5..]
