Skip to content

Commit

Permalink
[#118] Add primality testing for Eisenstein integers
Browse files Browse the repository at this point in the history
  • Loading branch information
rockbmb committed Aug 16, 2018
1 parent ec8b748 commit 4c51cc8
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Math/NumberTheory/EisensteinIntegers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ module Math.NumberTheory.EisensteinIntegers
, quotRemE
, quotE
, remE

-- * Primality functions
, isPrime
) where

import GHC.Generics (Generic)

import qualified Math.NumberTheory.Primes.Testing as Testing

infix 6 :+

Expand Down Expand Up @@ -127,4 +131,17 @@ conjugate (a :+ b) = (a - b) :+ (-b)

-- | The square of the magnitude of a Eisenstein integer.
norm :: EisensteinInteger -> Integer
norm (a :+ b) = a*a - a * b + b*b
norm (a :+ b) = a*a - a * b + b*b

-- | Checks if a given @EisensteinInteger@ is prime. @EisensteinInteger@s
-- whose norm is a prime congruent to @0@ or @1@ module 3 are prime.
-- See <http://thekeep.eiu.edu/theses/2467 Bandara, Sarada, "An Exposition of the Eisenstein Integers" (2016)>,
-- page 12.
isPrime :: EisensteinInteger -> Bool
isPrime e@(a :+ b)
| e == 0 = False
| b == 0 && a `mod` 3 == 2 = Testing.isPrime a
| a == 0 && b `mod` 3 == 2 = Testing.isPrime b
| nE `mod` 3 == 0 || nE `mod` 3 == 1 = Testing.isPrime nE
| otherwise = False
where nE = norm e

0 comments on commit 4c51cc8

Please sign in to comment.