ninetynine-1.3.0: Ninety-Nine Haskell Problems
CopyrightCopyright (C) 2022 Yoo Chung
LicenseGPL-3.0-or-later
Maintainerdev@chungyc.org
Safe HaskellSafe-Inferred
LanguageGHC2021

Problems.P45

Description

Part of Ninety-Nine Haskell Problems. Some solutions are in Solutions.P45.

Synopsis

Documentation

isGaussianPrime' :: Complex Integer -> Bool Source #

Using Fermat's two-square theorem, it can be shown that a Gaussian integer \(a+bi\) is prime if and only if it falls into one of the following categories:

  • |a| is prime and \(|a| \equiv 3 \mod 4\), if \(b=0\).
  • |b| is prime and \(|b| \equiv 3 \mod 4\), if \(a=0\).
  • \( a^2 + b^2 \) is prime, if \( a \neq 0 \) and \( b \neq 0 \).

Use this property to determine whether a Gaussian integer is a Gaussian prime.

Compare with the solution for Problems.P44:

$ stack bench --benchmark-arguments="P44/isGaussianPrime P45/isGaussianPrime'"

Examples

>>> isGaussianPrime' (0 :+ 5)
False
>>> isGaussianPrime' (17 :+ 0)
False
>>> isGaussianPrime' (5 :+ 2)
True