{- Copyright (C) 2011 Dr. Alistair Ward This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. -} {- | [@AUTHOR@] Dr. Alistair Ward [@DESCRIPTION@] Exports a common interface for implementations of /prime-number/ generators. -} module Factory.Math.Primes( -- * Type-classes Algorithmic(..), -- * Functions primorial, mersenneNumbers ) where import qualified Control.DeepSeq import qualified Data.Array.IArray -- | Defines the methods expected of a /prime-number/ generator. class Algorithmic algorithm where primes :: (Control.DeepSeq.NFData i, Data.Array.IArray.Ix i, Integral i) => algorithm -> [i] -- ^ Returns the constant, infinite, list of primes. {- | * Returns the constant list, defining the /Primorial/. * <https://en.wikipedia.org/wiki/Primorial>. * <https://mathworld.wolfram.com/Primorial.html>. -} primorial :: ( Algorithmic algorithm, Control.DeepSeq.NFData i, Data.Array.IArray.Ix i, Integral i ) => algorithm -> [i] primorial :: algorithm -> [i] primorial = (i -> i -> i) -> i -> [i] -> [i] forall b a. (b -> a -> b) -> b -> [a] -> [b] scanl i -> i -> i forall a. Num a => a -> a -> a (*) i 1 ([i] -> [i]) -> (algorithm -> [i]) -> algorithm -> [i] forall b c a. (b -> c) -> (a -> b) -> a -> c . algorithm -> [i] forall algorithm i. (Algorithmic algorithm, NFData i, Ix i, Integral i) => algorithm -> [i] primes {- | * Returns the constant ordered infinite list of /Mersenne numbers/. * Only the subset composed from a prime exponent is returned; which is a strict superset of the /Mersenne Primes/. * <https://en.wikipedia.org/wiki/Mersenne_prime>. * <https://mathworld.wolfram.com/MersenneNumber.html> -} mersenneNumbers :: (Algorithmic algorithm, Integral i) => algorithm -> [i] mersenneNumbers :: algorithm -> [i] mersenneNumbers algorithm algorithm = (Int -> i) -> [Int] -> [i] forall a b. (a -> b) -> [a] -> [b] map (i -> i forall a. Enum a => a -> a pred (i -> i) -> (Int -> i) -> Int -> i forall b c a. (b -> c) -> (a -> b) -> a -> c . (i 2 i -> Int -> i forall a b. (Num a, Integral b) => a -> b -> a ^)) (algorithm -> [Int] forall algorithm i. (Algorithmic algorithm, NFData i, Ix i, Integral i) => algorithm -> [i] primes algorithm algorithm :: [Int]) -- Whilst the exponentiation could be parallelised, not all values are known to be required.