arithmoi 0.9.0.0 → 0.10.0.0
raw patch · 74 files changed
+1343/−1405 lines, 74 filesdep +constraintsdep ~semiringsPVP ok
version bump matches the API change (PVP)
Dependencies added: constraints
Dependency ranges changed: semirings
API changes (from Hackage documentation)
- Math.NumberTheory.Curves.Montgomery: [SomePoint] :: (KnownNat a24, KnownNat n) => Point a24 n -> SomePoint
- Math.NumberTheory.Curves.Montgomery: add :: KnownNat n => Point a24 n -> Point a24 n -> Point a24 n -> Point a24 n
- Math.NumberTheory.Curves.Montgomery: data Point (a24 :: Nat) (n :: Nat)
- Math.NumberTheory.Curves.Montgomery: data SomePoint
- Math.NumberTheory.Curves.Montgomery: double :: (KnownNat a24, KnownNat n) => Point a24 n -> Point a24 n
- Math.NumberTheory.Curves.Montgomery: instance (GHC.TypeNats.KnownNat a24, GHC.TypeNats.KnownNat n) => GHC.Show.Show (Math.NumberTheory.Curves.Montgomery.Point a24 n)
- Math.NumberTheory.Curves.Montgomery: instance GHC.Show.Show Math.NumberTheory.Curves.Montgomery.SomePoint
- Math.NumberTheory.Curves.Montgomery: instance GHC.TypeNats.KnownNat n => GHC.Classes.Eq (Math.NumberTheory.Curves.Montgomery.Point a24 n)
- Math.NumberTheory.Curves.Montgomery: multiply :: (KnownNat a24, KnownNat n) => Word -> Point a24 n -> Point a24 n
- Math.NumberTheory.Curves.Montgomery: newPoint :: Integer -> Integer -> Maybe SomePoint
- Math.NumberTheory.Curves.Montgomery: pointA24 :: forall a24 n. KnownNat a24 => Point a24 n -> Integer
- Math.NumberTheory.Curves.Montgomery: pointN :: forall a24 n. KnownNat n => Point a24 n -> Integer
- Math.NumberTheory.Curves.Montgomery: pointX :: Point a24 n -> Integer
- Math.NumberTheory.Curves.Montgomery: pointZ :: Point a24 n -> Integer
- Math.NumberTheory.Euclidean: WrappedIntegral :: a -> WrappedIntegral a
- Math.NumberTheory.Euclidean: [unWrappedIntegral] :: WrappedIntegral a -> a
- Math.NumberTheory.Euclidean: div :: Euclidean a => a -> a -> a
- Math.NumberTheory.Euclidean: divMod :: Euclidean a => a -> a -> (a, a)
- Math.NumberTheory.Euclidean: instance GHC.Classes.Eq a => GHC.Classes.Eq (Math.NumberTheory.Euclidean.WrappedIntegral a)
- Math.NumberTheory.Euclidean: instance GHC.Classes.Ord a => GHC.Classes.Ord (Math.NumberTheory.Euclidean.WrappedIntegral a)
- Math.NumberTheory.Euclidean: instance GHC.Enum.Enum a => GHC.Enum.Enum (Math.NumberTheory.Euclidean.WrappedIntegral a)
- Math.NumberTheory.Euclidean: instance GHC.Num.Num a => GHC.Num.Num (Math.NumberTheory.Euclidean.WrappedIntegral a)
- Math.NumberTheory.Euclidean: instance GHC.Real.Integral a => GHC.Real.Integral (Math.NumberTheory.Euclidean.WrappedIntegral a)
- Math.NumberTheory.Euclidean: instance GHC.Real.Integral a => Math.NumberTheory.Euclidean.Euclidean (Math.NumberTheory.Euclidean.WrappedIntegral a)
- Math.NumberTheory.Euclidean: instance GHC.Real.Real a => GHC.Real.Real (Math.NumberTheory.Euclidean.WrappedIntegral a)
- Math.NumberTheory.Euclidean: instance GHC.Show.Show a => GHC.Show.Show (Math.NumberTheory.Euclidean.WrappedIntegral a)
- Math.NumberTheory.Euclidean: instance Math.NumberTheory.Euclidean.Euclidean GHC.Integer.Type.Integer
- Math.NumberTheory.Euclidean: instance Math.NumberTheory.Euclidean.Euclidean GHC.Natural.Natural
- Math.NumberTheory.Euclidean: instance Math.NumberTheory.Euclidean.Euclidean GHC.Types.Int
- Math.NumberTheory.Euclidean: instance Math.NumberTheory.Euclidean.Euclidean GHC.Types.Word
- Math.NumberTheory.Euclidean: mod :: Euclidean a => a -> a -> a
- Math.NumberTheory.Euclidean.Coprimes: instance (Math.NumberTheory.Euclidean.Euclidean a, GHC.Classes.Eq b, GHC.Num.Num b) => GHC.Base.Monoid (Math.NumberTheory.Euclidean.Coprimes.Coprimes a b)
- Math.NumberTheory.Euclidean.Coprimes: instance (Math.NumberTheory.Euclidean.Euclidean a, GHC.Classes.Eq b, GHC.Num.Num b) => GHC.Base.Semigroup (Math.NumberTheory.Euclidean.Coprimes.Coprimes a b)
- Math.NumberTheory.Moduli.PrimitiveRoot: CG2 :: CyclicGroup a
- Math.NumberTheory.Moduli.PrimitiveRoot: CG4 :: CyclicGroup a
- Math.NumberTheory.Moduli.PrimitiveRoot: CGDoubleOddPrimePower :: Prime a -> Word -> CyclicGroup a
- Math.NumberTheory.Moduli.PrimitiveRoot: CGOddPrimePower :: Prime a -> Word -> CyclicGroup a
- Math.NumberTheory.Moduli.PrimitiveRoot: cyclicGroupFromModulo :: (Ord a, Integral a, UniqueFactorisation a) => a -> Maybe (CyclicGroup a)
- Math.NumberTheory.Moduli.PrimitiveRoot: cyclicGroupToModulo :: Euclidean a => CyclicGroup a -> Prefactored a
- Math.NumberTheory.Moduli.PrimitiveRoot: data CyclicGroup a
- Math.NumberTheory.Moduli.PrimitiveRoot: getGroup :: PrimitiveRoot m -> CyclicGroup Natural
- Math.NumberTheory.Moduli.PrimitiveRoot: groupSize :: (Euclidean a, UniqueFactorisation a) => CyclicGroup a -> Prefactored a
- Math.NumberTheory.Moduli.PrimitiveRoot: instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Math.NumberTheory.Moduli.PrimitiveRoot.CyclicGroup a)
- Math.NumberTheory.Moduli.PrimitiveRoot: instance GHC.Classes.Eq a => GHC.Classes.Eq (Math.NumberTheory.Moduli.PrimitiveRoot.CyclicGroup a)
- Math.NumberTheory.Moduli.PrimitiveRoot: instance GHC.Generics.Generic (Math.NumberTheory.Moduli.PrimitiveRoot.CyclicGroup a)
- Math.NumberTheory.Moduli.PrimitiveRoot: instance GHC.Show.Show a => GHC.Show.Show (Math.NumberTheory.Moduli.PrimitiveRoot.CyclicGroup a)
- Math.NumberTheory.Moduli.PrimitiveRoot: isPrimitiveRoot' :: (Integral a, UniqueFactorisation a) => CyclicGroup a -> a -> Bool
- Math.NumberTheory.Moduli.Sqrt: sqrtModF :: Integer -> [(Integer, Int)] -> Maybe Integer
- Math.NumberTheory.Moduli.Sqrt: sqrtModFList :: Integer -> [(Integer, Int)] -> [Integer]
- Math.NumberTheory.Moduli.Sqrt: sqrtModP :: Integer -> Integer -> Maybe Integer
- Math.NumberTheory.Moduli.Sqrt: sqrtModP' :: Integer -> Integer -> Integer
- Math.NumberTheory.Moduli.Sqrt: sqrtModPList :: Integer -> Integer -> [Integer]
- Math.NumberTheory.Moduli.Sqrt: sqrtModPP :: Integer -> (Integer, Int) -> Maybe Integer
- Math.NumberTheory.Moduli.Sqrt: sqrtModPPList :: Integer -> (Integer, Int) -> [Integer]
- Math.NumberTheory.Moduli.Sqrt: tonelliShanks :: Integer -> Integer -> Integer
- Math.NumberTheory.MoebiusInversion.Int: generalInversion :: (Int -> Int) -> Int -> Int
- Math.NumberTheory.MoebiusInversion.Int: totientSum :: Int -> Int
- Math.NumberTheory.Prefactored: instance (Math.NumberTheory.Euclidean.Euclidean a, Math.NumberTheory.Primes.UniqueFactorisation a) => Math.NumberTheory.Primes.UniqueFactorisation (Math.NumberTheory.Prefactored.Prefactored a)
- Math.NumberTheory.Prefactored: instance Math.NumberTheory.Euclidean.Euclidean a => GHC.Num.Num (Math.NumberTheory.Prefactored.Prefactored a)
- Math.NumberTheory.Quadratic.EisensteinIntegers: instance Math.NumberTheory.Euclidean.Euclidean Math.NumberTheory.Quadratic.EisensteinIntegers.EisensteinInteger
- Math.NumberTheory.Quadratic.GaussianIntegers: instance Math.NumberTheory.Euclidean.Euclidean Math.NumberTheory.Quadratic.GaussianIntegers.GaussianInteger
- Math.NumberTheory.Recurrencies: partition :: Num a => [a]
- Math.NumberTheory.Zeta: intertwine :: [a] -> [a] -> [a]
- Math.NumberTheory.Zeta: skipEvens :: [a] -> [a]
- Math.NumberTheory.Zeta: skipOdds :: [a] -> [a]
- Math.NumberTheory.Zeta.Dirichlet: betas :: (Floating a, Ord a) => a -> [a]
- Math.NumberTheory.Zeta.Dirichlet: betasEven :: forall a. (Floating a, Ord a) => a -> [a]
- Math.NumberTheory.Zeta.Dirichlet: betasOdd :: [ExactPi]
- Math.NumberTheory.Zeta.Hurwitz: zetaHurwitz :: forall a. (Floating a, Ord a) => a -> a -> [a]
- Math.NumberTheory.Zeta.Riemann: zetas :: (Floating a, Ord a) => a -> [a]
- Math.NumberTheory.Zeta.Riemann: zetasEven :: [ExactPi]
- Math.NumberTheory.Zeta.Riemann: zetasOdd :: forall a. (Floating a, Ord a) => a -> [a]
+ Math.NumberTheory.Euclidean: WrapIntegral :: a -> WrappedIntegral a
+ Math.NumberTheory.Euclidean: [unwrapIntegral] :: WrappedIntegral a -> a
+ Math.NumberTheory.Euclidean: class Semiring a => GcdDomain a
+ Math.NumberTheory.Euclidean: degree :: Euclidean a => a -> Natural
+ Math.NumberTheory.Euclidean: divide :: GcdDomain a => a -> a -> Maybe a
+ Math.NumberTheory.Euclidean: infixl 7 `rem`
+ Math.NumberTheory.Euclidean: isUnit :: (Eq a, GcdDomain a) => a -> Bool
+ Math.NumberTheory.Euclidean.Coprimes: instance (GHC.Classes.Eq a, Data.Euclidean.GcdDomain a, GHC.Classes.Eq b, GHC.Num.Num b) => GHC.Base.Monoid (Math.NumberTheory.Euclidean.Coprimes.Coprimes a b)
+ Math.NumberTheory.Euclidean.Coprimes: instance (GHC.Classes.Eq a, Data.Euclidean.GcdDomain a, GHC.Classes.Eq b, GHC.Num.Num b) => GHC.Base.Semigroup (Math.NumberTheory.Euclidean.Coprimes.Coprimes a b)
+ Math.NumberTheory.Moduli.Class: instance GHC.Classes.Ord Math.NumberTheory.Moduli.Class.SomeMod
+ Math.NumberTheory.Moduli.Singleton: [Some] :: a m -> Some a
+ Math.NumberTheory.Moduli.Singleton: cyclicGroup :: forall a m. (Integral a, UniqueFactorisation a, KnownNat m) => Maybe (CyclicGroup a m)
+ Math.NumberTheory.Moduli.Singleton: cyclicGroupFromFactors :: (Eq a, Num a) => [(Prime a, Word)] -> Maybe (Some (CyclicGroup a))
+ Math.NumberTheory.Moduli.Singleton: cyclicGroupFromModulo :: (Integral a, UniqueFactorisation a) => a -> Maybe (Some (CyclicGroup a))
+ Math.NumberTheory.Moduli.Singleton: cyclicGroupToSFactors :: Num a => CyclicGroup a m -> SFactors a m
+ Math.NumberTheory.Moduli.Singleton: data CyclicGroup a (m :: Nat)
+ Math.NumberTheory.Moduli.Singleton: data SFactors a (m :: Nat)
+ Math.NumberTheory.Moduli.Singleton: data Some (a :: Nat -> *)
+ Math.NumberTheory.Moduli.Singleton: instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Math.NumberTheory.Moduli.Singleton.CyclicGroup a m)
+ Math.NumberTheory.Moduli.Singleton: instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Math.NumberTheory.Moduli.Singleton.SFactors a m)
+ Math.NumberTheory.Moduli.Singleton: instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Math.NumberTheory.Moduli.Singleton.Some (Math.NumberTheory.Moduli.Singleton.CyclicGroup a))
+ Math.NumberTheory.Moduli.Singleton: instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Math.NumberTheory.Moduli.Singleton.Some (Math.NumberTheory.Moduli.Singleton.SFactors a))
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Classes.Eq (Math.NumberTheory.Moduli.Singleton.CyclicGroup a m)
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Classes.Eq (Math.NumberTheory.Moduli.Singleton.SFactors a m)
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Classes.Eq a => GHC.Classes.Eq (Math.NumberTheory.Moduli.Singleton.Some (Math.NumberTheory.Moduli.Singleton.CyclicGroup a))
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Classes.Ord (Math.NumberTheory.Moduli.Singleton.CyclicGroup a m)
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Classes.Ord (Math.NumberTheory.Moduli.Singleton.SFactors a m)
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Classes.Ord a => GHC.Classes.Eq (Math.NumberTheory.Moduli.Singleton.Some (Math.NumberTheory.Moduli.Singleton.SFactors a))
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Classes.Ord a => GHC.Classes.Ord (Math.NumberTheory.Moduli.Singleton.Some (Math.NumberTheory.Moduli.Singleton.CyclicGroup a))
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Classes.Ord a => GHC.Classes.Ord (Math.NumberTheory.Moduli.Singleton.Some (Math.NumberTheory.Moduli.Singleton.SFactors a))
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Generics.Generic (Math.NumberTheory.Moduli.Singleton.CyclicGroup a m)
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Generics.Generic (Math.NumberTheory.Moduli.Singleton.SFactors a m)
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Show.Show a => GHC.Show.Show (Math.NumberTheory.Moduli.Singleton.CyclicGroup a m)
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Show.Show a => GHC.Show.Show (Math.NumberTheory.Moduli.Singleton.SFactors a m)
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Show.Show a => GHC.Show.Show (Math.NumberTheory.Moduli.Singleton.Some (Math.NumberTheory.Moduli.Singleton.CyclicGroup a))
+ Math.NumberTheory.Moduli.Singleton: instance GHC.Show.Show a => GHC.Show.Show (Math.NumberTheory.Moduli.Singleton.Some (Math.NumberTheory.Moduli.Singleton.SFactors a))
+ Math.NumberTheory.Moduli.Singleton: pattern CG2 :: CyclicGroup a m
+ Math.NumberTheory.Moduli.Singleton: pattern CG4 :: CyclicGroup a m
+ Math.NumberTheory.Moduli.Singleton: pattern CGDoubleOddPrimePower :: Prime a -> Word -> CyclicGroup a m
+ Math.NumberTheory.Moduli.Singleton: pattern CGOddPrimePower :: Prime a -> Word -> CyclicGroup a m
+ Math.NumberTheory.Moduli.Singleton: proofFromCyclicGroup :: Integral a => CyclicGroup a m -> () :- KnownNat m
+ Math.NumberTheory.Moduli.Singleton: proofFromSFactors :: Integral a => SFactors a m -> () :- KnownNat m
+ Math.NumberTheory.Moduli.Singleton: sfactors :: forall a m. (Ord a, UniqueFactorisation a, KnownNat m) => SFactors a m
+ Math.NumberTheory.Moduli.Singleton: sfactorsToCyclicGroup :: (Eq a, Num a) => SFactors a m -> Maybe (CyclicGroup a m)
+ Math.NumberTheory.Moduli.Singleton: someSFactors :: (Ord a, Num a) => [(Prime a, Word)] -> Some (SFactors a)
+ Math.NumberTheory.Moduli.Singleton: unSFactors :: SFactors a m -> [(Prime a, Word)]
+ Math.NumberTheory.Prefactored: instance (GHC.Classes.Eq a, Data.Euclidean.GcdDomain a, Math.NumberTheory.Primes.UniqueFactorisation a) => Math.NumberTheory.Primes.UniqueFactorisation (Math.NumberTheory.Prefactored.Prefactored a)
+ Math.NumberTheory.Prefactored: instance (GHC.Classes.Eq a, GHC.Num.Num a, Data.Euclidean.GcdDomain a) => GHC.Num.Num (Math.NumberTheory.Prefactored.Prefactored a)
+ Math.NumberTheory.Primes: factorBack :: Num a => [(Prime a, Word)] -> a
+ Math.NumberTheory.Primes.Small: smallPrimes :: Vector Word16
+ Math.NumberTheory.Quadratic.EisensteinIntegers: instance Data.Euclidean.Euclidean Math.NumberTheory.Quadratic.EisensteinIntegers.EisensteinInteger
+ Math.NumberTheory.Quadratic.EisensteinIntegers: instance Data.Euclidean.GcdDomain Math.NumberTheory.Quadratic.EisensteinIntegers.EisensteinInteger
+ Math.NumberTheory.Quadratic.EisensteinIntegers: instance Data.Semiring.Ring Math.NumberTheory.Quadratic.EisensteinIntegers.EisensteinInteger
+ Math.NumberTheory.Quadratic.EisensteinIntegers: instance Data.Semiring.Semiring Math.NumberTheory.Quadratic.EisensteinIntegers.EisensteinInteger
+ Math.NumberTheory.Quadratic.GaussianIntegers: instance Data.Euclidean.Euclidean Math.NumberTheory.Quadratic.GaussianIntegers.GaussianInteger
+ Math.NumberTheory.Quadratic.GaussianIntegers: instance Data.Euclidean.GcdDomain Math.NumberTheory.Quadratic.GaussianIntegers.GaussianInteger
+ Math.NumberTheory.Quadratic.GaussianIntegers: instance Data.Semiring.Ring Math.NumberTheory.Quadratic.GaussianIntegers.GaussianInteger
+ Math.NumberTheory.Quadratic.GaussianIntegers: instance Data.Semiring.Semiring Math.NumberTheory.Quadratic.GaussianIntegers.GaussianInteger
+ Math.NumberTheory.Zeta: betas :: (Floating a, Ord a) => a -> [a]
+ Math.NumberTheory.Zeta: betasOdd :: [ExactPi]
+ Math.NumberTheory.Zeta: zetaHurwitz :: forall a. (Floating a, Ord a) => a -> a -> [a]
+ Math.NumberTheory.Zeta: zetas :: (Floating a, Ord a) => a -> [a]
+ Math.NumberTheory.Zeta: zetasEven :: [ExactPi]
- Math.NumberTheory.ArithmeticFunctions: nFrees :: forall a. Integral a => Word -> [a]
+ Math.NumberTheory.ArithmeticFunctions: nFrees :: forall a. (Integral a, Bits a, UniqueFactorisation a, Enum (Prime a)) => Word -> [a]
- Math.NumberTheory.ArithmeticFunctions: nFreesBlock :: forall a. Integral a => Word -> a -> Word -> [a]
+ Math.NumberTheory.ArithmeticFunctions: nFreesBlock :: forall a. (Integral a, Bits a, UniqueFactorisation a, Enum (Prime a)) => Word -> a -> Word -> [a]
- Math.NumberTheory.ArithmeticFunctions.Inverse: asSetOfPreimages :: (Euclidean a, Integral a) => (forall b. Semiring b => (a -> b) -> a -> b) -> a -> Set a
+ Math.NumberTheory.ArithmeticFunctions.Inverse: asSetOfPreimages :: (Ord a, Semiring a) => (forall b. Semiring b => (a -> b) -> a -> b) -> a -> Set a
- Math.NumberTheory.ArithmeticFunctions.Inverse: inverseSigma :: (Semiring b, Euclidean a, UniqueFactorisation a, Integral a) => (a -> b) -> a -> b
+ Math.NumberTheory.ArithmeticFunctions.Inverse: inverseSigma :: (Semiring b, Euclidean a, UniqueFactorisation a, Integral a, Enum (Prime a), Bits a) => (a -> b) -> a -> b
- Math.NumberTheory.ArithmeticFunctions.NFreedom: nFrees :: forall a. Integral a => Word -> [a]
+ Math.NumberTheory.ArithmeticFunctions.NFreedom: nFrees :: forall a. (Integral a, Bits a, UniqueFactorisation a, Enum (Prime a)) => Word -> [a]
- Math.NumberTheory.ArithmeticFunctions.NFreedom: nFreesBlock :: forall a. Integral a => Word -> a -> Word -> [a]
+ Math.NumberTheory.ArithmeticFunctions.NFreedom: nFreesBlock :: forall a. (Integral a, Bits a, UniqueFactorisation a, Enum (Prime a)) => Word -> a -> Word -> [a]
- Math.NumberTheory.ArithmeticFunctions.NFreedom: sieveBlockNFree :: forall a. Integral a => Word -> a -> Word -> Vector Bool
+ Math.NumberTheory.ArithmeticFunctions.NFreedom: sieveBlockNFree :: forall a. (Integral a, Enum (Prime a), Bits a, UniqueFactorisation a) => Word -> a -> Word -> Vector Bool
- Math.NumberTheory.ArithmeticFunctions.SieveBlock: sieveBlock :: SieveBlockConfig a -> Word -> Word -> Vector a
+ Math.NumberTheory.ArithmeticFunctions.SieveBlock: sieveBlock :: forall v a. Vector v a => SieveBlockConfig a -> Word -> Word -> v a
- Math.NumberTheory.Euclidean: class (Eq a, Num a) => Euclidean a
+ Math.NumberTheory.Euclidean: class GcdDomain a => Euclidean a
- Math.NumberTheory.Euclidean: coprime :: Euclidean a => a -> a -> Bool
+ Math.NumberTheory.Euclidean: coprime :: GcdDomain a => a -> a -> Bool
- Math.NumberTheory.Euclidean: extendedGCD :: Euclidean a => a -> a -> (a, a, a)
+ Math.NumberTheory.Euclidean: extendedGCD :: (Eq a, Num a, Euclidean a) => a -> a -> (a, a, a)
- Math.NumberTheory.Euclidean: gcd :: Euclidean a => a -> a -> a
+ Math.NumberTheory.Euclidean: gcd :: GcdDomain a => a -> a -> a
- Math.NumberTheory.Euclidean: lcm :: Euclidean a => a -> a -> a
+ Math.NumberTheory.Euclidean: lcm :: GcdDomain a => a -> a -> a
- Math.NumberTheory.Euclidean.Coprimes: insert :: (Euclidean a, Eq b, Num b) => a -> b -> Coprimes a b -> Coprimes a b
+ Math.NumberTheory.Euclidean.Coprimes: insert :: (Eq a, GcdDomain a, Eq b, Num b) => a -> b -> Coprimes a b -> Coprimes a b
- Math.NumberTheory.Euclidean.Coprimes: singleton :: (Eq a, Num a, Eq b, Num b) => a -> b -> Coprimes a b
+ Math.NumberTheory.Euclidean.Coprimes: singleton :: (Eq a, GcdDomain a, Eq b, Num b) => a -> b -> Coprimes a b
- Math.NumberTheory.Euclidean.Coprimes: splitIntoCoprimes :: (Euclidean a, Eq b, Num b) => [(a, b)] -> Coprimes a b
+ Math.NumberTheory.Euclidean.Coprimes: splitIntoCoprimes :: (Eq a, GcdDomain a, Eq b, Num b) => [(a, b)] -> Coprimes a b
- Math.NumberTheory.Moduli.Chinese: chinese :: forall a. Euclidean a => (a, a) -> (a, a) -> Maybe a
+ Math.NumberTheory.Moduli.Chinese: chinese :: forall a. (Integral a, GcdDomain a, Euclidean a) => (a, a) -> (a, a) -> Maybe a
- Math.NumberTheory.Moduli.Chinese: chineseCoprime :: Euclidean a => (a, a) -> (a, a) -> Maybe a
+ Math.NumberTheory.Moduli.Chinese: chineseCoprime :: (Integral a, Euclidean a) => (a, a) -> (a, a) -> Maybe a
- Math.NumberTheory.Moduli.DiscreteLogarithm: discreteLogarithm :: KnownNat m => PrimitiveRoot m -> MultMod m -> Natural
+ Math.NumberTheory.Moduli.DiscreteLogarithm: discreteLogarithm :: CyclicGroup Integer m -> PrimitiveRoot m -> MultMod m -> Natural
- Math.NumberTheory.Moduli.Equations: solveQuadratic :: KnownNat m => Mod m -> Mod m -> Mod m -> [Mod m]
+ Math.NumberTheory.Moduli.Equations: solveQuadratic :: SFactors Integer m -> Mod m -> Mod m -> Mod m -> [Mod m]
- Math.NumberTheory.Moduli.PrimitiveRoot: isPrimitiveRoot :: KnownNat n => Mod n -> Maybe (PrimitiveRoot n)
+ Math.NumberTheory.Moduli.PrimitiveRoot: isPrimitiveRoot :: (Integral a, UniqueFactorisation a) => CyclicGroup a m -> Mod m -> Maybe (PrimitiveRoot m)
- Math.NumberTheory.Moduli.Sqrt: sqrtsMod :: KnownNat m => Mod m -> [Mod m]
+ Math.NumberTheory.Moduli.Sqrt: sqrtsMod :: SFactors Integer m -> Mod m -> [Mod m]
- Math.NumberTheory.MoebiusInversion: generalInversion :: (Int -> Integer) -> Int -> Integer
+ Math.NumberTheory.MoebiusInversion: generalInversion :: (Num t, Vector v t) => Proxy v -> (Word -> t) -> Word -> t
- Math.NumberTheory.MoebiusInversion: totientSum :: Int -> Integer
+ Math.NumberTheory.MoebiusInversion: totientSum :: (Integral t, Vector v t) => Proxy v -> Word -> t
- Math.NumberTheory.Prefactored: fromFactors :: Num a => Coprimes a Word -> Prefactored a
+ Math.NumberTheory.Prefactored: fromFactors :: Semiring a => Coprimes a Word -> Prefactored a
- Math.NumberTheory.Prefactored: fromValue :: (Eq a, Num a) => a -> Prefactored a
+ Math.NumberTheory.Prefactored: fromValue :: (Eq a, GcdDomain a) => a -> Prefactored a
- Math.NumberTheory.Primes.Factorisation: smallFactors :: Integer -> Integer -> ([(Integer, Word)], Maybe Integer)
+ Math.NumberTheory.Primes.Factorisation: smallFactors :: Integer -> ([(Integer, Word)], Maybe Integer)
- Math.NumberTheory.Primes.Factorisation.Certified: provenFactorisation :: Integer -> Integer -> [((Integer, Word), PrimalityProof)]
+ Math.NumberTheory.Primes.Factorisation.Certified: provenFactorisation :: Integer -> [((Integer, Word), PrimalityProof)]
- Math.NumberTheory.SmoothNumbers: fromList :: Euclidean a => [a] -> Maybe (SmoothBasis a)
+ Math.NumberTheory.SmoothNumbers: fromList :: (Eq a, GcdDomain a) => [a] -> Maybe (SmoothBasis a)
- Math.NumberTheory.SmoothNumbers: fromSet :: Euclidean a => Set a -> Maybe (SmoothBasis a)
+ Math.NumberTheory.SmoothNumbers: fromSet :: (Eq a, GcdDomain a) => Set a -> Maybe (SmoothBasis a)
- Math.NumberTheory.SmoothNumbers: fromSmoothUpperBound :: Integral a => a -> Maybe (SmoothBasis a)
+ Math.NumberTheory.SmoothNumbers: fromSmoothUpperBound :: (Integral a, Enum (Prime a), Bits a, UniqueFactorisation a) => a -> Maybe (SmoothBasis a)
- Math.NumberTheory.SmoothNumbers: isSmooth :: forall a. Euclidean a => SmoothBasis a -> a -> Bool
+ Math.NumberTheory.SmoothNumbers: isSmooth :: (Eq a, GcdDomain a) => SmoothBasis a -> a -> Bool
- Math.NumberTheory.SmoothNumbers: smoothOver :: Integral a => SmoothBasis a -> [a]
+ Math.NumberTheory.SmoothNumbers: smoothOver :: (Ord a, Num a) => SmoothBasis a -> [a]
- Math.NumberTheory.SmoothNumbers: smoothOverInRange :: forall a. Integral a => SmoothBasis a -> a -> a -> [a]
+ Math.NumberTheory.SmoothNumbers: smoothOverInRange :: (Ord a, Num a) => SmoothBasis a -> a -> a -> [a]
- Math.NumberTheory.SmoothNumbers: smoothOverInRangeBF :: forall a. (Enum a, Euclidean a) => SmoothBasis a -> a -> a -> [a]
+ Math.NumberTheory.SmoothNumbers: smoothOverInRangeBF :: (Eq a, Enum a, GcdDomain a) => SmoothBasis a -> a -> a -> [a]
Files
- Changes +59/−0
- Math/NumberTheory/ArithmeticFunctions/Inverse.hs +13/−9
- Math/NumberTheory/ArithmeticFunctions/Moebius.hs +2/−3
- Math/NumberTheory/ArithmeticFunctions/NFreedom.hs +7/−6
- Math/NumberTheory/ArithmeticFunctions/SieveBlock.hs +109/−38
- Math/NumberTheory/ArithmeticFunctions/SieveBlock/Unboxed.hs +0/−131
- Math/NumberTheory/Curves/Montgomery.hs +4/−2
- Math/NumberTheory/Euclidean.hs +41/−142
- Math/NumberTheory/Euclidean/Coprimes.hs +42/−27
- Math/NumberTheory/Moduli/Chinese.hs +4/−4
- Math/NumberTheory/Moduli/Class.hs +9/−1
- Math/NumberTheory/Moduli/DiscreteLogarithm.hs +29/−24
- Math/NumberTheory/Moduli/Equations.hs +13/−11
- Math/NumberTheory/Moduli/PrimitiveRoot.hs +26/−120
- Math/NumberTheory/Moduli/Singleton.hs +314/−0
- Math/NumberTheory/Moduli/Sqrt.hs +23/−29
- Math/NumberTheory/Moduli/SqrtOld.hs +0/−232
- Math/NumberTheory/MoebiusInversion.hs +52/−32
- Math/NumberTheory/MoebiusInversion/Int.hs +3/−1
- Math/NumberTheory/Powers/General.hs +9/−9
- Math/NumberTheory/Prefactored.hs +9/−6
- Math/NumberTheory/Primes.hs +9/−4
- Math/NumberTheory/Primes/Counting/Impl.hs +3/−2
- Math/NumberTheory/Primes/Factorisation.hs +2/−1
- Math/NumberTheory/Primes/Factorisation/Certified.hs +11/−13
- Math/NumberTheory/Primes/Factorisation/Montgomery.hs +24/−21
- Math/NumberTheory/Primes/Factorisation/TrialDivision.hs +1/−1
- Math/NumberTheory/Primes/Sieve.hs +3/−1
- Math/NumberTheory/Primes/Small.hs +20/−0
- Math/NumberTheory/Primes/Testing/Certificates.hs +2/−1
- Math/NumberTheory/Primes/Testing/Certificates/Internal.hs +5/−5
- Math/NumberTheory/Quadratic/EisensteinIntegers.hs +28/−18
- Math/NumberTheory/Quadratic/GaussianIntegers.hs +27/−18
- Math/NumberTheory/Recurrencies.hs +0/−17
- Math/NumberTheory/Recurrencies/Bilinear.hs +0/−38
- Math/NumberTheory/Recurrencies/Linear.hs +0/−14
- Math/NumberTheory/SmoothNumbers.hs +42/−33
- Math/NumberTheory/UniqueFactorisation.hs +0/−13
- Math/NumberTheory/Utils.hs +8/−6
- Math/NumberTheory/Utils/DirichletSeries.hs +3/−2
- Math/NumberTheory/Zeta.hs +10/−7
- Math/NumberTheory/Zeta/Dirichlet.hs +2/−5
- Math/NumberTheory/Zeta/Hurwitz.hs +5/−5
- Math/NumberTheory/Zeta/Riemann.hs +2/−5
- arithmoi.cabal +14/−16
- benchmark/Math/NumberTheory/DiscreteLogarithmBench.hs +14/−10
- benchmark/Math/NumberTheory/InverseBench.hs +3/−1
- benchmark/Math/NumberTheory/PrimesBench.hs +1/−1
- benchmark/Math/NumberTheory/PrimitiveRootsBench.hs +16/−5
- benchmark/Math/NumberTheory/SequenceBench.hs +3/−2
- benchmark/Math/NumberTheory/SmoothNumbersBench.hs +6/−7
- test-suite/Math/NumberTheory/ArithmeticFunctions/InverseTests.hs +3/−2
- test-suite/Math/NumberTheory/ArithmeticFunctions/SieveBlockTests.hs +0/−20
- test-suite/Math/NumberTheory/ArithmeticFunctionsTests.hs +8/−8
- test-suite/Math/NumberTheory/EisensteinIntegersTests.hs +15/−33
- test-suite/Math/NumberTheory/EuclideanTests.hs +76/−24
- test-suite/Math/NumberTheory/GaussianIntegersTests.hs +17/−5
- test-suite/Math/NumberTheory/Moduli/ChineseTests.hs +8/−5
- test-suite/Math/NumberTheory/Moduli/ClassTests.hs +1/−1
- test-suite/Math/NumberTheory/Moduli/DiscreteLogarithmTests.hs +12/−8
- test-suite/Math/NumberTheory/Moduli/EquationsTests.hs +2/−1
- test-suite/Math/NumberTheory/Moduli/PrimitiveRootTests.hs +44/−46
- test-suite/Math/NumberTheory/Moduli/SingletonTests.hs +46/−0
- test-suite/Math/NumberTheory/Moduli/SqrtTests.hs +7/−6
- test-suite/Math/NumberTheory/MoebiusInversion/IntTests.hs +0/−52
- test-suite/Math/NumberTheory/MoebiusInversionTests.hs +12/−10
- test-suite/Math/NumberTheory/PrefactoredTests.hs +2/−2
- test-suite/Math/NumberTheory/Primes/CountingTests.hs +1/−1
- test-suite/Math/NumberTheory/Primes/FactorisationTests.hs +9/−8
- test-suite/Math/NumberTheory/Recurrences/BilinearTests.hs +1/−1
- test-suite/Math/NumberTheory/SmoothNumbersTests.hs +30/−27
- test-suite/Math/NumberTheory/TestUtils.hs +6/−38
- test-suite/Math/NumberTheory/TestUtils/Wrappers.hs +8/−3
- test-suite/Test.hs +3/−5
Changes view
@@ -1,3 +1,62 @@+0.10.0.0+ This release supports GHC 8.0, 8.2, 8.4, 8.6 and 8.8.++ Breaking changes:++ Move 'Euclidean' type class to 'semirings' package (#168).+ Embrace the new 'Semiring' -> 'GcdDomain' -> 'Euclidean' hierarchy+ of classes, refining 'Num' and 'Integral' constraints.++ Deprecate 'Math.NumberTheory.Primes.Factorisation', use+ 'Math.NumberTheory.Primes.factorise' instead. Deprecate+ 'Math.NumberTheory.Primes.Sieve', use 'Enum' instance instead.+ Deprecate 'Math.NumberTheory.Primes.Factorisation.Certified' and+ 'Math.NumberTheory.Primes.Testing.Certificates'.++ Remove deprecated earlier 'Math.NumberTheory.Recurrencies.*'+ and 'Math.NumberTheory.UniqueFactorisation' modules.+ Use 'Math.NumberTheory.Recurrences.*' and 'Math.NumberTheory.Primes'+ instead.++ Remove deprecated earlier an old interface of 'Math.NumberTheory.Moduli.Sqrt'.++ Reshuffle exports from 'Math.NumberTheory.Zeta', do not advertise+ its submodules as available to import.++ Add a proxy argument storing vector's flavor to+ 'Math.NumberTheory.MoebiusInversion.{generalInversion,totientSum}'.+ Deprecate 'Math.NumberTheory.MoebiusInversion.Int'.++ Deprecate 'Math.NumberTheory.SmoothNumbers.{fromSet,fromSmoothUpperBound}'.+ Use 'Math.NumberTheory.SmoothNumbers.fromList' instead.+ Deprecate 'Math.NumberTheory.SmoothNumbers.smoothOverInRange' in favor+ of 'smoothOver' and 'Math.NumberTheory.SmoothNumbers.smoothOverInRange'+ in favor of 'isSmooth'.++ 'solveQuadratic' and 'sqrtsMod' require an additional argument: a singleton+ linking a type-level modulo with a term-level factorisation (#169).++ New features:++ The machinery of cyclic groups, primitive roots and discrete logarithms+ has been completely overhauled and rewritten using singleton types (#169).++ There is also a new singleton type, linking a type-level modulo with+ a term-level factorisation. It allows both to have a nicely-typed API+ of `Mod m` and avoid repeating factorisations (#169).++ Refer to a brand new module 'Math.NumberTheory.Moduli.Singleton' for details.++ Add a new function 'factorBack'.++ Improvements:++ Add 'Ord SomeMod' instance (#165).++ Generalize 'sieveBlock' to handle any flavor of 'Vector' (#164).++ Add Semiring and Ring instances for Eisenstein and Gaussian integers.+ 0.9.0.0 This release supports GHC 8.0, 8.2, 8.4 and 8.6.
Math/NumberTheory/ArithmeticFunctions/Inverse.hs view
@@ -9,6 +9,8 @@ -- <https://www.emis.de/journals/JIS/VOL19/Alekseyev/alek5.pdf Computing the Inverses, their Power Sums, and Extrema for Euler’s Totient and Other Multiplicative Functions> -- by M. A. Alekseyev. +{-# LANGUAGE CPP #-}+{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -25,13 +27,16 @@ ) where import Prelude hiding (rem, quot)+import Data.Bits (Bits) import Data.List import Data.Map (Map) import qualified Data.Map as M import Data.Maybe import Data.Ord (Down(..))+#if __GLASGOW_HASKELL__ < 803 import Data.Semigroup-import Data.Semiring (Semiring(..))+#endif+import Data.Semiring (Semiring(..), Mul(..)) import Data.Set (Set) import qualified Data.Set as S import Numeric.Natural@@ -41,7 +46,6 @@ import Math.NumberTheory.Logarithms import Math.NumberTheory.Powers import Math.NumberTheory.Primes-import Math.NumberTheory.Primes.Sieve (primes) import Math.NumberTheory.Utils.DirichletSeries (DirichletSeries) import qualified Math.NumberTheory.Utils.DirichletSeries as DS import Math.NumberTheory.Utils.FromIntegral@@ -87,7 +91,7 @@ -- | See section 5.2 of the paper. invSigma- :: forall a. (Euclidean a, Integral a, UniqueFactorisation a)+ :: forall a. (Euclidean a, Integral a, UniqueFactorisation a, Enum (Prime a), Bits a) => [(Prime a, Word)] -- ^ Factorisation of a value of the sum-of-divisors function -> [PrimePowers a]@@ -129,7 +133,7 @@ pksSmall :: Map (Prime a) (Set Word) pksSmall = M.fromDistinctAscList [ (p, pows)- | p <- takeWhile ((< lim) . unPrime) primes+ | p <- [nextPrime 2 .. precPrime lim] , let pows = doPrime p , not (null pows) ]@@ -156,7 +160,7 @@ -- This allows us to crop resulting Dirichlet series (see 'filter' calls -- in 'invertFunction' below) at the end of each batch, saving time and memory. strategy- :: forall a c. (Euclidean c, Ord c)+ :: forall a c. (GcdDomain c, Ord c) => ArithmeticFunction a c -- ^ Arithmetic function, which we aim to inverse -> [(Prime c, Word)]@@ -177,7 +181,7 @@ -> ([PrimePowers a], (Maybe (Prime c, Word), [PrimePowers a])) go ts (p, k) = (rs, (Just (p, k), qs)) where- predicate (PrimePowers q ls) = any (\l -> g (f q l) `rem` unPrime p == 0) ls+ predicate (PrimePowers q ls) = any (\l -> isJust $ g (f q l) `divide` unPrime p) ls (qs, rs) = partition predicate ts -- | Main workhorse.@@ -298,7 +302,7 @@ -- >>> unMaxWord (inverseSigma MaxWord 120) -- 95 inverseSigma- :: (Semiring b, Euclidean a, UniqueFactorisation a, Integral a)+ :: (Semiring b, Euclidean a, UniqueFactorisation a, Integral a, Enum (Prime a), Bits a) => (a -> b) -> a -> b@@ -363,8 +367,8 @@ -- | Helper to extract a set of preimages for 'inverseTotient' or 'inverseSigma'. asSetOfPreimages- :: (Euclidean a, Integral a)+ :: (Ord a, Semiring a) => (forall b. Semiring b => (a -> b) -> a -> b) -> a -> S.Set a-asSetOfPreimages f = S.mapMonotonic getProduct . f (S.singleton . Product)+asSetOfPreimages f = S.mapMonotonic getMul . f (S.singleton . Mul)
Math/NumberTheory/ArithmeticFunctions/Moebius.hs view
@@ -37,8 +37,7 @@ import Unsafe.Coerce import Math.NumberTheory.Powers.Squares (integerSquareRoot)-import Math.NumberTheory.Primes (unPrime)-import Math.NumberTheory.Primes.Sieve (primes)+import Math.NumberTheory.Primes import Math.NumberTheory.Utils.FromIntegral (wordToInt) import Math.NumberTheory.Logarithms@@ -161,7 +160,7 @@ -- Bit fiddling in 'mapper' is correct only -- if all sufficiently small (<= 191) primes has been sieved out. ps :: [Int]- ps = takeWhile (<= (191 `max` integerSquareRoot highIndex)) $ map unPrime primes+ ps = map unPrime [nextPrime 2 .. precPrime (191 `max` integerSquareRoot highIndex)] mapper :: Int -> Word8 -> Word8 mapper ix val
Math/NumberTheory/ArithmeticFunctions/NFreedom.hs view
@@ -7,6 +7,7 @@ -- N-free number generation. -- +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE ScopedTypeVariables #-} module Math.NumberTheory.ArithmeticFunctions.NFreedom@@ -17,13 +18,13 @@ import Control.Monad (forM_) import Control.Monad.ST (runST)+import Data.Bits (Bits) import Data.List (scanl') import qualified Data.Vector.Unboxed as U import qualified Data.Vector.Unboxed.Mutable as MU import Math.NumberTheory.Powers.Squares (integerSquareRoot)-import Math.NumberTheory.Primes (unPrime)-import Math.NumberTheory.Primes.Sieve (primes)+import Math.NumberTheory.Primes import Math.NumberTheory.Utils.FromIntegral (wordToInt) -- | Evaluate the `Math.NumberTheory.ArithmeticFunctions.isNFree` function over a block.@@ -42,7 +43,7 @@ -- >>> sieveBlockNFree 2 1 10 -- [True,True,True,False,True,True,True,False,False,True] sieveBlockNFree- :: forall a . Integral a+ :: forall a. (Integral a, Enum (Prime a), Bits a, UniqueFactorisation a) => Word -- ^ Power whose @n@-freedom will be checked. -> a@@ -82,14 +83,14 @@ highIndex = lowIndex + len - 1 ps :: [a]- ps = takeWhile (<= integerSquareRoot highIndex) $ map unPrime primes+ ps = if highIndex < 4 then [] else map unPrime [nextPrime 2 .. precPrime (integerSquareRoot highIndex)] -- | For a given nonnegative integer power @n@, generate all @n@-free -- numbers in ascending order, starting at @1@. -- -- When @n@ is @0@ or @1@, the resulting list is @[1]@. nFrees- :: forall a. Integral a+ :: forall a. (Integral a, Bits a, UniqueFactorisation a, Enum (Prime a)) => Word -- ^ Power @n@ to be used to generate @n@-free numbers. -> [a]@@ -125,7 +126,7 @@ -- -- As with @nFrees@, passing @n = 0, 1@ results in an empty list. nFreesBlock- :: forall a . Integral a+ :: forall a . (Integral a, Bits a, UniqueFactorisation a, Enum (Prime a)) => Word -- ^ Power @n@ to be used to generate @n@-free numbers. -> a
Math/NumberTheory/ArithmeticFunctions/SieveBlock.hs view
@@ -9,7 +9,6 @@ -- {-# LANGUAGE BangPatterns #-}-{-# LANGUAGE CPP #-} {-# LANGUAGE MagicHash #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE UnboxedTuples #-}@@ -24,23 +23,60 @@ , sieveBlockMoebius ) where -import Control.Monad (forM_)+import Control.Monad (forM_, when) import Control.Monad.ST (runST)+import Data.Bits import Data.Coerce+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Generic.Mutable as MG import qualified Data.Vector as V-import qualified Data.Vector.Mutable as MV+import qualified Data.Vector.Unboxed as U+import qualified Data.Vector.Unboxed.Mutable as MU import GHC.Exts import Math.NumberTheory.ArithmeticFunctions.Class-import Math.NumberTheory.ArithmeticFunctions.Moebius (sieveBlockMoebius)-import Math.NumberTheory.ArithmeticFunctions.SieveBlock.Unboxed-import Math.NumberTheory.Logarithms (integerLogBase')-import Math.NumberTheory.Primes.Sieve (primes)+import Math.NumberTheory.ArithmeticFunctions.Moebius (Moebius, sieveBlockMoebius)+import Math.NumberTheory.Logarithms (wordLog2, integerLogBase')+import Math.NumberTheory.Primes import Math.NumberTheory.Primes.Types import Math.NumberTheory.Powers.Squares (integerSquareRoot)-import Math.NumberTheory.Utils (splitOff#)+import Math.NumberTheory.Utils (splitOff) import Math.NumberTheory.Utils.FromIntegral (wordToInt, intToWord) +-- | A record, which specifies a function to evaluate over a block.+--+-- For example, here is a configuration for the totient function:+--+-- > SieveBlockConfig+-- > { sbcEmpty = 1+-- > , sbcFunctionOnPrimePower = \p a -> (unPrime p - 1) * unPrime p ^ (a - 1)+-- > , sbcAppend = (*)+-- > }+data SieveBlockConfig a = SieveBlockConfig+ { sbcEmpty :: a+ -- ^ value of a function on 1+ , sbcFunctionOnPrimePower :: Prime Word -> Word -> a+ -- ^ how to evaluate a function on prime powers+ , sbcAppend :: a -> a -> a+ -- ^ how to combine values of a function on coprime arguments+ }++-- | Create a config for a multiplicative function from its definition on prime powers.+multiplicativeSieveBlockConfig :: Num a => (Prime Word -> Word -> a) -> SieveBlockConfig a+multiplicativeSieveBlockConfig f = SieveBlockConfig+ { sbcEmpty = 1+ , sbcFunctionOnPrimePower = f+ , sbcAppend = (*)+ }++-- | Create a config for an additive function from its definition on prime powers.+additiveSieveBlockConfig :: Num a => (Prime Word -> Word -> a) -> SieveBlockConfig a+additiveSieveBlockConfig f = SieveBlockConfig+ { sbcEmpty = 0+ , sbcFunctionOnPrimePower = f+ , sbcAppend = (+)+ }+ -- | 'runFunctionOverBlock' @f@ @x@ @l@ evaluates an arithmetic function -- for integers between @x@ and @x+l-1@ and returns a vector of length @l@. -- It completely avoids factorisation, so it is asymptotically faster than@@ -61,7 +97,7 @@ -> Word -> Word -> V.Vector a-runFunctionOverBlock (ArithmeticFunction f g) = (V.map g .) . sieveBlock SieveBlockConfig+runFunctionOverBlock (ArithmeticFunction f g) = (G.map g .) . sieveBlock SieveBlockConfig { sbcEmpty = mempty , sbcAppend = mappend , sbcFunctionOnPrimePower = coerce f@@ -72,10 +108,6 @@ -- -- Based on Algorithm M of <https://arxiv.org/pdf/1305.1639.pdf Parity of the number of primes in a given interval and algorithms of the sublinear summation> by A. V. Lelechenko. See Lemma 2 on p. 5 on its algorithmic complexity. For the majority of use-cases its time complexity is O(x^(1+ε)). ----- 'sieveBlock' is similar to 'sieveBlockUnboxed' up to flavour of 'Data.Vector',--- but is typically 7x-10x slower and consumes 3x memory.--- Use unboxed version whenever possible.--- -- For example, following code lists smallest prime factors: -- -- >>> sieveBlock (SieveBlockConfig maxBound (\p _ -> unPrime p) min) 2 10@@ -86,12 +118,14 @@ -- >>> sieveBlock (SieveBlockConfig [] (\p k -> [(unPrime p, k)]) (++)) 2 10 -- [[(2,1)],[(3,1)],[(2,2)],[(5,1)],[(2,1),(3,1)],[(7,1)],[(2,3)],[(3,2)],[(2,1),(5,1)],[(11,1)]] sieveBlock- :: SieveBlockConfig a+ :: forall v a.+ G.Vector v a+ => SieveBlockConfig a -> Word -> Word- -> V.Vector a-sieveBlock _ _ 0 = V.empty-sieveBlock (SieveBlockConfig empty f append) lowIndex' len' = runST $ do+ -> v a+sieveBlock _ _ 0 = G.empty+sieveBlock (SieveBlockConfig empty f append) !lowIndex' len' = runST $ do let lowIndex :: Int lowIndex = wordToInt lowIndex'@@ -99,35 +133,72 @@ len :: Int len = wordToInt len' - as <- V.unsafeThaw $ V.enumFromN lowIndex' len- bs <- MV.replicate len empty-- let highIndex :: Int+ highIndex :: Int highIndex = lowIndex + len - 1 + highIndex' :: Word+ highIndex' = intToWord highIndex+ ps :: [Int]- ps = takeWhile (<= integerSquareRoot highIndex) $ map unPrime primes+ ps = if highIndex < 4 then [] else map unPrime [nextPrime 2 .. precPrime (integerSquareRoot highIndex)] - forM_ ps $ \p -> do+ as <- MU.replicate len 1+ bs <- MG.replicate len empty - let p# :: Word#- !p'@(W# p#) = intToWord p+ let doPrime 2 = do+ let fs = V.generate (wordLog2 highIndex')+ (\k -> f (Prime 2) (intToWord k + 1))+ npLow = (lowIndex' + 1) `shiftR` 1+ npHigh = highIndex' `shiftR` 1+ forM_ [npLow .. npHigh] $ \np@(W# np#) -> do+ let ix = wordToInt (np `shiftL` 1) - lowIndex :: Int+ tz = I# (word2Int# (ctz# np#))+ MU.unsafeModify as (\x -> x `shiftL` (tz + 1)) ix+ MG.unsafeModify bs (\y -> y `append` V.unsafeIndex fs tz) ix - fs = V.generate- (integerLogBase' (toInteger p) (toInteger highIndex))- (\k -> f (Prime p') (intToWord k + 1))+ doPrime p = do+ let p' = intToWord p+ f0 = f (Prime p') 1+ logp = integerLogBase' (toInteger p) (toInteger highIndex) - 1+ fs = V.generate logp (\k -> f (Prime p') (intToWord k + 2))+ npLow = (lowIndex + p - 1) `quot` p+ npHigh = highIndex `quot` p - offset :: Int- offset = negate lowIndex `mod` p+ forM_ [npLow .. npHigh] $ \np -> do+ let !(I# ix#) = np * p - lowIndex+ (q, r) = np `quotRem` p+ if r /= 0+ then do+ MU.unsafeModify as (\x -> x * p') (I# ix#)+ MG.unsafeModify bs (\y -> y `append` f0) (I# ix#)+ else do+ let (pow, _) = splitOff p q+ MU.unsafeModify as (\x -> x * p' ^ (pow + 2)) (I# ix#)+ MG.unsafeModify bs (\y -> y `append` V.unsafeIndex fs (wordToInt pow)) (I# ix#) - forM_ [offset, offset + p .. len - 1] $ \ix -> do- W# a# <- MV.unsafeRead as ix- let !(# pow#, a'# #) = splitOff# p# (a# `quotWord#` p#)- MV.unsafeWrite as ix (W# a'#)- MV.unsafeModify bs (\y -> y `append` V.unsafeIndex fs (I# (word2Int# pow#))) ix+ forM_ ps doPrime forM_ [0 .. len - 1] $ \k -> do- a <- MV.unsafeRead as k- MV.unsafeModify bs (\b -> if a /= 1 then b `append` f (Prime a) 1 else b) k+ a <- MU.unsafeRead as k+ let a' = intToWord (k + lowIndex)+ when (a /= a') $+ MG.unsafeModify bs (\b -> b `append` f (Prime $ a' `quot` a) 1) k - V.unsafeFreeze bs+ G.unsafeFreeze bs++-- | This is 'sieveBlock' specialized to unboxed vectors.+--+-- >>> sieveBlockUnboxed (SieveBlockConfig 1 (\_ a -> a + 1) (*)) 1 10+-- [1,2,2,3,2,4,2,4,3,4]+sieveBlockUnboxed+ :: U.Unbox a+ => SieveBlockConfig a+ -> Word+ -> Word+ -> U.Vector a+sieveBlockUnboxed = sieveBlock++{-# SPECIALIZE sieveBlockUnboxed :: SieveBlockConfig Int -> Word -> Word -> U.Vector Int #-}+{-# SPECIALIZE sieveBlockUnboxed :: SieveBlockConfig Word -> Word -> Word -> U.Vector Word #-}+{-# SPECIALIZE sieveBlockUnboxed :: SieveBlockConfig Bool -> Word -> Word -> U.Vector Bool #-}+{-# SPECIALIZE sieveBlockUnboxed :: SieveBlockConfig Moebius -> Word -> Word -> U.Vector Moebius #-}
− Math/NumberTheory/ArithmeticFunctions/SieveBlock/Unboxed.hs
@@ -1,131 +0,0 @@--- |--- Module: Math.NumberTheory.ArithmeticFunctions.SieveBlock.Unboxed--- Copyright: (c) 2017 Andrew Lelechenko--- Licence: MIT--- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com>------ Bulk evaluation of arithmetic functions without factorisation--- of arguments.-----{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE MagicHash #-}-{-# LANGUAGE ScopedTypeVariables #-}-{-# LANGUAGE UnboxedTuples #-}--module Math.NumberTheory.ArithmeticFunctions.SieveBlock.Unboxed- ( SieveBlockConfig(..)- , multiplicativeSieveBlockConfig- , additiveSieveBlockConfig- , sieveBlockUnboxed- ) where--import Control.Monad (forM_)-import Control.Monad.ST (runST)-import qualified Data.Vector.Unboxed as V-import qualified Data.Vector.Unboxed.Mutable as MV-import GHC.Exts--import Math.NumberTheory.ArithmeticFunctions.Moebius (Moebius)-import Math.NumberTheory.Logarithms (integerLogBase')-import Math.NumberTheory.Primes.Sieve (primes)-import Math.NumberTheory.Primes.Types (Prime(..))-import Math.NumberTheory.Powers.Squares (integerSquareRoot)-import Math.NumberTheory.Utils (splitOff#)-import Math.NumberTheory.Utils.FromIntegral (wordToInt, intToWord)---- | A record, which specifies a function to evaluate over a block.------ For example, here is a configuration for the totient function:------ > SieveBlockConfig--- > { sbcEmpty = 1--- > , sbcFunctionOnPrimePower = \p a -> (unPrime p - 1) * unPrime p ^ (a - 1)--- > , sbcAppend = (*)--- > }-data SieveBlockConfig a = SieveBlockConfig- { sbcEmpty :: a- -- ^ value of a function on 1- , sbcFunctionOnPrimePower :: Prime Word -> Word -> a- -- ^ how to evaluate a function on prime powers- , sbcAppend :: a -> a -> a- -- ^ how to combine values of a function on coprime arguments- }---- | Create a config for a multiplicative function from its definition on prime powers.-multiplicativeSieveBlockConfig :: Num a => (Prime Word -> Word -> a) -> SieveBlockConfig a-multiplicativeSieveBlockConfig f = SieveBlockConfig- { sbcEmpty = 1- , sbcFunctionOnPrimePower = f- , sbcAppend = (*)- }---- | Create a config for an additive function from its definition on prime powers.-additiveSieveBlockConfig :: Num a => (Prime Word -> Word -> a) -> SieveBlockConfig a-additiveSieveBlockConfig f = SieveBlockConfig- { sbcEmpty = 0- , sbcFunctionOnPrimePower = f- , sbcAppend = (+)- }---- | Evaluate a function over a block in accordance to provided configuration.--- Value of @f@ at 0, if zero falls into block, is undefined.------ Based on Algorithm M of <https://arxiv.org/pdf/1305.1639.pdf Parity of the number of primes in a given interval and algorithms of the sublinear summation> by A. V. Lelechenko. See Lemma 2 on p. 5 on its algorithmic complexity. For the majority of use-cases its time complexity is O(x^(1+ε)).------ For example, here is an analogue of divisor function 'Math.NumberTheory.ArithmeticFunctions.tau':------ >>> sieveBlockUnboxed (SieveBlockConfig 1 (\_ a -> a + 1) (*)) 1 10--- [1,2,2,3,2,4,2,4,3,4]-sieveBlockUnboxed- :: V.Unbox a- => SieveBlockConfig a- -> Word- -> Word- -> V.Vector a-sieveBlockUnboxed _ _ 0 = V.empty-sieveBlockUnboxed (SieveBlockConfig empty f append) lowIndex' len' = runST $ do-- let lowIndex :: Int- lowIndex = wordToInt lowIndex'-- len :: Int- len = wordToInt len'-- as <- V.unsafeThaw $ V.enumFromN lowIndex' len- bs <- MV.replicate len empty-- let highIndex :: Int- highIndex = lowIndex + len - 1-- ps :: [Int]- ps = takeWhile (<= integerSquareRoot highIndex) $ map unPrime primes-- forM_ ps $ \p -> do-- let p# :: Word#- !p'@(W# p#) = intToWord p-- fs = V.generate- (integerLogBase' (toInteger p) (toInteger highIndex))- (\k -> f (Prime p') (intToWord k + 1))-- offset :: Int- offset = negate lowIndex `mod` p-- forM_ [offset, offset + p .. len - 1] $ \ix -> do- W# a# <- MV.unsafeRead as ix- let !(# pow#, a'# #) = splitOff# p# (a# `quotWord#` p#)- MV.unsafeWrite as ix (W# a'#)- MV.unsafeModify bs (\y -> y `append` V.unsafeIndex fs (I# (word2Int# pow#))) ix-- forM_ [0 .. len - 1] $ \k -> do- a <- MV.unsafeRead as k- MV.unsafeModify bs (\b -> if a /= 1 then b `append` f (Prime a) 1 else b) k-- V.unsafeFreeze bs--{-# SPECIALIZE sieveBlockUnboxed :: SieveBlockConfig Int -> Word -> Word -> V.Vector Int #-}-{-# SPECIALIZE sieveBlockUnboxed :: SieveBlockConfig Word -> Word -> Word -> V.Vector Word #-}-{-# SPECIALIZE sieveBlockUnboxed :: SieveBlockConfig Bool -> Word -> Word -> V.Vector Bool #-}-{-# SPECIALIZE sieveBlockUnboxed :: SieveBlockConfig Moebius -> Word -> Word -> V.Vector Moebius #-}
Math/NumberTheory/Curves/Montgomery.hs view
@@ -4,7 +4,8 @@ -- Licence: MIT -- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com> ----- Arithmetic on Montgomery elliptic curve.+-- Arithmetic on Montgomery elliptic curves.+-- This is an internal module, exposed only for purposes of testing. -- {-# LANGUAGE BangPatterns #-}@@ -15,6 +16,7 @@ {-# LANGUAGE ScopedTypeVariables #-} {-# OPTIONS_GHC -fno-warn-type-defaults #-}+{-# OPTIONS_HADDOCK hide #-} module Math.NumberTheory.Curves.Montgomery ( Point@@ -68,7 +70,7 @@ Point _ 0 == Point _ 0 = True Point _ 0 == _ = False _ == Point _ 0 = False- p@(Point x1 z1) == Point x2 z2 = let n = pointN p in x1 * z2 `mod` n == x2 * z1 `mod` n+ p@(Point x1 z1) == Point x2 z2 = let n = pointN p in (x1 * z2 - x2 * z1) `rem` n == 0 -- | For debugging. instance (KnownNat a24, KnownNat n) => Show (Point a24 n) where
Math/NumberTheory/Euclidean.hs view
@@ -14,151 +14,50 @@ {-# LANGUAGE ScopedTypeVariables #-} module Math.NumberTheory.Euclidean- ( Euclidean(..)+ ( GcdDomain(..)+ , Euclidean(..) , WrappedIntegral(..)+ , extendedGCD+ , isUnit ) where import Prelude hiding (divMod, div, gcd, lcm, mod, quotRem, quot, rem)-import qualified Prelude as P--import GHC.Exts-import GHC.Integer.GMP.Internals-import Numeric.Natural---- | A class to represent a Euclidean domain,--- which is basically an 'Integral' without 'toInteger'.-class (Eq a, Num a) => Euclidean a where- -- | When restriced to a subring of the Euclidean domain @a@ isomorphic to- -- @Integer@, this function should match @quotRem@ for Integers.- quotRem :: a -> a -> (a, a)- -- | When restriced to a subring of the Euclidean domain @a@ isomorphic to- -- @Integer@, this function should match @divMod@ for Integers.- divMod :: a -> a -> (a, a)-- quot :: a -> a -> a- quot x y = fst (quotRem x y)-- rem :: a -> a -> a- rem x y = snd (quotRem x y)-- div :: a -> a -> a- div x y = fst (divMod x y)-- mod :: a -> a -> a- mod x y = snd (divMod x y)-- -- | @'gcd' x y@ is the greatest number that divides both @x@ and @y@.- gcd :: a -> a -> a- gcd x y = gcd' (abs x) (abs y)- where- gcd' :: a -> a -> a- gcd' a 0 = a- gcd' a b = gcd' b (abs (a `mod` b))-- -- | @'lcm' x y@ is the smallest number that both @x@ and @y@ divide.- lcm :: a -> a -> a- lcm _ 0 = 0- lcm 0 _ = 0- lcm x y = abs ((x `quot` (gcd x y)) * y)-- -- | Test whether two numbers are coprime.- coprime :: a -> a -> Bool- coprime x y = gcd x y == 1-- -- | Calculate the greatest common divisor of two numbers and coefficients- -- for the linear combination.- --- -- For signed types satisfies:- --- -- > case extendedGCD a b of- -- > (d, u, v) -> u*a + v*b == d- -- > && d == gcd a b- --- -- For unsigned and bounded types the property above holds, but since @u@ and @v@ must also be unsigned,- -- the result may look weird. E. g., on 64-bit architecture- --- -- > extendedGCD (2 :: Word) (3 :: Word) == (1, 2^64-1, 1)- --- -- For unsigned and unbounded types (like 'Numeric.Natural.Natural') the result is undefined.- --- -- For signed types we also have- --- -- > abs u < abs b || abs b <= 1- -- >- -- > abs v < abs a || abs a <= 1- --- -- (except if one of @a@ and @b@ is 'minBound' of a signed type).- extendedGCD :: a -> a -> (a, a, a)- extendedGCD a b = (d, x * signum a, y * signum b)- where- (d, x, y) = eGCD 0 1 1 0 (abs a) (abs b)- eGCD !n1 o1 !n2 o2 r s- | s == 0 = (r, o1, o2)- | otherwise = case r `quotRem` s of- (q, t) -> eGCD (o1 - q*n1) n1 (o2 - q*n2) n2 s t--coprimeIntegral :: Integral a => a -> a -> Bool-coprimeIntegral x y = (odd x || odd y) && P.gcd x y == 1---- | Wrapper around 'Integral', which has an 'Euclidean' instance.-newtype WrappedIntegral a = WrappedIntegral { unWrappedIntegral :: a }- deriving (Eq, Ord, Show, Num, Integral, Real, Enum)--instance Integral a => Euclidean (WrappedIntegral a) where- quotRem = P.quotRem- divMod = P.divMod- quot = P.quot- rem = P.rem- div = P.div- mod = P.mod- gcd = P.gcd- lcm = P.lcm- coprime = coprimeIntegral--instance Euclidean Int where- quotRem = P.quotRem- divMod = P.divMod- quot = P.quot- rem = P.rem- div = P.div- mod = P.mod- gcd (I# x) (I# y) = I# (gcdInt x y)- lcm = P.lcm- coprime = coprimeIntegral--instance Euclidean Word where- quotRem = P.quotRem- divMod = P.divMod- quot = P.quot- rem = P.rem- div = P.div- mod = P.mod- gcd (W# x) (W# y) = W# (gcdWord x y)- lcm = P.lcm- coprime = coprimeIntegral+import Data.Euclidean+import Data.Maybe+import Data.Semiring (Semiring(..), isZero) -instance Euclidean Integer where- quotRem = P.quotRem- divMod = P.divMod- quot = P.quot- rem = P.rem- div = P.div- mod = P.mod- gcd = gcdInteger- lcm = lcmInteger- coprime = coprimeIntegral- -- Blocked by GHC bug- -- https://ghc.haskell.org/trac/ghc/ticket/15350- -- extendedGCD = gcdExtInteger+-- | Check whether an element is a unit of the ring.+isUnit :: (Eq a, GcdDomain a) => a -> Bool+isUnit x = not (isZero x) && isJust (one `divide` x) --- | Beware that 'extendedGCD' does not make any sense for 'Natural'.-instance Euclidean Natural where- quotRem = P.quotRem- divMod = P.divMod- quot = P.quot- rem = P.rem- div = P.div- mod = P.mod- gcd = P.gcd- lcm = P.lcm- coprime = coprimeIntegral+-- | Calculate the greatest common divisor of two numbers and coefficients+-- for the linear combination.+--+-- For signed types satisfies:+--+-- > case extendedGCD a b of+-- > (d, u, v) -> u*a + v*b == d+-- > && d == gcd a b+--+-- For unsigned and bounded types the property above holds, but since @u@ and @v@ must also be unsigned,+-- the result may look weird. E. g., on 64-bit architecture+--+-- > extendedGCD (2 :: Word) (3 :: Word) == (1, 2^64-1, 1)+--+-- For unsigned and unbounded types (like 'Numeric.Natural.Natural') the result is undefined.+--+-- For signed types we also have+--+-- > abs u < abs b || abs b <= 1+-- >+-- > abs v < abs a || abs a <= 1+--+-- (except if one of @a@ and @b@ is 'minBound' of a signed type).+extendedGCD :: (Eq a, Num a, Euclidean a) => a -> a -> (a, a, a)+extendedGCD a b = (d, x * signum a, y * signum b)+ where+ (d, x, y) = eGCD 0 1 1 0 (abs a) (abs b)+ eGCD !n1 o1 !n2 o2 r s+ | s == 0 = (r, o1, o2)+ | otherwise = case r `quotRem` s of+ (q, t) -> eGCD (o1 - q*n1) n1 (o2 - q*n2) n2 s t
Math/NumberTheory/Euclidean/Coprimes.hs view
@@ -8,6 +8,7 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TupleSections #-} module Math.NumberTheory.Euclidean.Coprimes ( splitIntoCoprimes@@ -20,9 +21,11 @@ import Prelude hiding (gcd, quot, rem) import Data.Coerce import Data.List (tails, mapAccumL)+import Data.Maybe #if __GLASGOW_HASKELL__ < 803 import Data.Semigroup #endif+import Data.Semiring (Semiring(..), isZero) import Math.NumberTheory.Euclidean @@ -33,48 +36,59 @@ } deriving (Eq, Show) -doPair :: (Euclidean a, Eq b, Num b) => a -> b -> a -> b -> (a, a, [(a, b)])-doPair x xm y ym = case gcd x y of- 1 -> (x, y, [])- g -> (x', y', concat rests)+unsafeDivide :: GcdDomain a => a -> a -> a+unsafeDivide x y = case x `divide` y of+ Nothing -> error "violated prerequisite of unsafeDivide"+ Just z -> z++doPair :: (Eq a, GcdDomain a, Eq b, Num b) => a -> b -> a -> b -> (a, a, [(a, b)])+doPair x xm y ym+ | isUnit g = (x, y, [])+ | otherwise = (x', y', concat rests) where- (x', g', xgs) = doPair (x `quot` g) xm g (xm + ym)- xgs' = if g' == 1 then xgs else ((g', xm + ym) : xgs)+ g = gcd x y - (y', rests) = mapAccumL go (y `quot` g) xgs'- go w (t, tm) = (w', if t' == 1 then acc else (t', tm) : acc)+ (x', g', xgs) = doPair (x `unsafeDivide` g) xm g (xm + ym)+ xgs' = if isUnit g' then xgs else ((g', xm + ym) : xgs)++ (y', rests) = mapAccumL go (y `unsafeDivide` g) xgs'+ go w (t, tm) = (w', if isUnit t' then acc else (t', tm) : acc) where (w', t', acc) = doPair w ym t tm -_propDoPair :: (Euclidean a, Integral b) => a -> b -> a -> b -> Bool+_propDoPair :: (Eq a, Num a, GcdDomain a, Integral b) => a -> b -> a -> b -> Bool _propDoPair x xm y ym- = x `rem` x' == 0- && y `rem` y' == 0+ = isJust (x `divide` x')+ && isJust (y `divide` y') && coprime x' y' && all (coprime x') (map fst rest) && all (coprime y') (map fst rest)- && all (/= 1) (map fst rest)+ && all (not . isUnit) (map fst rest) && and [ coprime s t | (s, _) : ts <- tails rest, (t, _) <- ts ]- && (x ^ xm) * (y ^ ym) == (x' ^ xm) * (y' ^ ym) * product (map (\(r, k) -> r ^ k) rest)+ && abs ((x ^ xm) * (y ^ ym)) == abs ((x' ^ xm) * (y' ^ ym) * product (map (\(r, k) -> r ^ k) rest)) where (x', y', rest) = doPair x xm y ym insertInternal :: forall a b.- (Euclidean a, Eq b, Num b)+ (Eq a, GcdDomain a, Eq b, Num b) => a -> b -> Coprimes a b -> (Coprimes a b, Coprimes a b)-insertInternal 0 _ = const (Coprimes [(0, 1)], Coprimes [])-insertInternal xx xm = coerce (go ([], []) xx)+insertInternal xx xm+ | isZero xx && xm == 0 = (, Coprimes [])+ | isZero xx = const (Coprimes [(zero, 1)], Coprimes [])+ | otherwise = coerce (go ([], []) xx) where go :: ([(a, b)], [(a, b)]) -> a -> [(a, b)] -> ([(a, b)], [(a, b)])- go (old, new) 1 rest = (rest ++ old, new)+ go (old, new) x rest+ | isUnit x = (rest ++ old, new) go (old, new) x [] = (old, (x, xm) : new)- go _ _ ((0, _) : _) = ([(0, 1)], [])+ go _ _ ((x, _) : _)+ | isZero x = ([(zero, 1)], []) go (old, new) x ((y, ym) : rest)- | y' == 1 = go (old, xys ++ new) x' rest+ | isUnit y' = go (old, xys ++ new) x' rest | otherwise = go ((y', ym) : old, xys ++ new) x' rest where (x', y', xys) = doPair x xm y ym@@ -83,10 +97,11 @@ -- -- >>> singleton 210 1 -- Coprimes {unCoprimes = [(210,1)]}-singleton :: (Eq a, Num a, Eq b, Num b) => a -> b -> Coprimes a b-singleton 0 0 = Coprimes []-singleton 1 _ = Coprimes []-singleton a b = Coprimes [(a, b)]+singleton :: (Eq a, GcdDomain a, Eq b, Num b) => a -> b -> Coprimes a b+singleton a b+ | isZero a && b == 0 = Coprimes []+ | isUnit a = Coprimes []+ | otherwise = Coprimes [(a, b)] -- | Add a non-zero number with its multiplicity to 'Coprimes'. --@@ -94,17 +109,17 @@ -- Coprimes {unCoprimes = [(7,1),(5,2),(3,3),(2,4)]} -- >>> insert 2 4 (insert 7 1 (insert 5 2 (singleton 4 3))) -- Coprimes {unCoprimes = [(7,1),(5,2),(2,10)]}-insert :: (Euclidean a, Eq b, Num b) => a -> b -> Coprimes a b -> Coprimes a b+insert :: (Eq a, GcdDomain a, Eq b, Num b) => a -> b -> Coprimes a b -> Coprimes a b insert x xm ys = Coprimes $ unCoprimes zs <> unCoprimes ws where (zs, ws) = insertInternal x xm ys -instance (Euclidean a, Eq b, Num b) => Semigroup (Coprimes a b) where+instance (Eq a, GcdDomain a, Eq b, Num b) => Semigroup (Coprimes a b) where (Coprimes xs) <> ys = Coprimes $ unCoprimes zs <> foldMap unCoprimes wss where (zs, wss) = mapAccumL (\vs (x, xm) -> insertInternal x xm vs) ys xs -instance (Euclidean a, Eq b, Num b) => Monoid (Coprimes a b) where+instance (Eq a, GcdDomain a, Eq b, Num b) => Monoid (Coprimes a b) where mempty = Coprimes [] mappend = (<>) @@ -120,5 +135,5 @@ -- Coprimes {unCoprimes = [(28,1),(33,1),(5,2)]} -- >>> splitIntoCoprimes [(360, 1), (210, 1)] -- Coprimes {unCoprimes = [(7,1),(5,2),(3,3),(2,4)]}-splitIntoCoprimes :: (Euclidean a, Eq b, Num b) => [(a, b)] -> Coprimes a b+splitIntoCoprimes :: (Eq a, GcdDomain a, Eq b, Num b) => [(a, b)] -> Coprimes a b splitIntoCoprimes = foldl (\acc (x, xm) -> insert x xm acc) mempty
Math/NumberTheory/Moduli/Chinese.hs view
@@ -31,7 +31,7 @@ , chineseRemainder2 ) where -import Prelude hiding (mod, quot, gcd, lcm)+import Prelude hiding (rem, quot, gcd, lcm) import Control.Monad (foldM) import Data.Foldable@@ -54,7 +54,7 @@ -- Just 5 -- >>> chineseCoprime (3, 4) (5, 6) -- Nothing -- moduli must be coprime-chineseCoprime :: Euclidean a => (a, a) -> (a, a) -> Maybe a+chineseCoprime :: (Integral a, Euclidean a) => (a, a) -> (a, a) -> Maybe a chineseCoprime (n1, m1) (n2, m2) = case d of 1 -> Just $ ((1 - u * m1) * n1 + (1 - v * m2) * n2) `mod` (m1 * m2) _ -> Nothing@@ -76,9 +76,9 @@ -- Just 11 -- >>> chinese (3, 4) (2, 6) -- Nothing-chinese :: forall a. Euclidean a => (a, a) -> (a, a) -> Maybe a+chinese :: forall a. (Integral a, GcdDomain a, Euclidean a) => (a, a) -> (a, a) -> Maybe a chinese (n1, m1) (n2, m2)- | n1 `mod` g == n2 `mod` g+ | (n1 - n2) `rem` g == 0 = chineseCoprime (n1 `mod` m1', m1') (n2 `mod` m2', m2') | otherwise = Nothing
Math/NumberTheory/Moduli/Class.hs view
@@ -264,9 +264,17 @@ InfMod :: Rational -> SomeMod instance Eq SomeMod where- SomeMod mx == SomeMod my = getMod mx == getMod my && getVal mx == getVal my+ SomeMod mx == SomeMod my =+ getMod mx == getMod my && getVal mx == getVal my InfMod rx == InfMod ry = rx == ry _ == _ = False++instance Ord SomeMod where+ SomeMod mx `compare` SomeMod my =+ getMod mx `compare` getMod my <> getVal mx `compare` getVal my+ SomeMod{} `compare` InfMod{} = LT+ InfMod{} `compare` SomeMod{} = GT+ InfMod rx `compare` InfMod ry = rx `compare` ry instance Show SomeMod where show = \case
Math/NumberTheory/Moduli/DiscreteLogarithm.hs view
@@ -6,9 +6,14 @@ -- {-# LANGUAGE BangPatterns #-}+{-# LANGUAGE CPP #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ViewPatterns #-} +#if __GLASGOW_HASKELL__ < 801+{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}+#endif+ module Math.NumberTheory.Moduli.DiscreteLogarithm ( discreteLogarithm ) where@@ -21,36 +26,36 @@ import GHC.TypeNats.Compat import Math.NumberTheory.Moduli.Chinese (chineseRemainder2)-import Math.NumberTheory.Moduli.Class (KnownNat, MultMod(..), Mod, getVal)+import Math.NumberTheory.Moduli.Class (MultMod(..), Mod, getVal) import Math.NumberTheory.Moduli.Equations (solveLinear)-import Math.NumberTheory.Moduli.PrimitiveRoot (PrimitiveRoot(..), CyclicGroup(..))+import Math.NumberTheory.Moduli.PrimitiveRoot (PrimitiveRoot(..))+import Math.NumberTheory.Moduli.Singleton import Math.NumberTheory.Powers.Squares (integerSquareRoot) import Math.NumberTheory.Primes (unPrime) -- | Computes the discrete logarithm. Currently uses a combination of the baby-step -- giant-step method and Pollard's rho algorithm, with Bach reduction.-discreteLogarithm :: KnownNat m => PrimitiveRoot m -> MultMod m -> Natural-discreteLogarithm a b = discreteLogarithm' (getGroup a) (multElement $ unPrimitiveRoot a) (multElement b)--discreteLogarithm'- :: KnownNat m- => CyclicGroup Natural -- ^ group structure (must be the multiplicative group mod m)- -> Mod m -- ^ a- -> Mod m -- ^ b- -> Natural -- ^ result-discreteLogarithm' cg a b =- case cg of- CG2- -> 0- -- the only valid input was a=1, b=1- CG4- -> if b == 1 then 0 else 1- -- the only possible input here is a=3 with b = 1 or 3- CGOddPrimePower (toInteger . unPrime -> p) k- -> discreteLogarithmPP p k (getVal a) (getVal b)- CGDoubleOddPrimePower (toInteger . unPrime -> p) k- -> discreteLogarithmPP p k (getVal a `rem` p^k) (getVal b `rem` p^k)- -- we have the isomorphism t -> t `rem` p^k from (Z/2p^kZ)* -> (Z/p^kZ)*+--+-- >>> :set -XDataKinds+-- >>> import Data.Maybe+-- >>> let cg = fromJust cyclicGroup :: CyclicGroup Integer 13+-- >>> let rt = fromJust (isPrimitiveRoot cg 2)+-- >>> let x = fromJust (isMultElement 11)+-- >>> discreteLogarithm cg rt x+-- 7+discreteLogarithm :: CyclicGroup Integer m -> PrimitiveRoot m -> MultMod m -> Natural+discreteLogarithm cg (multElement . unPrimitiveRoot -> a) (multElement -> b) = case cg of+ CG2+ -> 0+ -- the only valid input was a=1, b=1+ CG4+ -> if getVal b == 1 then 0 else 1+ -- the only possible input here is a=3 with b = 1 or 3+ CGOddPrimePower (unPrime -> p) k+ -> discreteLogarithmPP p k (getVal a) (getVal b)+ CGDoubleOddPrimePower (unPrime -> p) k+ -> discreteLogarithmPP p k (getVal a `rem` p^k) (getVal b `rem` p^k)+ -- we have the isomorphism t -> t `rem` p^k from (Z/2p^kZ)* -> (Z/p^kZ)* -- Implementation of Bach reduction (https://www2.eecs.berkeley.edu/Pubs/TechRpts/1984/CSD-84-186.pdf) {-# INLINE discreteLogarithmPP #-}
Math/NumberTheory/Moduli/Equations.hs view
@@ -15,10 +15,12 @@ , solveQuadratic ) where +import Data.Constraint import GHC.Integer.GMP.Internals import Math.NumberTheory.Moduli.Chinese import Math.NumberTheory.Moduli.Class+import Math.NumberTheory.Moduli.Singleton import Math.NumberTheory.Moduli.Sqrt import Math.NumberTheory.Primes import Math.NumberTheory.Utils (recipMod)@@ -56,21 +58,21 @@ -- | Find all solutions of ax² + bx + c ≡ 0 (mod m). -- -- >>> :set -XDataKinds--- >>> solveQuadratic (1 :: Mod 32) 0 (-17) -- solving x² - 17 ≡ 0 (mod 32)+-- >>> solveQuadratic sfactors (1 :: Mod 32) 0 (-17) -- solving x² - 17 ≡ 0 (mod 32) -- [(9 `modulo` 32),(25 `modulo` 32),(7 `modulo` 32),(23 `modulo` 32)] solveQuadratic- :: KnownNat m- => Mod m -- ^ a+ :: SFactors Integer m+ -> Mod m -- ^ a -> Mod m -- ^ b -> Mod m -- ^ c -> [Mod m] -- ^ list of x-solveQuadratic a b c- = map fromInteger- $ fst- $ combine- $ map (\(p, n) -> (solveQuadraticPrimePower a' b' c' p n, unPrime p ^ n))- $ factorise- $ getMod a+solveQuadratic sm a b c = case proofFromSFactors sm of+ Sub Dict ->+ map fromInteger+ $ fst+ $ combine+ $ map (\(p, n) -> (solveQuadraticPrimePower a' b' c' p n, unPrime p ^ n))+ $ unSFactors sm where a' = getVal a b' = getVal b@@ -123,7 +125,7 @@ (_, False) -> [1] _ -> [] solveQuadraticPrime a b c p- | a `mod` p' == 0+ | a `rem` p' == 0 = solveLinear' p' b c | otherwise = map (\n -> n * recipModInteger (2 * a) p' `mod` p')
Math/NumberTheory/Moduli/PrimitiveRoot.hs view
@@ -14,130 +14,41 @@ {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TupleSections #-} {-# LANGUAGE UndecidableInstances #-}+{-# LANGUAGE ViewPatterns #-} +#if __GLASGOW_HASKELL__ < 801+{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}+#endif+ module Math.NumberTheory.Moduli.PrimitiveRoot- ( -- * Cyclic groups- CyclicGroup(..)- , cyclicGroupFromModulo- , cyclicGroupToModulo- , groupSize- -- * Primitive roots- , PrimitiveRoot+ ( -- * Primitive roots+ PrimitiveRoot , unPrimitiveRoot- , getGroup , isPrimitiveRoot- , isPrimitiveRoot' ) where -#if __GLASGOW_HASKELL__ < 803-import Data.Semigroup-#endif- import Math.NumberTheory.ArithmeticFunctions (totient)-import qualified Math.NumberTheory.Euclidean as E-import Math.NumberTheory.Euclidean.Coprimes as Coprimes (singleton)-import Math.NumberTheory.Moduli.Class (getNatMod, getNatVal, KnownNat, Mod, MultMod, isMultElement)-import Math.NumberTheory.Powers.General (highestPower)+import Math.NumberTheory.Moduli.Class hiding (powMod)+import Math.NumberTheory.Moduli.Singleton import Math.NumberTheory.Powers.Modular-import Math.NumberTheory.Prefactored import Math.NumberTheory.Primes -import Control.DeepSeq import Control.Monad (guard)-import GHC.Generics-import Numeric.Natural---- | A multiplicative group of residues is called cyclic,--- if there is a primitive root @g@,--- whose powers generates all elements.--- Any cyclic multiplicative group of residues--- falls into one of the following cases.-data CyclicGroup a- = CG2 -- ^ Residues modulo 2.- | CG4 -- ^ Residues modulo 4.- | CGOddPrimePower (Prime a) Word- -- ^ Residues modulo @p@^@k@ for __odd__ prime @p@.- | CGDoubleOddPrimePower (Prime a) Word- -- ^ Residues modulo 2@p@^@k@ for __odd__ prime @p@.- deriving (Eq, Show, Generic)--instance NFData a => NFData (CyclicGroup a)---- | Check whether a multiplicative group of residues,--- characterized by its modulo, is cyclic and, if yes, return its form.------ >>> cyclicGroupFromModulo 4--- Just CG4--- >>> cyclicGroupFromModulo (2 * 13 ^ 3)--- Just (CGDoubleOddPrimePower (Prime 13) 3)--- >>> cyclicGroupFromModulo (4 * 13)--- Nothing-cyclicGroupFromModulo- :: (Ord a, Integral a, UniqueFactorisation a)- => a- -> Maybe (CyclicGroup a)-cyclicGroupFromModulo = \case- 2 -> Just CG2- 4 -> Just CG4- n- | n <= 1 -> Nothing- | odd n -> uncurry CGOddPrimePower <$> isPrimePower n- | odd halfN -> uncurry CGDoubleOddPrimePower <$> isPrimePower halfN- | otherwise -> Nothing- where- halfN = n `quot` 2--isPrimePower- :: (Integral a, UniqueFactorisation a)- => a- -> Maybe (Prime a, Word)-isPrimePower n = (, k) <$> isPrime m- where- (m, k) = highestPower n---- | Extract modulo and its factorisation from--- a cyclic multiplicative group of residues.------ >>> cyclicGroupToModulo CG4--- Prefactored {prefValue = 4, prefFactors = Coprimes {unCoprimes = [(2,2)]}}------ >>> import Data.Maybe--- >>> cyclicGroupToModulo (CGDoubleOddPrimePower (fromJust (isPrime 13)) 3)--- Prefactored {prefValue = 4394, prefFactors = Coprimes {unCoprimes = [(13,3),(2,1)]}}-cyclicGroupToModulo- :: E.Euclidean a- => CyclicGroup a- -> Prefactored a-cyclicGroupToModulo = fromFactors . \case- CG2 -> Coprimes.singleton 2 1- CG4 -> Coprimes.singleton 2 2- CGOddPrimePower p k -> Coprimes.singleton (unPrime p) k- CGDoubleOddPrimePower p k -> Coprimes.singleton 2 1- <> Coprimes.singleton (unPrime p) k+import Data.Constraint -- | 'PrimitiveRoot' m is a type which is only inhabited -- by <https://en.wikipedia.org/wiki/Primitive_root_modulo_n primitive roots> of m.-data PrimitiveRoot m = PrimitiveRoot+newtype PrimitiveRoot m = PrimitiveRoot { unPrimitiveRoot :: MultMod m -- ^ Extract primitive root value.- , getGroup :: CyclicGroup Natural -- ^ Get cyclic group structure. } deriving (Eq, Show) --- | 'isPrimitiveRoot'' @cg@ @a@ checks whether @a@ is--- a <https://en.wikipedia.org/wiki/Primitive_root_modulo_n primitive root>--- of a given cyclic multiplicative group of residues @cg@.------ >>> let Just cg = cyclicGroupFromModulo 13--- >>> isPrimitiveRoot' cg 1--- False--- >>> isPrimitiveRoot' cg 2--- True+-- https://en.wikipedia.org/wiki/Primitive_root_modulo_n#Finding_primitive_roots isPrimitiveRoot' :: (Integral a, UniqueFactorisation a)- => CyclicGroup a+ => CyclicGroup a m -> a -> Bool--- https://en.wikipedia.org/wiki/Primitive_root_modulo_n#Finding_primitive_roots isPrimitiveRoot' cg r = case cg of CG2 -> r == 1@@ -157,23 +68,18 @@ -- a <https://en.wikipedia.org/wiki/Primitive_root_modulo_n primitive root>. -- -- >>> :set -XDataKinds--- >>> isPrimitiveRoot (1 :: Mod 13)+-- >>> import Data.Maybe+-- >>> isPrimitiveRoot (fromJust cyclicGroup) (1 :: Mod 13) -- Nothing--- >>> isPrimitiveRoot (2 :: Mod 13)--- Just (PrimitiveRoot {unPrimitiveRoot = MultMod {multElement = (2 `modulo` 13)}, getGroup = CGOddPrimePower (Prime 13) 1})------ This function is a convenient wrapper around 'isPrimitiveRoot''. The latter--- provides better control and performance, if you need them.+-- >>> isPrimitiveRoot (fromJust cyclicGroup) (2 :: Mod 13)+-- Just (PrimitiveRoot {unPrimitiveRoot = MultMod {multElement = (2 `modulo` 13)}}) isPrimitiveRoot- :: KnownNat n- => Mod n- -> Maybe (PrimitiveRoot n)-isPrimitiveRoot r = do- r' <- isMultElement r- cg <- cyclicGroupFromModulo (getNatMod r)- guard $ isPrimitiveRoot' cg (getNatVal r)- return $ PrimitiveRoot r' cg---- | Calculate the size of a given cyclic group.-groupSize :: (E.Euclidean a, UniqueFactorisation a) => CyclicGroup a -> Prefactored a-groupSize = totient . cyclicGroupToModulo+ :: (Integral a, UniqueFactorisation a)+ => CyclicGroup a m+ -> Mod m+ -> Maybe (PrimitiveRoot m)+isPrimitiveRoot cg r = case proofFromCyclicGroup cg of+ Sub Dict -> do+ r' <- isMultElement r+ guard $ isPrimitiveRoot' cg (fromIntegral (getNatVal r))+ return $ PrimitiveRoot r'
+ Math/NumberTheory/Moduli/Singleton.hs view
@@ -0,0 +1,314 @@+-- |+-- Module: Math.NumberTheory.Moduli.Singleton+-- Copyright: (c) 2019 Andrew Lelechenko+-- Licence: MIT+-- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com>+--+-- Singleton data types.+--++{-# LANGUAGE CPP #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE GADTs #-}+{-# LANGUAGE KindSignatures #-}+{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE PatternSynonyms #-}+{-# LANGUAGE PolyKinds #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE TupleSections #-}+{-# LANGUAGE TypeOperators #-}+{-# LANGUAGE ViewPatterns #-}++module Math.NumberTheory.Moduli.Singleton+ ( -- * SFactors singleton+ SFactors+ , sfactors+ , someSFactors+ , unSFactors+ , proofFromSFactors+ -- * CyclicGroup singleton+ , CyclicGroup+ , cyclicGroup+ , cyclicGroupFromFactors+ , cyclicGroupFromModulo+ , proofFromCyclicGroup+ , pattern CG2+ , pattern CG4+ , pattern CGOddPrimePower+ , pattern CGDoubleOddPrimePower+ -- * SFactors \<=\> CyclicGroup+ , cyclicGroupToSFactors+ , sfactorsToCyclicGroup+ -- * Some wrapper+ , Some(..)+ ) where++import Control.DeepSeq+import Data.Constraint+import Data.List+import qualified Data.Map as M+import Data.Proxy+#if __GLASGOW_HASKELL__ < 803+import Data.Semigroup+#endif+import GHC.Generics+import GHC.TypeNats.Compat+import Numeric.Natural+import Unsafe.Coerce++import Math.NumberTheory.Powers+import Math.NumberTheory.Primes+import Math.NumberTheory.Primes.Types++-- | Wrapper to hide an unknown type-level natural.+data Some (a :: Nat -> *) where+ Some :: a m -> Some a++-- | From "Data.Constraint.Nat".+newtype Magic n = Magic (KnownNat n => Dict (KnownNat n))++-- | This singleton data type establishes a correspondence+-- between a modulo @m@ on type level+-- and its factorisation on term level.+newtype SFactors a (m :: Nat) = SFactors+ { unSFactors :: [(Prime a, Word)]+ -- ^ Factors of @m@.+ } deriving (Show, Generic)++instance Eq (SFactors a m) where+ _ == _ = True++instance Ord (SFactors a m) where+ _ `compare` _ = EQ++instance NFData a => NFData (SFactors a m)++instance Ord a => Eq (Some (SFactors a)) where+ Some (SFactors xs) == Some (SFactors ys) =+ xs == ys++instance Ord a => Ord (Some (SFactors a)) where+ Some (SFactors xs) `compare` Some (SFactors ys) =+ xs `compare` ys++instance Show a => Show (Some (SFactors a)) where+ showsPrec p (Some x) = showsPrec p x++instance NFData a => NFData (Some (SFactors a)) where+ rnf (Some x) = rnf x++-- | Create a singleton from a type-level positive modulo @m@,+-- passed in a constraint.+--+-- >>> :set -XDataKinds+-- >>> sfactors :: SFactors Integer 13+-- SFactors {sfactorsFactors = [(Prime 13,1)]}+sfactors :: forall a m. (Ord a, UniqueFactorisation a, KnownNat m) => SFactors a m+sfactors = if m == 0+ then error "sfactors: modulo must be positive"+ else SFactors (sort (factorise m))+ where+ m = fromIntegral (natVal (Proxy :: Proxy m))++-- | Create a singleton from factors of @m@.+-- Factors must be distinct, as in output of 'factorise'.+--+-- >>> import Math.NumberTheory.Primes+-- >>> someSFactors (factorise 98)+-- SFactors {sfactorsFactors = [(Prime 2,1),(Prime 7,2)]}+someSFactors :: (Ord a, Num a) => [(Prime a, Word)] -> Some (SFactors a)+someSFactors+ = Some+ . SFactors+ -- Just a precaution against ill-formed lists of factors+ . M.assocs+ . M.fromListWith (+)++-- | Convert a singleton to a proof that @m@ is known. Usage example:+--+-- > toModulo :: SFactors Integer m -> Natural+-- > toModulo t = case proofFromSFactors t of Sub Dict -> natVal t+proofFromSFactors :: Integral a => SFactors a m -> (() :- KnownNat m)+proofFromSFactors (SFactors fs) = Sub $ unsafeCoerce (Magic Dict) (fromIntegral (factorBack fs) :: Natural)++-- | This singleton data type establishes a correspondence+-- between a modulo @m@ on type level+-- and a cyclic group of the same order on term level.+data CyclicGroup a (m :: Nat)+ = CG2' -- ^ Residues modulo 2.+ | CG4' -- ^ Residues modulo 4.+ | CGOddPrimePower' (Prime a) Word+ -- ^ Residues modulo @p@^@k@ for __odd__ prime @p@.+ | CGDoubleOddPrimePower' (Prime a) Word+ -- ^ Residues modulo 2@p@^@k@ for __odd__ prime @p@.+ deriving (Show, Generic)++instance Eq (CyclicGroup a m) where+ _ == _ = True++instance Ord (CyclicGroup a m) where+ _ `compare` _ = EQ++instance NFData a => NFData (CyclicGroup a m)++instance Eq a => Eq (Some (CyclicGroup a)) where+ Some CG2' == Some CG2' = True+ Some CG4' == Some CG4' = True+ Some (CGOddPrimePower' p1 k1) == Some (CGOddPrimePower' p2 k2) =+ p1 == p2 && k1 == k2+ Some (CGDoubleOddPrimePower' p1 k1) == Some (CGDoubleOddPrimePower' p2 k2) =+ p1 == p2 && k1 == k2+ _ == _ = False++instance Ord a => Ord (Some (CyclicGroup a)) where+ compare (Some x) (Some y) = case x of+ CG2' -> case y of+ CG2' -> EQ+ _ -> LT+ CG4' -> case y of+ CG2' -> GT+ CG4' -> EQ+ _ -> LT+ CGOddPrimePower' p1 k1 -> case y of+ CGDoubleOddPrimePower'{} -> LT+ CGOddPrimePower' p2 k2 ->+ p1 `compare` p2 <> k1 `compare` k2+ _ -> GT+ CGDoubleOddPrimePower' p1 k1 -> case y of+ CGDoubleOddPrimePower' p2 k2 ->+ p1 `compare` p2 <> k1 `compare` k2+ _ -> GT++instance Show a => Show (Some (CyclicGroup a)) where+ showsPrec p (Some x) = showsPrec p x++instance NFData a => NFData (Some (CyclicGroup a)) where+ rnf (Some x) = rnf x++-- | Create a singleton from a type-level positive modulo @m@,+-- passed in a constraint.+--+-- >>> :set -XDataKinds+-- >>> import Data.Maybe+-- >>> cyclicGroup :: CyclicGroup Integer 169+-- CGOddPrimePower' (Prime 13) 2+--+-- >>> sfactorsToCyclicGroup (fromModulo 4)+-- Just CG4'+-- >>> sfactorsToCyclicGroup (fromModulo (2 * 13 ^ 3))+-- Just (CGDoubleOddPrimePower' (Prime 13) 3)+-- >>> sfactorsToCyclicGroup (fromModulo (4 * 13))+-- Nothing+cyclicGroup+ :: forall a m.+ (Integral a, UniqueFactorisation a, KnownNat m)+ => Maybe (CyclicGroup a m)+cyclicGroup = fromModuloInternal m+ where+ m = fromIntegral (natVal (Proxy :: Proxy m))++cyclicGroupFromFactors+ :: (Eq a, Num a)+ => [(Prime a, Word)]+ -> Maybe (Some (CyclicGroup a))+cyclicGroupFromFactors = \case+ [(unPrime -> 2, 1)] -> Just $ Some CG2'+ [(unPrime -> 2, 2)] -> Just $ Some CG4'+ [(unPrime -> 2, _)] -> Nothing+ [(p, k)] -> Just $ Some $ CGOddPrimePower' p k+ [(unPrime -> 2, 1), (p, k)] -> Just $ Some $ CGDoubleOddPrimePower' p k+ [(p, k), (unPrime -> 2, 1)] -> Just $ Some $ CGDoubleOddPrimePower' p k+ _ -> Nothing++-- | Similar to 'cyclicGroupFromFactors' . 'factorise',+-- but much faster, because it+-- but performes only one primality test instead of full+-- factorisation.+cyclicGroupFromModulo+ :: (Integral a, UniqueFactorisation a)+ => a+ -> Maybe (Some (CyclicGroup a))+cyclicGroupFromModulo = fmap Some . fromModuloInternal++fromModuloInternal+ :: (Integral a, UniqueFactorisation a)+ => a+ -> Maybe (CyclicGroup a m)+fromModuloInternal = \case+ 2 -> Just CG2'+ 4 -> Just CG4'+ n+ | even n -> uncurry CGDoubleOddPrimePower' <$> isOddPrimePower (n `div` 2)+ | otherwise -> uncurry CGOddPrimePower' <$> isOddPrimePower n++isOddPrimePower+ :: (Integral a, UniqueFactorisation a)+ => a+ -> Maybe (Prime a, Word)+isOddPrimePower n+ | even n = Nothing+ | otherwise = (, k) <$> isPrime p+ where+ (p, k) = highestPower n++-- | Convert a cyclic group to a proof that @m@ is known. Usage example:+--+-- > toModulo :: CyclicGroup Integer m -> Natural+-- > toModulo t = case proofFromCyclicGroup t of Sub Dict -> natVal t+proofFromCyclicGroup :: Integral a => CyclicGroup a m -> (() :- KnownNat m)+proofFromCyclicGroup = proofFromSFactors . cyclicGroupToSFactors++-- | Check whether a multiplicative group of residues,+-- characterized by its modulo, is cyclic and, if yes, return its form.+--+-- >>> sfactorsToCyclicGroup (fromModulo 4)+-- Just CG4'+-- >>> sfactorsToCyclicGroup (fromModulo (2 * 13 ^ 3))+-- Just (CGDoubleOddPrimePower' (Prime 13) 3)+-- >>> sfactorsToCyclicGroup (fromModulo (4 * 13))+-- Nothing+sfactorsToCyclicGroup :: (Eq a, Num a) => SFactors a m -> Maybe (CyclicGroup a m)+sfactorsToCyclicGroup (SFactors fs) = case fs of+ [(unPrime -> 2, 1)] -> Just CG2'+ [(unPrime -> 2, 2)] -> Just CG4'+ [(unPrime -> 2, _)] -> Nothing+ [(p, k)] -> Just $ CGOddPrimePower' p k+ [(p, k), (unPrime -> 2, 1)] -> Just $ CGDoubleOddPrimePower' p k+ [(unPrime -> 2, 1), (p, k)] -> Just $ CGDoubleOddPrimePower' p k+ _ -> Nothing++-- | Invert 'sfactorsToCyclicGroup'.+--+-- >>> import Data.Maybe+-- >>> cyclicGroupToSFactors (fromJust (sfactorsToCyclicGroup (fromModulo 4)))+-- SFactors {sfactorsModulo = 4, sfactorsFactors = [(Prime 2,2)]}+cyclicGroupToSFactors :: Num a => CyclicGroup a m -> SFactors a m+cyclicGroupToSFactors = SFactors . \case+ CG2' -> [(Prime 2, 1)]+ CG4' -> [(Prime 2, 2)]+ CGOddPrimePower' p k -> [(p, k)]+ CGDoubleOddPrimePower' p k -> [(Prime 2, 1), (p, k)]++-- | Unidirectional pattern for residues modulo 2.+pattern CG2 :: CyclicGroup a m+pattern CG2 <- CG2'++-- | Unidirectional pattern for residues modulo 4.+pattern CG4 :: CyclicGroup a m+pattern CG4 <- CG4'++-- | Unidirectional pattern for residues modulo @p@^@k@ for __odd__ prime @p@.+pattern CGOddPrimePower :: Prime a -> Word -> CyclicGroup a m+pattern CGOddPrimePower p k <- CGOddPrimePower' p k++-- | Unidirectional pattern for residues modulo 2@p@^@k@ for __odd__ prime @p@.+pattern CGDoubleOddPrimePower :: Prime a -> Word -> CyclicGroup a m+pattern CGDoubleOddPrimePower p k <- CGDoubleOddPrimePower' p k++#if __GLASGOW_HASKELL__ > 801+{-# COMPLETE CG2, CG4, CGOddPrimePower, CGDoubleOddPrimePower #-}+#endif
Math/NumberTheory/Moduli/Sqrt.hs view
@@ -17,39 +17,29 @@ , sqrtsModFactorisation , sqrtsModPrimePower , sqrtsModPrime- -- * Old interface- , Old.sqrtModP- , Old.sqrtModPList- , Old.sqrtModP'- , Old.tonelliShanks- , Old.sqrtModPP- , Old.sqrtModPPList- , Old.sqrtModF- , Old.sqrtModFList ) where import Control.Monad (liftM2) import Data.Bits+import Data.Constraint import Math.NumberTheory.Moduli.Chinese-import Math.NumberTheory.Moduli.Class (Mod, getVal, getMod, KnownNat)+import Math.NumberTheory.Moduli.Class hiding (powMod) import Math.NumberTheory.Moduli.Jacobi+import Math.NumberTheory.Moduli.Singleton import Math.NumberTheory.Powers.Modular (powMod)-import Math.NumberTheory.Primes.Types-import Math.NumberTheory.Primes.Sieve (sieveFrom)-import Math.NumberTheory.Primes (Prime, factorise)+import Math.NumberTheory.Primes import Math.NumberTheory.Utils (shiftToOddCount, splitOff, recipMod) import Math.NumberTheory.Utils.FromIntegral -import qualified Math.NumberTheory.Moduli.SqrtOld as Old- -- | List all modular square roots. -- -- >>> :set -XDataKinds--- >>> sqrtsMod (1 :: Mod 60)+-- >>> sqrtsMod sfactors (1 :: Mod 60) -- [(1 `modulo` 60),(49 `modulo` 60),(41 `modulo` 60),(29 `modulo` 60),(31 `modulo` 60),(19 `modulo` 60),(11 `modulo` 60),(59 `modulo` 60)]-sqrtsMod :: KnownNat m => Mod m -> [Mod m]-sqrtsMod a = map fromInteger $ sqrtsModFactorisation (getVal a) (factorise (getMod a))+sqrtsMod :: SFactors Integer m -> Mod m -> [Mod m]+sqrtsMod sm a = case proofFromSFactors sm of+ Sub Dict -> map fromInteger $ sqrtsModFactorisation (getVal a) (unSFactors sm) -- | List all square roots modulo a number, the factorisation of which is -- passed as a second argument.@@ -61,7 +51,7 @@ sqrtsModFactorisation n pps = map fst $ foldl1 (liftM2 comb) cs where ms :: [Integer]- ms = map (\(Prime p, pow) -> p ^ pow) pps+ ms = map (\(p, pow) -> unPrime p ^ pow) pps rs :: [[Integer]] rs = map (\(p, pow) -> sqrtsModPrimePower n p pow) pps@@ -81,7 +71,7 @@ -- [3,12,21,24,6,15] sqrtsModPrimePower :: Integer -> Prime Integer -> Word -> [Integer] sqrtsModPrimePower nn p 1 = sqrtsModPrime nn p-sqrtsModPrimePower nn (Prime prime) expo = let primeExpo = prime ^ expo in+sqrtsModPrimePower nn (unPrime -> prime) expo = let primeExpo = prime ^ expo in case splitOff prime (nn `mod` primeExpo) of (_, 0) -> [0, prime ^ ((expo + 1) `quot` 2) .. primeExpo - 1] (kk, n)@@ -115,8 +105,8 @@ -- >>> sqrtsModPrime 2 (fromJust (isPrime 5)) -- [] sqrtsModPrime :: Integer -> Prime Integer -> [Integer]-sqrtsModPrime n (Prime 2) = [n `mod` 2]-sqrtsModPrime n (Prime prime) = case jacobi n prime of+sqrtsModPrime n (unPrime -> 2) = [n `mod` 2]+sqrtsModPrime n (unPrime -> prime) = case jacobi n prime of MinusOne -> [] Zero -> [0] One -> let r = sqrtModP' (n `mod` prime) prime in [r, prime - r]@@ -176,9 +166,10 @@ -- | prime must be odd, n must be coprime with prime sqrtModPP' :: Integer -> Integer -> Word -> Maybe Integer-sqrtModPP' n prime expo = case sqrtsModPrime n (Prime prime) of- [] -> Nothing- r : _ -> fixup r+sqrtModPP' n prime expo = case jacobi n prime of+ MinusOne -> Nothing+ Zero -> Nothing+ One -> fixup $ sqrtModP' (n `mod` prime) prime where fixup r = let diff' = r*r-n in if diff' == 0@@ -235,11 +226,14 @@ findNonSquare :: Integer -> Integer findNonSquare n- | rem8 n == 5 || rem8 n == 3 = 2- | otherwise = search primelist+ | rem8 n == 5 || rem8 n == 3 = 2+ | otherwise = search candidates where- primelist = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67]- ++ map unPrime (sieveFrom (68 + n `rem` 4)) -- prevent sharing+ -- It is enough to consider only prime candidates, but+ -- the probability that the smallest non-residue is > 67+ -- is small and 'jacobi' test is fast,+ -- so we use [71..n] instead of filter isPrime [71..n].+ candidates = 3:5:7:11:13:17:19:23:29:31:37:41:43:47:53:59:61:67:[71..n] search (p:ps) = case jacobi p n of MinusOne -> p _ -> search ps
− Math/NumberTheory/Moduli/SqrtOld.hs
@@ -1,232 +0,0 @@--- |--- Module: Math.NumberTheory.Moduli.Sqrt--- Copyright: (c) 2011 Daniel Fischer--- Licence: MIT--- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com>------ Modular square roots.-----{-# LANGUAGE BangPatterns #-}-{-# LANGUAGE CPP #-}-{-# LANGUAGE ViewPatterns #-}--module Math.NumberTheory.Moduli.SqrtOld- ( sqrtModP- , sqrtModPList- , sqrtModP'- , tonelliShanks- , sqrtModPP- , sqrtModPPList- , sqrtModF- , sqrtModFList- ) where--import Control.Monad (liftM2)-import Data.Bits-import Data.List (nub)-import GHC.Integer.GMP.Internals--import Math.NumberTheory.Moduli.Chinese-import Math.NumberTheory.Moduli.Jacobi-import Math.NumberTheory.Primes.Sieve (sieveFrom)-import Math.NumberTheory.Primes.Types (unPrime)-import Math.NumberTheory.Utils (shiftToOddCount, splitOff)-import Math.NumberTheory.Utils.FromIntegral--{-# DEPRECATED sqrtModP, sqrtModP', sqrtModPList, tonelliShanks "Use 'Math.NumberTheory.Moduli.Sqrt.sqrtsModPrime' instead" #-}-{-# DEPRECATED sqrtModPP, sqrtModPPList "Use 'Math.NumberTheory.Moduli.Sqrt.sqrtsModPrimePower' instead" #-}-{-# DEPRECATED sqrtModF, sqrtModFList "Use 'Math.NumberTheory.Moduli.Sqrt.sqrtsModFactorisation' or 'Math.NumberTheory.Moduli.Sqrt.sqrtsMod' instead" #-}----- | @sqrtModP n prime@ calculates a modular square root of @n@ modulo @prime@--- if that exists. The second argument /must/ be a (positive) prime, otherwise--- the computation may not terminate and if it does, may yield a wrong result.--- The precondition is /not/ checked.------ If @prime@ is a prime and @n@ a quadratic residue modulo @prime@, the result--- is @Just r@ where @r^2 ≡ n (mod prime)@, if @n@ is a quadratic nonresidue,--- the result is @Nothing@.-sqrtModP :: Integer -> Integer -> Maybe Integer-sqrtModP n 2 = Just (n `mod` 2)-sqrtModP n prime = case jacobi n prime of- MinusOne -> Nothing- Zero -> Just 0- One -> Just (sqrtModP' (n `mod` prime) prime)---- | @sqrtModPList n prime@ computes the list of all square roots of @n@--- modulo @prime@. @prime@ /must/ be a (positive) prime.--- The precondition is /not/ checked.-sqrtModPList :: Integer -> Integer -> [Integer]-sqrtModPList n prime- | prime == 2 = [n `mod` 2]- | otherwise = case sqrtModP n prime of- Just 0 -> [0]- Just r -> [r,prime-r] -- The group of units in Z/(p) is cyclic- _ -> []---- | @sqrtModP' square prime@ finds a square root of @square@ modulo--- prime. @prime@ /must/ be a (positive) prime, and @square@ /must/ be a positive--- quadratic residue modulo @prime@, i.e. @'jacobi square prime == 1@.--- The precondition is /not/ checked.-sqrtModP' :: Integer -> Integer -> Integer-sqrtModP' square prime- | prime == 2 = square- | rem4 prime == 3 = powModInteger square ((prime + 1) `quot` 4) prime- | otherwise = tonelliShanks square prime---- | @tonelliShanks square prime@ calculates a square root of @square@--- modulo @prime@, where @prime@ is a prime of the form @4*k + 1@ and--- @square@ is a positive quadratic residue modulo @prime@, using the--- Tonelli-Shanks algorithm.--- No checks on the input are performed.-tonelliShanks :: Integer -> Integer -> Integer-tonelliShanks square prime = loop rc t1 generator log2- where- (log2,q) = shiftToOddCount (prime-1)- nonSquare = findNonSquare prime- generator = powModInteger nonSquare q prime- rc = powModInteger square ((q+1) `quot` 2) prime- t1 = powModInteger square q prime- msqr x = (x*x) `rem` prime- msquare 0 x = x- msquare k x = msquare (k-1) (msqr x)- findPeriod per 1 = per- findPeriod per x = findPeriod (per+1) (msqr x)- loop !r t c m- | t == 1 = r- | otherwise = loop nextR nextT nextC nextM- where- nextM = findPeriod 0 t- b = msquare (m - 1 - nextM) c- nextR = (r*b) `rem` prime- nextC = msqr b- nextT = (t*nextC) `rem` prime---- | @sqrtModPP n (prime,expo)@ calculates a square root of @n@--- modulo @prime^expo@ if one exists. @prime@ /must/ be a--- (positive) prime. @expo@ must be positive, @n@ must be coprime--- to @prime@-sqrtModPP :: Integer -> (Integer,Int) -> Maybe Integer-sqrtModPP n (2,e) = sqM2P n e-sqrtModPP n (prime,expo) = case sqrtModP n prime of- Just r -> fixup r- _ -> Nothing- where- fixup r = let diff' = r*r-n- in if diff' == 0- then Just r- else case splitOff prime diff' of- (wordToInt -> e,q) | expo <= e -> Just r- | otherwise -> fmap (\inv -> hoist inv r (q `mod` prime) (prime^e)) (recipMod (2*r) prime)-- hoist inv root elim pp- | diff' == 0 = root'- | expo <= wordToInt ex = root'- | otherwise = hoist inv root' (nelim `mod` prime) (prime^ex)- where- root' = (root + (inv*(prime-elim))*pp) `mod` (prime*pp)- diff' = root'*root' - n- (ex, nelim) = splitOff prime diff'---- dirty, dirty-sqM2P :: Integer -> Int -> Maybe Integer-sqM2P n e- | e < 2 = Just (n `mod` 2)- | n' == 0 = Just 0- | odd k = Nothing- | otherwise = fmap ((`mod` mdl) . (`shiftL` k2)) $ solve s e2- where- mdl = 1 `shiftL` e- n' = n `mod` mdl- (wordToInt -> k,s) = shiftToOddCount n'- k2 = k `quot` 2- e2 = e-k- solve _ 1 = Just 1- solve 1 _ = Just 1- solve r _- | rem4 r == 3 = Nothing -- otherwise r ≡ 1 (mod 4)- | rem8 r == 5 = Nothing -- otherwise r ≡ 1 (mod 8)- | otherwise = fixup r (wordToInt $ fst $ shiftToOddCount (r-1))- where- fixup x pw- | pw >= e2 = Just x- | otherwise = fixup x' pw'- where- x' = x + (1 `shiftL` (pw-1))- d = x'*x' - r- pw' = if d == 0 then e2 else wordToInt (fst (shiftToOddCount d))---- | @sqrtModF n primePowers@ calculates a square root of @n@ modulo--- @product [p^k | (p,k) <- primePowers]@ if one exists and all primes--- are distinct.--- The list must be non-empty, @n@ must be coprime with all primes.-sqrtModF :: Integer -> [(Integer,Int)] -> Maybe Integer-sqrtModF _ [] = Nothing-sqrtModF n pps = do roots <- mapM (sqrtModPP n) pps- chineseRemainder $ zip roots (map (uncurry (^)) pps)---- | @sqrtModFList n primePowers@ calculates all square roots of @n@ modulo--- @product [p^k | (p,k) <- primePowers]@ if all primes are distinct.--- The list must be non-empty, @n@ must be coprime with all primes.-sqrtModFList :: Integer -> [(Integer,Int)] -> [Integer]-sqrtModFList _ [] = []-sqrtModFList n pps = map fst $ foldl1 (liftM2 comb) cs- where- ms :: [Integer]- ms = map (uncurry (^)) pps- rs :: [[Integer]]- rs = map (sqrtModPPList n) pps- cs :: [[(Integer,Integer)]]- cs = zipWith (\l m -> map (\x -> (x,m)) l) rs ms- comb t1@(_,m1) t2@(_,m2) = (chineseRemainder2 t1 t2,m1*m2)---- | @sqrtModPPList n (prime,expo)@ calculates the list of all--- square roots of @n@ modulo @prime^expo@. The same restriction--- as in 'sqrtModPP' applies to the arguments.-sqrtModPPList :: Integer -> (Integer,Int) -> [Integer]-sqrtModPPList n (2,1) = [n `mod` 2]-sqrtModPPList n (2,expo)- = case sqM2P n expo of- Just r -> let m = 1 `shiftL` (expo-1)- in nub [r, (r+m) `mod` (2*m), (m-r) `mod` (2*m), 2*m-r]- _ -> []-sqrtModPPList n pe@(prime,expo)- = case sqrtModPP n pe of- Just 0 -> [0]- Just r -> [prime^expo - r, r] -- The group of units in Z/(p^e) is cyclic- _ -> []----- Utilities--{-# SPECIALISE rem4 :: Integer -> Int,- Int -> Int,- Word -> Int- #-}-rem4 :: Integral a => a -> Int-rem4 n = fromIntegral n .&. 3--{-# SPECIALISE rem8 :: Integer -> Int,- Int -> Int,- Word -> Int- #-}-rem8 :: Integral a => a -> Int-rem8 n = fromIntegral n .&. 7--findNonSquare :: Integer -> Integer-findNonSquare n- | rem8 n == 5 || rem8 n == 3 = 2- | otherwise = search primelist- where- primelist = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67]- ++ map unPrime (sieveFrom (68 + n `rem` 4)) -- prevent sharing- search (p:ps) = case jacobi p n of- MinusOne -> p- _ -> search ps- search _ = error "Should never have happened, prime list exhausted."--recipMod :: Integer -> Integer -> Maybe Integer-recipMod x m = case recipModInteger x m of- 0 -> Nothing- y -> Just y
Math/NumberTheory/MoebiusInversion.hs view
@@ -17,20 +17,32 @@ import Control.Monad import Control.Monad.ST-import qualified Data.Vector.Mutable as MV+import Data.Proxy+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Generic.Mutable as MG import Math.NumberTheory.Powers.Squares+import Math.NumberTheory.Utils.FromIntegral -- | @totientSum n@ is, for @n > 0@, the sum of @[totient k | k <- [1 .. n]]@, -- computed via generalised Möbius inversion. -- See <http://mathworld.wolfram.com/TotientSummatoryFunction.html> for the -- formula used for @totientSum@.-totientSum :: Int -> Integer-totientSum n- | n < 1 = 0- | otherwise = generalInversion (triangle . fromIntegral) n+--+-- >>> import Data.Proxy+-- >>> totientSum (Proxy :: Proxy Data.Vector.Unboxed.Vector) 100 :: Int+-- 3044+-- >>> totientSum (Proxy :: Proxy Data.Vector.Vector) 100 :: Integer+-- 3044+totientSum+ :: (Integral t, G.Vector v t)+ => Proxy v+ -> Word+ -> t+totientSum _ 0 = 0+totientSum proxy n = generalInversion proxy (triangle . fromIntegral) n where- triangle k = (k*(k+1)) `quot` 2+ triangle k = (k * (k + 1)) `quot` 2 -- | @generalInversion g n@ evaluates the generalised Möbius inversion of @g@ -- at the argument @n@.@@ -76,28 +88,36 @@ -- The value @f n@ is then computed by @generalInversion g n@. Note that when -- many values of @f@ are needed, there are far more efficient methods, this -- method is only appropriate to compute isolated values of @f@.-generalInversion :: (Int -> Integer) -> Int -> Integer-generalInversion fun n- | n < 1 = error "Möbius inversion only defined on positive domain"- | n == 1 = fun 1- | n == 2 = fun 2 - fun 1- | n == 3 = fun 3 - 2*fun 1- | otherwise = fastInvert fun n--fastInvert :: (Int -> Integer) -> Int -> Integer-fastInvert fun n = runST (fastInvertST fun n)+generalInversion+ :: (Num t, G.Vector v t)+ => Proxy v+ -> (Word -> t)+ -> Word+ -> t+generalInversion proxy fun n = case n of+ 0 ->error "Möbius inversion only defined on positive domain"+ 1 -> fun 1+ 2 -> fun 2 - fun 1+ 3 -> fun 3 - 2*fun 1+ _ -> runST (fastInvertST proxy (fun . intToWord) (wordToInt n)) -fastInvertST :: forall s. (Int -> Integer) -> Int -> ST s Integer-fastInvertST fun n = do+fastInvertST+ :: forall s t v.+ (Num t, G.Vector v t)+ => Proxy v+ -> (Int -> t)+ -> Int+ -> ST s t+fastInvertST _ fun n = do let !k0 = integerSquareRoot (n `quot` 2) !mk0 = n `quot` (2*k0+1) kmax a m = (a `quot` m - 1) `quot` 2 - small <- MV.unsafeNew (mk0 + 1) :: ST s (MV.MVector s Integer)- MV.unsafeWrite small 0 0- MV.unsafeWrite small 1 $! (fun 1)+ small <- MG.unsafeNew (mk0 + 1) :: ST s (G.Mutable v s t)+ MG.unsafeWrite small 0 0+ MG.unsafeWrite small 1 $! (fun 1) when (mk0 >= 2) $- MV.unsafeWrite small 2 $! (fun 2 - fun 1)+ MG.unsafeWrite small 2 $! (fun 2 - fun 1) let calcit :: Int -> Int -> Int -> ST s (Int, Int) calcit switch change i@@ -107,22 +127,22 @@ let mloop !acc k !m | k < switch = kloop acc k | otherwise = do- val <- MV.unsafeRead small m+ val <- MG.unsafeRead small m let nxtk = kmax i (m+1) mloop (acc - fromIntegral (k-nxtk)*val) nxtk (m+1) kloop !acc k | k == 0 = do- MV.unsafeWrite small i $! acc+ MG.unsafeWrite small i $! acc calcit switch change (i+1) | otherwise = do- val <- MV.unsafeRead small (i `quot` (2*k+1))+ val <- MG.unsafeRead small (i `quot` (2*k+1)) kloop (acc-val) (k-1) mloop (fun i - fun (i `quot` 2)) ((i-1) `quot` 2) 1 (sw, ch) <- calcit 1 8 3- large <- MV.unsafeNew k0 :: ST s (MV.MVector s Integer)+ large <- MG.unsafeNew k0 :: ST s (G.Mutable v s t) - let calcbig :: Int -> Int -> Int -> ST s (MV.MVector s Integer)+ let calcbig :: Int -> Int -> Int -> ST s (G.Mutable v s t) calcbig switch change j | j == 0 = return large | (2*j-1)*change <= n = calcbig (switch+1) (change + 4*switch+6) j@@ -131,21 +151,21 @@ mloop !acc k m | k < switch = kloop acc k | otherwise = do- val <- MV.unsafeRead small m+ val <- MG.unsafeRead small m let nxtk = kmax i (m+1) mloop (acc - fromIntegral (k-nxtk)*val) nxtk (m+1) kloop !acc k | k == 0 = do- MV.unsafeWrite large (j-1) $! acc+ MG.unsafeWrite large (j-1) $! acc calcbig switch change (j-1) | otherwise = do let m = i `quot` (2*k+1) val <- if m <= mk0- then MV.unsafeRead small m- else MV.unsafeRead large (k*(2*j-1)+j-1)+ then MG.unsafeRead small m+ else MG.unsafeRead large (k*(2*j-1)+j-1) kloop (acc-val) (k-1) mloop (fun i - fun (i `quot` 2)) ((i-1) `quot` 2) 1 mvec <- calcbig sw ch k0- MV.unsafeRead mvec 0+ MG.unsafeRead mvec 0
Math/NumberTheory/MoebiusInversion/Int.hs view
@@ -10,7 +10,9 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE ScopedTypeVariables #-} -module Math.NumberTheory.MoebiusInversion.Int+{-# OPTIONS_HADDOCK hide #-}++module Math.NumberTheory.MoebiusInversion.Int {-# DEPRECATED "Use Math.NumberTheory.MoebiusInversion" #-} ( generalInversion , totientSum ) where
Math/NumberTheory/Powers/General.hs view
@@ -28,6 +28,7 @@ import Data.Bits import Data.List (foldl') import qualified Data.Set as Set+import Data.Vector.Unboxed (toList) import Numeric.Natural @@ -38,6 +39,7 @@ import qualified Math.NumberTheory.Powers.Squares as P2 import qualified Math.NumberTheory.Powers.Cubes as P3 import qualified Math.NumberTheory.Powers.Fourth as P4+import Math.NumberTheory.Primes.Small import Math.NumberTheory.Utils.FromIntegral (intToWord, wordToInt) -- | Calculate an integer root, @'integerRoot' k n@ computes the (floor of) the @k@-th@@ -256,20 +258,18 @@ (k,r) -> findHighPower (gcd k e) ((p,k):pws) r (P2.integerSquareRoot r) ps findHighPower e pws m _ [] = finishPower e pws m +smallOddPrimes :: [Integer]+smallOddPrimes+ = takeWhile (< spBound)+ $ map fromIntegral+ $ tail+ $ toList smallPrimes+ spBEx :: Word spBEx = 14 spBound :: Integer spBound = 2^spBEx--smallOddPrimes :: [Integer]-smallOddPrimes = 3:5:primes'- where- primes' = 7:11:13:17:19:23:29:filter isPrime (takeWhile (< spBound) $ scanl (+) 31 (cycle [6,4,2,4,2,4,6,2]))- isPrime n = go primes'- where- go (p:ps) = (p*p > n) || (n `rem` p /= 0 && go ps)- go [] = True -- n large, has no prime divisors < spBound finishPower :: Word -> [(Integer, Word)] -> Integer -> (Integer, Word)
Math/NumberTheory/Prefactored.hs view
@@ -18,9 +18,12 @@ , fromFactors ) where +import Prelude hiding ((^), gcd) import Control.Arrow- import Data.Semigroup+import Data.Semiring (Semiring(..), Mul(..), (^))+import qualified Data.Semiring as Semiring+import Unsafe.Coerce import Math.NumberTheory.Euclidean import Math.NumberTheory.Euclidean.Coprimes@@ -84,7 +87,7 @@ -- -- >>> fromValue 123 -- Prefactored {prefValue = 123, prefFactors = Coprimes {unCoprimes = [(123,1)]}}-fromValue :: (Eq a, Num a) => a -> Prefactored a+fromValue :: (Eq a, GcdDomain a) => a -> Prefactored a fromValue a = Prefactored a (singleton a 1) -- | Create 'Prefactored' from a given list of pairwise coprime@@ -94,10 +97,10 @@ -- Prefactored {prefValue = 23100, prefFactors = Coprimes {unCoprimes = [(28,1),(33,1),(5,2)]}} -- >>> fromFactors (splitIntoCoprimes [(140, 2), (165, 3)]) -- Prefactored {prefValue = 88045650000, prefFactors = Coprimes {unCoprimes = [(28,2),(33,3),(5,5)]}}-fromFactors :: Num a => Coprimes a Word -> Prefactored a-fromFactors as = Prefactored (product (map (uncurry (^)) (unCoprimes as))) as+fromFactors :: Semiring a => Coprimes a Word -> Prefactored a+fromFactors as = Prefactored (getMul $ foldMap (\(a, k) -> Mul $ a ^ k) (unCoprimes as)) as -instance Euclidean a => Num (Prefactored a) where+instance (Eq a, Num a, GcdDomain a) => Num (Prefactored a) where Prefactored v1 _ + Prefactored v2 _ = fromValue (v1 + v2) Prefactored v1 _ - Prefactored v2 _@@ -109,7 +112,7 @@ signum (Prefactored v _) = Prefactored (signum v) mempty fromInteger n = fromValue (fromInteger n) -instance (Euclidean a, UniqueFactorisation a) => UniqueFactorisation (Prefactored a) where+instance (Eq a, GcdDomain a, UniqueFactorisation a) => UniqueFactorisation (Prefactored a) where factorise (Prefactored _ f) = concatMap (\(x, xm) -> map (\(p, k) -> (Prime $ fromValue $ unPrime p, k * xm)) (factorise x)) (unCoprimes f) isPrime (Prefactored _ f) = case unCoprimes f of
Math/NumberTheory/Primes.hs view
@@ -10,6 +10,7 @@ {-# LANGUAGE LambdaCase #-} {-# OPTIONS_GHC -fno-warn-orphans #-}+{-# OPTIONS_GHC -fno-warn-deprecations #-} module Math.NumberTheory.Primes ( Prime@@ -17,6 +18,7 @@ , nextPrime , precPrime , UniqueFactorisation(..)+ , factorBack , -- * Old interface primes ) where@@ -97,6 +99,9 @@ factorise = map (coerce integerToNatural *** id) . F.factorise . naturalToInteger isPrime n = if T.isPrime (toInteger n) then Just (Prime n) else Nothing +factorBack :: Num a => [(Prime a, Word)] -> a+factorBack = product . map (\(p, k) -> unPrime p ^ k)+ -- | Smallest prime, greater or equal to argument. -- -- > nextPrime (-100) == 2@@ -192,21 +197,21 @@ enumFromThenGeneric :: (Bits a, Integral a, UniqueFactorisation a) => Prime a -> Prime a -> [Prime a] enumFromThenGeneric p@(Prime p') (Prime q') = case p' `compare` q' of- LT -> filter (\(Prime r') -> (r' - p') `mod` delta == 0) $ enumFromGeneric p+ LT -> filter (\(Prime r') -> (r' - p') `rem` delta == 0) $ enumFromGeneric p where delta = q' - p' EQ -> repeat p- GT -> filter (\(Prime r') -> (p' - r') `mod` delta == 0) $ reverse $ enumFromToGeneric (Prime 2) p+ GT -> filter (\(Prime r') -> (p' - r') `rem` delta == 0) $ reverse $ enumFromToGeneric (Prime 2) p where delta = p' - q' enumFromThenToGeneric :: (Bits a, Integral a, UniqueFactorisation a) => Prime a -> Prime a -> Prime a -> [Prime a] enumFromThenToGeneric p@(Prime p') (Prime q') r@(Prime r') = case p' `compare` q' of- LT -> filter (\(Prime t') -> (t' - p') `mod` delta == 0) $ enumFromToGeneric p r+ LT -> filter (\(Prime t') -> (t' - p') `rem` delta == 0) $ enumFromToGeneric p r where delta = q' - p' EQ -> if p' <= r' then repeat p else []- GT -> filter (\(Prime t') -> (p' - t') `mod` delta == 0) $ reverse $ enumFromToGeneric r p+ GT -> filter (\(Prime t') -> (p' - t') `rem` delta == 0) $ reverse $ enumFromToGeneric r p where delta = p' - q'
Math/NumberTheory/Primes/Counting/Impl.hs view
@@ -23,8 +23,9 @@ #include "MachDeps.h" import Math.NumberTheory.Primes.Sieve.Eratosthenes-import Math.NumberTheory.Primes.Sieve.Indexing-import Math.NumberTheory.Primes.Counting.Approximate+ (PrimeSieve(..), primeList, primeSieve, psieveFrom, sieveTo, sieveBits, sieveRange, countFromTo, countToNth, countAll, nthPrimeCt)+import Math.NumberTheory.Primes.Sieve.Indexing (toPrim, idxPr)+import Math.NumberTheory.Primes.Counting.Approximate (nthPrimeApprox, approxPrimeCount) import Math.NumberTheory.Primes.Types import Math.NumberTheory.Powers.Squares import Math.NumberTheory.Powers.Cubes
Math/NumberTheory/Primes/Factorisation.hs view
@@ -1,5 +1,6 @@ -- | -- Module: Math.NumberTheory.Primes.Factorisation+-- Description: Deprecated -- Copyright: (c) 2011 Daniel Fischer -- Licence: MIT -- Maintainer: Daniel Fischer <daniel.is.fischer@googlemail.com>@@ -12,7 +13,7 @@ -- and in the case of the Carmichael function that the list of prime factors -- with their multiplicities is ascending. -module Math.NumberTheory.Primes.Factorisation+module Math.NumberTheory.Primes.Factorisation {-# DEPRECATED "Use 'Math.NumberTheory.Primes.factorise' instead" #-} ( -- * Factorisation functions -- $algorithm -- ** Complete factorisation
Math/NumberTheory/Primes/Factorisation/Certified.hs view
@@ -1,5 +1,6 @@ -- | -- Module: Math.NumberTheory.Primes.Factorisation.Certified+-- Description: Deprecated -- Copyright: (c) 2011 Daniel Fischer -- Licence: MIT -- Maintainer: Daniel Fischer <daniel.is.fischer@googlemail.com>@@ -9,7 +10,7 @@ -- For large numbers, this will be very slow in general. -- Use only if you're paranoid or must be /really/ sure. {-# LANGUAGE BangPatterns, CPP #-}-module Math.NumberTheory.Primes.Factorisation.Certified+module Math.NumberTheory.Primes.Factorisation.Certified {-# DEPRECATED "This module will be removed in the next release" #-} ( certifiedFactorisation , certificateFactorisation , provenFactorisation@@ -31,27 +32,24 @@ certifiedFactorisation :: Integer -> [(Integer, Word)] certifiedFactorisation = map fst . certificateFactorisation --- | @'certificateFactorisation' n@ produces a 'provenFactorisation'--- with a default bound of @100000@.+-- | @'certificateFactorisation' n@ produces a 'provenFactorisation'. certificateFactorisation :: Integer -> [((Integer, Word),PrimalityProof)]-certificateFactorisation n = provenFactorisation 100000 n+certificateFactorisation n = provenFactorisation n --- | @'provenFactorisation' bound n@ constructs a the prime factorisation of @n@+-- | @'provenFactorisation' n@ constructs a the prime factorisation of @n@ -- (which must be positive) together with proofs of primality of the factors,--- using trial division up to @bound@ (which is arbitrarily replaced by @2000@--- if the supplied value is smaller) and elliptic curve factorisation for the+-- using trial division up to 2^16 and elliptic curve factorisation for the -- remaining factors if necessary. -- -- Construction of primality proofs can take a /very/ long time, so this -- will usually be slow (but should be faster than using 'factorise' and -- proving the primality of the factors from scratch).-provenFactorisation :: Integer -> Integer -> [((Integer, Word),PrimalityProof)]-provenFactorisation _ 1 = []-provenFactorisation bd n+provenFactorisation :: Integer -> [((Integer, Word),PrimalityProof)]+provenFactorisation 1 = []+provenFactorisation n | n < 2 = error "provenFactorisation: argument not positive"- | bd < 2000 = provenFactorisation 2000 n- | otherwise = test $- case smallFactors bd n of+ | otherwise = let bd = 65536 in test $+ case smallFactors n of (sfs,mb) -> map (\t@(p,_) -> (t, smallCert p)) sfs ++ case mb of Nothing -> []
Math/NumberTheory/Primes/Factorisation/Montgomery.hs view
@@ -59,6 +59,7 @@ import Data.Semigroup #endif import Data.Traversable+import Data.Vector.Unboxed (toList) import GHC.TypeNats.Compat @@ -67,10 +68,10 @@ import Math.NumberTheory.Moduli.Class import Math.NumberTheory.Powers.General (highestPower, largePFPower) import Math.NumberTheory.Powers.Squares (integerSquareRoot')-import Math.NumberTheory.Primes.Sieve.Eratosthenes-import Math.NumberTheory.Primes.Sieve.Indexing+import Math.NumberTheory.Primes.Sieve.Eratosthenes (PrimeSieve(..), psieveFrom)+import Math.NumberTheory.Primes.Sieve.Indexing (toPrim)+import Math.NumberTheory.Primes.Small import Math.NumberTheory.Primes.Testing.Probabilistic-import Math.NumberTheory.Primes.Types (unPrime) import Math.NumberTheory.Unsafe import Math.NumberTheory.Utils @@ -100,10 +101,10 @@ -- seem to be slower than the 'StdGen' based variant. stepFactorisation :: Integer -> [(Integer, Word)] stepFactorisation n- = let (sfs,mb) = smallFactors 100000 n+ = let (sfs,mb) = smallFactors n in sfs ++ case mb of Nothing -> []- Just r -> curveFactorisation (Just 10000000000) bailliePSW+ Just r -> curveFactorisation (Just $ 65536 * 65536) bailliePSW (\m k -> (if k < (m-1) then k else error "Curves exhausted",k+1)) 6 Nothing r -- | @'defaultStdGenFactorisation'@ first strips off all small prime factors and then,@@ -122,10 +123,10 @@ -- @n@ must be larger than @1@. defaultStdGenFactorisation' :: StdGen -> Integer -> [(Integer, Word)] defaultStdGenFactorisation' sg n- = let (sfs,mb) = smallFactors 100000 n+ = let (sfs,mb) = smallFactors n in sfs ++ case mb of Nothing -> []- Just m -> stdGenFactorisation (Just 10000000000) sg Nothing m+ Just m -> stdGenFactorisation (Just $ 65536 * 65536) sg Nothing m ---------------------------------------------------------------------------------------------------- -- Factorisation wrappers --@@ -155,7 +156,7 @@ -- make a huge difference. So, if the default takes too long, try another one; or you can improve your -- chances for a quick result by running several instances in parallel. ----- 'curveFactorisation' @n@ requires that small (< 100000) prime factors of @n@+-- 'curveFactorisation' @n@ requires that small (< 65536) prime factors of @n@ -- have been stripped before. Otherwise it is likely to cycle forever. When in doubt, -- use 'defaultStdGenFactorisation'. --@@ -288,8 +289,9 @@ g -> Just g where n = getMod s- smallPrimes = takeWhile (<= b1) (2 : 3 : 5 : list primeStore)- smallPowers = map findPower smallPrimes+ smallPowers+ = map findPower+ $ takeWhile (<= b1) (2 : 3 : 5 : list primeStore) findPower p = go p where go acc@@ -346,23 +348,24 @@ list sieves = concat [[off + toPrim i | i <- [0 .. li], unsafeAt bs i] | PS vO bs <- sieves, let { (_,li) = bounds bs; off = fromInteger vO; }] --- | @'smallFactors' bound n@ finds all prime divisors of @n > 1@ up to @bound@ by trial division and returns the+-- | @'smallFactors' n@ finds all prime divisors of @n > 1@ up to 2^16 by trial division and returns the -- list of these together with their multiplicities, and a possible remaining factor which may be composite.-smallFactors :: Integer -> Integer -> ([(Integer, Word)], Maybe Integer)-smallFactors bd n = case shiftToOddCount n of+smallFactors :: Integer -> ([(Integer, Word)], Maybe Integer)+smallFactors n = case shiftToOddCount n of (0,m) -> go m prms (k,m) -> (2,k) <: if m == 1 then ([],Nothing) else go m prms where- prms = map unPrime $ tail (primeStore >>= primeList)+ prms = map fromIntegral $ toList smallPrimes x <: ~(l,b) = (x:l,b)+ go m []+ | m < 65536 * 65536 = ([(m, 1)], Nothing)+ | otherwise = ([], Just m) go m (p:ps)- | m < p*p = ([(m,1)], Nothing)- | bd < p = ([], Just m)- | otherwise = case splitOff p m of- (0,_) -> go m ps- (k,r) | r == 1 -> ([(p,k)], Nothing)- | otherwise -> (p,k) <: go r ps- go m [] = ([(m,1)], Nothing)+ | m < p*p = ([(m,1)], Nothing)+ | otherwise = case splitOff p m of+ (0,_) -> go m ps+ (k,r) | r == 1 -> ([(p,k)], Nothing)+ | otherwise -> (p,k) <: go r ps -- | For a given estimated decimal length of the smallest prime factor -- ("tier") return parameters B1, B2 and the number of curves to try
Math/NumberTheory/Primes/Factorisation/TrialDivision.hs view
@@ -17,7 +17,7 @@ , trialDivisionPrimeTo ) where -import Math.NumberTheory.Primes.Sieve.Eratosthenes+import Math.NumberTheory.Primes.Sieve.Eratosthenes (primeList, primeSieve, psieveList) import Math.NumberTheory.Powers.Squares import Math.NumberTheory.Primes.Types import Math.NumberTheory.Utils
Math/NumberTheory/Primes/Sieve.hs view
@@ -1,5 +1,6 @@ -- | -- Module: Math.NumberTheory.Primes.Sieve+-- Description: Deprecated -- Copyright: (c) 2011 Daniel Fischer -- Licence: MIT -- Maintainer: Daniel Fischer <daniel.is.fischer@googlemail.com>@@ -13,7 +14,8 @@ -- However, that means we must store data for primes up to the square root of -- where sieving is done, thus sieving primes up to @n@ requires -- @/O/(sqrt n/log n)@ space.-module Math.NumberTheory.Primes.Sieve++module Math.NumberTheory.Primes.Sieve {-# DEPRECATED "Use 'Enum' instance of 'Math.NumberTheory.Primes.Prime' instead" #-} ( -- * Limitations -- $limits
+ Math/NumberTheory/Primes/Small.hs view
@@ -0,0 +1,20 @@+-- |+-- Module: Math.NumberTheory.Primes.Small+-- Copyright: (c) 2019 Andrew Lelechenko+-- Licence: MIT+-- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com>+--+-- This is an internal module,+-- defining an array of precalculated primes < 2^16.+--++module Math.NumberTheory.Primes.Small+ ( smallPrimes+ ) where++import Data.Vector.Unboxed (Vector, fromList)+import Data.Word++smallPrimes :: Vector Word16+smallPrimes = fromList+ [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919,7927,7933,7937,7949,7951,7963,7993,8009,8011,8017,8039,8053,8059,8069,8081,8087,8089,8093,8101,8111,8117,8123,8147,8161,8167,8171,8179,8191,8209,8219,8221,8231,8233,8237,8243,8263,8269,8273,8287,8291,8293,8297,8311,8317,8329,8353,8363,8369,8377,8387,8389,8419,8423,8429,8431,8443,8447,8461,8467,8501,8513,8521,8527,8537,8539,8543,8563,8573,8581,8597,8599,8609,8623,8627,8629,8641,8647,8663,8669,8677,8681,8689,8693,8699,8707,8713,8719,8731,8737,8741,8747,8753,8761,8779,8783,8803,8807,8819,8821,8831,8837,8839,8849,8861,8863,8867,8887,8893,8923,8929,8933,8941,8951,8963,8969,8971,8999,9001,9007,9011,9013,9029,9041,9043,9049,9059,9067,9091,9103,9109,9127,9133,9137,9151,9157,9161,9173,9181,9187,9199,9203,9209,9221,9227,9239,9241,9257,9277,9281,9283,9293,9311,9319,9323,9337,9341,9343,9349,9371,9377,9391,9397,9403,9413,9419,9421,9431,9433,9437,9439,9461,9463,9467,9473,9479,9491,9497,9511,9521,9533,9539,9547,9551,9587,9601,9613,9619,9623,9629,9631,9643,9649,9661,9677,9679,9689,9697,9719,9721,9733,9739,9743,9749,9767,9769,9781,9787,9791,9803,9811,9817,9829,9833,9839,9851,9857,9859,9871,9883,9887,9901,9907,9923,9929,9931,9941,9949,9967,9973,10007,10009,10037,10039,10061,10067,10069,10079,10091,10093,10099,10103,10111,10133,10139,10141,10151,10159,10163,10169,10177,10181,10193,10211,10223,10243,10247,10253,10259,10267,10271,10273,10289,10301,10303,10313,10321,10331,10333,10337,10343,10357,10369,10391,10399,10427,10429,10433,10453,10457,10459,10463,10477,10487,10499,10501,10513,10529,10531,10559,10567,10589,10597,10601,10607,10613,10627,10631,10639,10651,10657,10663,10667,10687,10691,10709,10711,10723,10729,10733,10739,10753,10771,10781,10789,10799,10831,10837,10847,10853,10859,10861,10867,10883,10889,10891,10903,10909,10937,10939,10949,10957,10973,10979,10987,10993,11003,11027,11047,11057,11059,11069,11071,11083,11087,11093,11113,11117,11119,11131,11149,11159,11161,11171,11173,11177,11197,11213,11239,11243,11251,11257,11261,11273,11279,11287,11299,11311,11317,11321,11329,11351,11353,11369,11383,11393,11399,11411,11423,11437,11443,11447,11467,11471,11483,11489,11491,11497,11503,11519,11527,11549,11551,11579,11587,11593,11597,11617,11621,11633,11657,11677,11681,11689,11699,11701,11717,11719,11731,11743,11777,11779,11783,11789,11801,11807,11813,11821,11827,11831,11833,11839,11863,11867,11887,11897,11903,11909,11923,11927,11933,11939,11941,11953,11959,11969,11971,11981,11987,12007,12011,12037,12041,12043,12049,12071,12073,12097,12101,12107,12109,12113,12119,12143,12149,12157,12161,12163,12197,12203,12211,12227,12239,12241,12251,12253,12263,12269,12277,12281,12289,12301,12323,12329,12343,12347,12373,12377,12379,12391,12401,12409,12413,12421,12433,12437,12451,12457,12473,12479,12487,12491,12497,12503,12511,12517,12527,12539,12541,12547,12553,12569,12577,12583,12589,12601,12611,12613,12619,12637,12641,12647,12653,12659,12671,12689,12697,12703,12713,12721,12739,12743,12757,12763,12781,12791,12799,12809,12821,12823,12829,12841,12853,12889,12893,12899,12907,12911,12917,12919,12923,12941,12953,12959,12967,12973,12979,12983,13001,13003,13007,13009,13033,13037,13043,13049,13063,13093,13099,13103,13109,13121,13127,13147,13151,13159,13163,13171,13177,13183,13187,13217,13219,13229,13241,13249,13259,13267,13291,13297,13309,13313,13327,13331,13337,13339,13367,13381,13397,13399,13411,13417,13421,13441,13451,13457,13463,13469,13477,13487,13499,13513,13523,13537,13553,13567,13577,13591,13597,13613,13619,13627,13633,13649,13669,13679,13681,13687,13691,13693,13697,13709,13711,13721,13723,13729,13751,13757,13759,13763,13781,13789,13799,13807,13829,13831,13841,13859,13873,13877,13879,13883,13901,13903,13907,13913,13921,13931,13933,13963,13967,13997,13999,14009,14011,14029,14033,14051,14057,14071,14081,14083,14087,14107,14143,14149,14153,14159,14173,14177,14197,14207,14221,14243,14249,14251,14281,14293,14303,14321,14323,14327,14341,14347,14369,14387,14389,14401,14407,14411,14419,14423,14431,14437,14447,14449,14461,14479,14489,14503,14519,14533,14537,14543,14549,14551,14557,14561,14563,14591,14593,14621,14627,14629,14633,14639,14653,14657,14669,14683,14699,14713,14717,14723,14731,14737,14741,14747,14753,14759,14767,14771,14779,14783,14797,14813,14821,14827,14831,14843,14851,14867,14869,14879,14887,14891,14897,14923,14929,14939,14947,14951,14957,14969,14983,15013,15017,15031,15053,15061,15073,15077,15083,15091,15101,15107,15121,15131,15137,15139,15149,15161,15173,15187,15193,15199,15217,15227,15233,15241,15259,15263,15269,15271,15277,15287,15289,15299,15307,15313,15319,15329,15331,15349,15359,15361,15373,15377,15383,15391,15401,15413,15427,15439,15443,15451,15461,15467,15473,15493,15497,15511,15527,15541,15551,15559,15569,15581,15583,15601,15607,15619,15629,15641,15643,15647,15649,15661,15667,15671,15679,15683,15727,15731,15733,15737,15739,15749,15761,15767,15773,15787,15791,15797,15803,15809,15817,15823,15859,15877,15881,15887,15889,15901,15907,15913,15919,15923,15937,15959,15971,15973,15991,16001,16007,16033,16057,16061,16063,16067,16069,16073,16087,16091,16097,16103,16111,16127,16139,16141,16183,16187,16189,16193,16217,16223,16229,16231,16249,16253,16267,16273,16301,16319,16333,16339,16349,16361,16363,16369,16381,16411,16417,16421,16427,16433,16447,16451,16453,16477,16481,16487,16493,16519,16529,16547,16553,16561,16567,16573,16603,16607,16619,16631,16633,16649,16651,16657,16661,16673,16691,16693,16699,16703,16729,16741,16747,16759,16763,16787,16811,16823,16829,16831,16843,16871,16879,16883,16889,16901,16903,16921,16927,16931,16937,16943,16963,16979,16981,16987,16993,17011,17021,17027,17029,17033,17041,17047,17053,17077,17093,17099,17107,17117,17123,17137,17159,17167,17183,17189,17191,17203,17207,17209,17231,17239,17257,17291,17293,17299,17317,17321,17327,17333,17341,17351,17359,17377,17383,17387,17389,17393,17401,17417,17419,17431,17443,17449,17467,17471,17477,17483,17489,17491,17497,17509,17519,17539,17551,17569,17573,17579,17581,17597,17599,17609,17623,17627,17657,17659,17669,17681,17683,17707,17713,17729,17737,17747,17749,17761,17783,17789,17791,17807,17827,17837,17839,17851,17863,17881,17891,17903,17909,17911,17921,17923,17929,17939,17957,17959,17971,17977,17981,17987,17989,18013,18041,18043,18047,18049,18059,18061,18077,18089,18097,18119,18121,18127,18131,18133,18143,18149,18169,18181,18191,18199,18211,18217,18223,18229,18233,18251,18253,18257,18269,18287,18289,18301,18307,18311,18313,18329,18341,18353,18367,18371,18379,18397,18401,18413,18427,18433,18439,18443,18451,18457,18461,18481,18493,18503,18517,18521,18523,18539,18541,18553,18583,18587,18593,18617,18637,18661,18671,18679,18691,18701,18713,18719,18731,18743,18749,18757,18773,18787,18793,18797,18803,18839,18859,18869,18899,18911,18913,18917,18919,18947,18959,18973,18979,19001,19009,19013,19031,19037,19051,19069,19073,19079,19081,19087,19121,19139,19141,19157,19163,19181,19183,19207,19211,19213,19219,19231,19237,19249,19259,19267,19273,19289,19301,19309,19319,19333,19373,19379,19381,19387,19391,19403,19417,19421,19423,19427,19429,19433,19441,19447,19457,19463,19469,19471,19477,19483,19489,19501,19507,19531,19541,19543,19553,19559,19571,19577,19583,19597,19603,19609,19661,19681,19687,19697,19699,19709,19717,19727,19739,19751,19753,19759,19763,19777,19793,19801,19813,19819,19841,19843,19853,19861,19867,19889,19891,19913,19919,19927,19937,19949,19961,19963,19973,19979,19991,19993,19997,20011,20021,20023,20029,20047,20051,20063,20071,20089,20101,20107,20113,20117,20123,20129,20143,20147,20149,20161,20173,20177,20183,20201,20219,20231,20233,20249,20261,20269,20287,20297,20323,20327,20333,20341,20347,20353,20357,20359,20369,20389,20393,20399,20407,20411,20431,20441,20443,20477,20479,20483,20507,20509,20521,20533,20543,20549,20551,20563,20593,20599,20611,20627,20639,20641,20663,20681,20693,20707,20717,20719,20731,20743,20747,20749,20753,20759,20771,20773,20789,20807,20809,20849,20857,20873,20879,20887,20897,20899,20903,20921,20929,20939,20947,20959,20963,20981,20983,21001,21011,21013,21017,21019,21023,21031,21059,21061,21067,21089,21101,21107,21121,21139,21143,21149,21157,21163,21169,21179,21187,21191,21193,21211,21221,21227,21247,21269,21277,21283,21313,21317,21319,21323,21341,21347,21377,21379,21383,21391,21397,21401,21407,21419,21433,21467,21481,21487,21491,21493,21499,21503,21517,21521,21523,21529,21557,21559,21563,21569,21577,21587,21589,21599,21601,21611,21613,21617,21647,21649,21661,21673,21683,21701,21713,21727,21737,21739,21751,21757,21767,21773,21787,21799,21803,21817,21821,21839,21841,21851,21859,21863,21871,21881,21893,21911,21929,21937,21943,21961,21977,21991,21997,22003,22013,22027,22031,22037,22039,22051,22063,22067,22073,22079,22091,22093,22109,22111,22123,22129,22133,22147,22153,22157,22159,22171,22189,22193,22229,22247,22259,22271,22273,22277,22279,22283,22291,22303,22307,22343,22349,22367,22369,22381,22391,22397,22409,22433,22441,22447,22453,22469,22481,22483,22501,22511,22531,22541,22543,22549,22567,22571,22573,22613,22619,22621,22637,22639,22643,22651,22669,22679,22691,22697,22699,22709,22717,22721,22727,22739,22741,22751,22769,22777,22783,22787,22807,22811,22817,22853,22859,22861,22871,22877,22901,22907,22921,22937,22943,22961,22963,22973,22993,23003,23011,23017,23021,23027,23029,23039,23041,23053,23057,23059,23063,23071,23081,23087,23099,23117,23131,23143,23159,23167,23173,23189,23197,23201,23203,23209,23227,23251,23269,23279,23291,23293,23297,23311,23321,23327,23333,23339,23357,23369,23371,23399,23417,23431,23447,23459,23473,23497,23509,23531,23537,23539,23549,23557,23561,23563,23567,23581,23593,23599,23603,23609,23623,23627,23629,23633,23663,23669,23671,23677,23687,23689,23719,23741,23743,23747,23753,23761,23767,23773,23789,23801,23813,23819,23827,23831,23833,23857,23869,23873,23879,23887,23893,23899,23909,23911,23917,23929,23957,23971,23977,23981,23993,24001,24007,24019,24023,24029,24043,24049,24061,24071,24077,24083,24091,24097,24103,24107,24109,24113,24121,24133,24137,24151,24169,24179,24181,24197,24203,24223,24229,24239,24247,24251,24281,24317,24329,24337,24359,24371,24373,24379,24391,24407,24413,24419,24421,24439,24443,24469,24473,24481,24499,24509,24517,24527,24533,24547,24551,24571,24593,24611,24623,24631,24659,24671,24677,24683,24691,24697,24709,24733,24749,24763,24767,24781,24793,24799,24809,24821,24841,24847,24851,24859,24877,24889,24907,24917,24919,24923,24943,24953,24967,24971,24977,24979,24989,25013,25031,25033,25037,25057,25073,25087,25097,25111,25117,25121,25127,25147,25153,25163,25169,25171,25183,25189,25219,25229,25237,25243,25247,25253,25261,25301,25303,25307,25309,25321,25339,25343,25349,25357,25367,25373,25391,25409,25411,25423,25439,25447,25453,25457,25463,25469,25471,25523,25537,25541,25561,25577,25579,25583,25589,25601,25603,25609,25621,25633,25639,25643,25657,25667,25673,25679,25693,25703,25717,25733,25741,25747,25759,25763,25771,25793,25799,25801,25819,25841,25847,25849,25867,25873,25889,25903,25913,25919,25931,25933,25939,25943,25951,25969,25981,25997,25999,26003,26017,26021,26029,26041,26053,26083,26099,26107,26111,26113,26119,26141,26153,26161,26171,26177,26183,26189,26203,26209,26227,26237,26249,26251,26261,26263,26267,26293,26297,26309,26317,26321,26339,26347,26357,26371,26387,26393,26399,26407,26417,26423,26431,26437,26449,26459,26479,26489,26497,26501,26513,26539,26557,26561,26573,26591,26597,26627,26633,26641,26647,26669,26681,26683,26687,26693,26699,26701,26711,26713,26717,26723,26729,26731,26737,26759,26777,26783,26801,26813,26821,26833,26839,26849,26861,26863,26879,26881,26891,26893,26903,26921,26927,26947,26951,26953,26959,26981,26987,26993,27011,27017,27031,27043,27059,27061,27067,27073,27077,27091,27103,27107,27109,27127,27143,27179,27191,27197,27211,27239,27241,27253,27259,27271,27277,27281,27283,27299,27329,27337,27361,27367,27397,27407,27409,27427,27431,27437,27449,27457,27479,27481,27487,27509,27527,27529,27539,27541,27551,27581,27583,27611,27617,27631,27647,27653,27673,27689,27691,27697,27701,27733,27737,27739,27743,27749,27751,27763,27767,27773,27779,27791,27793,27799,27803,27809,27817,27823,27827,27847,27851,27883,27893,27901,27917,27919,27941,27943,27947,27953,27961,27967,27983,27997,28001,28019,28027,28031,28051,28057,28069,28081,28087,28097,28099,28109,28111,28123,28151,28163,28181,28183,28201,28211,28219,28229,28277,28279,28283,28289,28297,28307,28309,28319,28349,28351,28387,28393,28403,28409,28411,28429,28433,28439,28447,28463,28477,28493,28499,28513,28517,28537,28541,28547,28549,28559,28571,28573,28579,28591,28597,28603,28607,28619,28621,28627,28631,28643,28649,28657,28661,28663,28669,28687,28697,28703,28711,28723,28729,28751,28753,28759,28771,28789,28793,28807,28813,28817,28837,28843,28859,28867,28871,28879,28901,28909,28921,28927,28933,28949,28961,28979,29009,29017,29021,29023,29027,29033,29059,29063,29077,29101,29123,29129,29131,29137,29147,29153,29167,29173,29179,29191,29201,29207,29209,29221,29231,29243,29251,29269,29287,29297,29303,29311,29327,29333,29339,29347,29363,29383,29387,29389,29399,29401,29411,29423,29429,29437,29443,29453,29473,29483,29501,29527,29531,29537,29567,29569,29573,29581,29587,29599,29611,29629,29633,29641,29663,29669,29671,29683,29717,29723,29741,29753,29759,29761,29789,29803,29819,29833,29837,29851,29863,29867,29873,29879,29881,29917,29921,29927,29947,29959,29983,29989,30011,30013,30029,30047,30059,30071,30089,30091,30097,30103,30109,30113,30119,30133,30137,30139,30161,30169,30181,30187,30197,30203,30211,30223,30241,30253,30259,30269,30271,30293,30307,30313,30319,30323,30341,30347,30367,30389,30391,30403,30427,30431,30449,30467,30469,30491,30493,30497,30509,30517,30529,30539,30553,30557,30559,30577,30593,30631,30637,30643,30649,30661,30671,30677,30689,30697,30703,30707,30713,30727,30757,30763,30773,30781,30803,30809,30817,30829,30839,30841,30851,30853,30859,30869,30871,30881,30893,30911,30931,30937,30941,30949,30971,30977,30983,31013,31019,31033,31039,31051,31063,31069,31079,31081,31091,31121,31123,31139,31147,31151,31153,31159,31177,31181,31183,31189,31193,31219,31223,31231,31237,31247,31249,31253,31259,31267,31271,31277,31307,31319,31321,31327,31333,31337,31357,31379,31387,31391,31393,31397,31469,31477,31481,31489,31511,31513,31517,31531,31541,31543,31547,31567,31573,31583,31601,31607,31627,31643,31649,31657,31663,31667,31687,31699,31721,31723,31727,31729,31741,31751,31769,31771,31793,31799,31817,31847,31849,31859,31873,31883,31891,31907,31957,31963,31973,31981,31991,32003,32009,32027,32029,32051,32057,32059,32063,32069,32077,32083,32089,32099,32117,32119,32141,32143,32159,32173,32183,32189,32191,32203,32213,32233,32237,32251,32257,32261,32297,32299,32303,32309,32321,32323,32327,32341,32353,32359,32363,32369,32371,32377,32381,32401,32411,32413,32423,32429,32441,32443,32467,32479,32491,32497,32503,32507,32531,32533,32537,32561,32563,32569,32573,32579,32587,32603,32609,32611,32621,32633,32647,32653,32687,32693,32707,32713,32717,32719,32749,32771,32779,32783,32789,32797,32801,32803,32831,32833,32839,32843,32869,32887,32909,32911,32917,32933,32939,32941,32957,32969,32971,32983,32987,32993,32999,33013,33023,33029,33037,33049,33053,33071,33073,33083,33091,33107,33113,33119,33149,33151,33161,33179,33181,33191,33199,33203,33211,33223,33247,33287,33289,33301,33311,33317,33329,33331,33343,33347,33349,33353,33359,33377,33391,33403,33409,33413,33427,33457,33461,33469,33479,33487,33493,33503,33521,33529,33533,33547,33563,33569,33577,33581,33587,33589,33599,33601,33613,33617,33619,33623,33629,33637,33641,33647,33679,33703,33713,33721,33739,33749,33751,33757,33767,33769,33773,33791,33797,33809,33811,33827,33829,33851,33857,33863,33871,33889,33893,33911,33923,33931,33937,33941,33961,33967,33997,34019,34031,34033,34039,34057,34061,34123,34127,34129,34141,34147,34157,34159,34171,34183,34211,34213,34217,34231,34253,34259,34261,34267,34273,34283,34297,34301,34303,34313,34319,34327,34337,34351,34361,34367,34369,34381,34403,34421,34429,34439,34457,34469,34471,34483,34487,34499,34501,34511,34513,34519,34537,34543,34549,34583,34589,34591,34603,34607,34613,34631,34649,34651,34667,34673,34679,34687,34693,34703,34721,34729,34739,34747,34757,34759,34763,34781,34807,34819,34841,34843,34847,34849,34871,34877,34883,34897,34913,34919,34939,34949,34961,34963,34981,35023,35027,35051,35053,35059,35069,35081,35083,35089,35099,35107,35111,35117,35129,35141,35149,35153,35159,35171,35201,35221,35227,35251,35257,35267,35279,35281,35291,35311,35317,35323,35327,35339,35353,35363,35381,35393,35401,35407,35419,35423,35437,35447,35449,35461,35491,35507,35509,35521,35527,35531,35533,35537,35543,35569,35573,35591,35593,35597,35603,35617,35671,35677,35729,35731,35747,35753,35759,35771,35797,35801,35803,35809,35831,35837,35839,35851,35863,35869,35879,35897,35899,35911,35923,35933,35951,35963,35969,35977,35983,35993,35999,36007,36011,36013,36017,36037,36061,36067,36073,36083,36097,36107,36109,36131,36137,36151,36161,36187,36191,36209,36217,36229,36241,36251,36263,36269,36277,36293,36299,36307,36313,36319,36341,36343,36353,36373,36383,36389,36433,36451,36457,36467,36469,36473,36479,36493,36497,36523,36527,36529,36541,36551,36559,36563,36571,36583,36587,36599,36607,36629,36637,36643,36653,36671,36677,36683,36691,36697,36709,36713,36721,36739,36749,36761,36767,36779,36781,36787,36791,36793,36809,36821,36833,36847,36857,36871,36877,36887,36899,36901,36913,36919,36923,36929,36931,36943,36947,36973,36979,36997,37003,37013,37019,37021,37039,37049,37057,37061,37087,37097,37117,37123,37139,37159,37171,37181,37189,37199,37201,37217,37223,37243,37253,37273,37277,37307,37309,37313,37321,37337,37339,37357,37361,37363,37369,37379,37397,37409,37423,37441,37447,37463,37483,37489,37493,37501,37507,37511,37517,37529,37537,37547,37549,37561,37567,37571,37573,37579,37589,37591,37607,37619,37633,37643,37649,37657,37663,37691,37693,37699,37717,37747,37781,37783,37799,37811,37813,37831,37847,37853,37861,37871,37879,37889,37897,37907,37951,37957,37963,37967,37987,37991,37993,37997,38011,38039,38047,38053,38069,38083,38113,38119,38149,38153,38167,38177,38183,38189,38197,38201,38219,38231,38237,38239,38261,38273,38281,38287,38299,38303,38317,38321,38327,38329,38333,38351,38371,38377,38393,38431,38447,38449,38453,38459,38461,38501,38543,38557,38561,38567,38569,38593,38603,38609,38611,38629,38639,38651,38653,38669,38671,38677,38693,38699,38707,38711,38713,38723,38729,38737,38747,38749,38767,38783,38791,38803,38821,38833,38839,38851,38861,38867,38873,38891,38903,38917,38921,38923,38933,38953,38959,38971,38977,38993,39019,39023,39041,39043,39047,39079,39089,39097,39103,39107,39113,39119,39133,39139,39157,39161,39163,39181,39191,39199,39209,39217,39227,39229,39233,39239,39241,39251,39293,39301,39313,39317,39323,39341,39343,39359,39367,39371,39373,39383,39397,39409,39419,39439,39443,39451,39461,39499,39503,39509,39511,39521,39541,39551,39563,39569,39581,39607,39619,39623,39631,39659,39667,39671,39679,39703,39709,39719,39727,39733,39749,39761,39769,39779,39791,39799,39821,39827,39829,39839,39841,39847,39857,39863,39869,39877,39883,39887,39901,39929,39937,39953,39971,39979,39983,39989,40009,40013,40031,40037,40039,40063,40087,40093,40099,40111,40123,40127,40129,40151,40153,40163,40169,40177,40189,40193,40213,40231,40237,40241,40253,40277,40283,40289,40343,40351,40357,40361,40387,40423,40427,40429,40433,40459,40471,40483,40487,40493,40499,40507,40519,40529,40531,40543,40559,40577,40583,40591,40597,40609,40627,40637,40639,40693,40697,40699,40709,40739,40751,40759,40763,40771,40787,40801,40813,40819,40823,40829,40841,40847,40849,40853,40867,40879,40883,40897,40903,40927,40933,40939,40949,40961,40973,40993,41011,41017,41023,41039,41047,41051,41057,41077,41081,41113,41117,41131,41141,41143,41149,41161,41177,41179,41183,41189,41201,41203,41213,41221,41227,41231,41233,41243,41257,41263,41269,41281,41299,41333,41341,41351,41357,41381,41387,41389,41399,41411,41413,41443,41453,41467,41479,41491,41507,41513,41519,41521,41539,41543,41549,41579,41593,41597,41603,41609,41611,41617,41621,41627,41641,41647,41651,41659,41669,41681,41687,41719,41729,41737,41759,41761,41771,41777,41801,41809,41813,41843,41849,41851,41863,41879,41887,41893,41897,41903,41911,41927,41941,41947,41953,41957,41959,41969,41981,41983,41999,42013,42017,42019,42023,42043,42061,42071,42073,42083,42089,42101,42131,42139,42157,42169,42179,42181,42187,42193,42197,42209,42221,42223,42227,42239,42257,42281,42283,42293,42299,42307,42323,42331,42337,42349,42359,42373,42379,42391,42397,42403,42407,42409,42433,42437,42443,42451,42457,42461,42463,42467,42473,42487,42491,42499,42509,42533,42557,42569,42571,42577,42589,42611,42641,42643,42649,42667,42677,42683,42689,42697,42701,42703,42709,42719,42727,42737,42743,42751,42767,42773,42787,42793,42797,42821,42829,42839,42841,42853,42859,42863,42899,42901,42923,42929,42937,42943,42953,42961,42967,42979,42989,43003,43013,43019,43037,43049,43051,43063,43067,43093,43103,43117,43133,43151,43159,43177,43189,43201,43207,43223,43237,43261,43271,43283,43291,43313,43319,43321,43331,43391,43397,43399,43403,43411,43427,43441,43451,43457,43481,43487,43499,43517,43541,43543,43573,43577,43579,43591,43597,43607,43609,43613,43627,43633,43649,43651,43661,43669,43691,43711,43717,43721,43753,43759,43777,43781,43783,43787,43789,43793,43801,43853,43867,43889,43891,43913,43933,43943,43951,43961,43963,43969,43973,43987,43991,43997,44017,44021,44027,44029,44041,44053,44059,44071,44087,44089,44101,44111,44119,44123,44129,44131,44159,44171,44179,44189,44201,44203,44207,44221,44249,44257,44263,44267,44269,44273,44279,44281,44293,44351,44357,44371,44381,44383,44389,44417,44449,44453,44483,44491,44497,44501,44507,44519,44531,44533,44537,44543,44549,44563,44579,44587,44617,44621,44623,44633,44641,44647,44651,44657,44683,44687,44699,44701,44711,44729,44741,44753,44771,44773,44777,44789,44797,44809,44819,44839,44843,44851,44867,44879,44887,44893,44909,44917,44927,44939,44953,44959,44963,44971,44983,44987,45007,45013,45053,45061,45077,45083,45119,45121,45127,45131,45137,45139,45161,45179,45181,45191,45197,45233,45247,45259,45263,45281,45289,45293,45307,45317,45319,45329,45337,45341,45343,45361,45377,45389,45403,45413,45427,45433,45439,45481,45491,45497,45503,45523,45533,45541,45553,45557,45569,45587,45589,45599,45613,45631,45641,45659,45667,45673,45677,45691,45697,45707,45737,45751,45757,45763,45767,45779,45817,45821,45823,45827,45833,45841,45853,45863,45869,45887,45893,45943,45949,45953,45959,45971,45979,45989,46021,46027,46049,46051,46061,46073,46091,46093,46099,46103,46133,46141,46147,46153,46171,46181,46183,46187,46199,46219,46229,46237,46261,46271,46273,46279,46301,46307,46309,46327,46337,46349,46351,46381,46399,46411,46439,46441,46447,46451,46457,46471,46477,46489,46499,46507,46511,46523,46549,46559,46567,46573,46589,46591,46601,46619,46633,46639,46643,46649,46663,46679,46681,46687,46691,46703,46723,46727,46747,46751,46757,46769,46771,46807,46811,46817,46819,46829,46831,46853,46861,46867,46877,46889,46901,46919,46933,46957,46993,46997,47017,47041,47051,47057,47059,47087,47093,47111,47119,47123,47129,47137,47143,47147,47149,47161,47189,47207,47221,47237,47251,47269,47279,47287,47293,47297,47303,47309,47317,47339,47351,47353,47363,47381,47387,47389,47407,47417,47419,47431,47441,47459,47491,47497,47501,47507,47513,47521,47527,47533,47543,47563,47569,47581,47591,47599,47609,47623,47629,47639,47653,47657,47659,47681,47699,47701,47711,47713,47717,47737,47741,47743,47777,47779,47791,47797,47807,47809,47819,47837,47843,47857,47869,47881,47903,47911,47917,47933,47939,47947,47951,47963,47969,47977,47981,48017,48023,48029,48049,48073,48079,48091,48109,48119,48121,48131,48157,48163,48179,48187,48193,48197,48221,48239,48247,48259,48271,48281,48299,48311,48313,48337,48341,48353,48371,48383,48397,48407,48409,48413,48437,48449,48463,48473,48479,48481,48487,48491,48497,48523,48527,48533,48539,48541,48563,48571,48589,48593,48611,48619,48623,48647,48649,48661,48673,48677,48679,48731,48733,48751,48757,48761,48767,48779,48781,48787,48799,48809,48817,48821,48823,48847,48857,48859,48869,48871,48883,48889,48907,48947,48953,48973,48989,48991,49003,49009,49019,49031,49033,49037,49043,49057,49069,49081,49103,49109,49117,49121,49123,49139,49157,49169,49171,49177,49193,49199,49201,49207,49211,49223,49253,49261,49277,49279,49297,49307,49331,49333,49339,49363,49367,49369,49391,49393,49409,49411,49417,49429,49433,49451,49459,49463,49477,49481,49499,49523,49529,49531,49537,49547,49549,49559,49597,49603,49613,49627,49633,49639,49663,49667,49669,49681,49697,49711,49727,49739,49741,49747,49757,49783,49787,49789,49801,49807,49811,49823,49831,49843,49853,49871,49877,49891,49919,49921,49927,49937,49939,49943,49957,49991,49993,49999,50021,50023,50033,50047,50051,50053,50069,50077,50087,50093,50101,50111,50119,50123,50129,50131,50147,50153,50159,50177,50207,50221,50227,50231,50261,50263,50273,50287,50291,50311,50321,50329,50333,50341,50359,50363,50377,50383,50387,50411,50417,50423,50441,50459,50461,50497,50503,50513,50527,50539,50543,50549,50551,50581,50587,50591,50593,50599,50627,50647,50651,50671,50683,50707,50723,50741,50753,50767,50773,50777,50789,50821,50833,50839,50849,50857,50867,50873,50891,50893,50909,50923,50929,50951,50957,50969,50971,50989,50993,51001,51031,51043,51047,51059,51061,51071,51109,51131,51133,51137,51151,51157,51169,51193,51197,51199,51203,51217,51229,51239,51241,51257,51263,51283,51287,51307,51329,51341,51343,51347,51349,51361,51383,51407,51413,51419,51421,51427,51431,51437,51439,51449,51461,51473,51479,51481,51487,51503,51511,51517,51521,51539,51551,51563,51577,51581,51593,51599,51607,51613,51631,51637,51647,51659,51673,51679,51683,51691,51713,51719,51721,51749,51767,51769,51787,51797,51803,51817,51827,51829,51839,51853,51859,51869,51871,51893,51899,51907,51913,51929,51941,51949,51971,51973,51977,51991,52009,52021,52027,52051,52057,52067,52069,52081,52103,52121,52127,52147,52153,52163,52177,52181,52183,52189,52201,52223,52237,52249,52253,52259,52267,52289,52291,52301,52313,52321,52361,52363,52369,52379,52387,52391,52433,52453,52457,52489,52501,52511,52517,52529,52541,52543,52553,52561,52567,52571,52579,52583,52609,52627,52631,52639,52667,52673,52691,52697,52709,52711,52721,52727,52733,52747,52757,52769,52783,52807,52813,52817,52837,52859,52861,52879,52883,52889,52901,52903,52919,52937,52951,52957,52963,52967,52973,52981,52999,53003,53017,53047,53051,53069,53077,53087,53089,53093,53101,53113,53117,53129,53147,53149,53161,53171,53173,53189,53197,53201,53231,53233,53239,53267,53269,53279,53281,53299,53309,53323,53327,53353,53359,53377,53381,53401,53407,53411,53419,53437,53441,53453,53479,53503,53507,53527,53549,53551,53569,53591,53593,53597,53609,53611,53617,53623,53629,53633,53639,53653,53657,53681,53693,53699,53717,53719,53731,53759,53773,53777,53783,53791,53813,53819,53831,53849,53857,53861,53881,53887,53891,53897,53899,53917,53923,53927,53939,53951,53959,53987,53993,54001,54011,54013,54037,54049,54059,54083,54091,54101,54121,54133,54139,54151,54163,54167,54181,54193,54217,54251,54269,54277,54287,54293,54311,54319,54323,54331,54347,54361,54367,54371,54377,54401,54403,54409,54413,54419,54421,54437,54443,54449,54469,54493,54497,54499,54503,54517,54521,54539,54541,54547,54559,54563,54577,54581,54583,54601,54617,54623,54629,54631,54647,54667,54673,54679,54709,54713,54721,54727,54751,54767,54773,54779,54787,54799,54829,54833,54851,54869,54877,54881,54907,54917,54919,54941,54949,54959,54973,54979,54983,55001,55009,55021,55049,55051,55057,55061,55073,55079,55103,55109,55117,55127,55147,55163,55171,55201,55207,55213,55217,55219,55229,55243,55249,55259,55291,55313,55331,55333,55337,55339,55343,55351,55373,55381,55399,55411,55439,55441,55457,55469,55487,55501,55511,55529,55541,55547,55579,55589,55603,55609,55619,55621,55631,55633,55639,55661,55663,55667,55673,55681,55691,55697,55711,55717,55721,55733,55763,55787,55793,55799,55807,55813,55817,55819,55823,55829,55837,55843,55849,55871,55889,55897,55901,55903,55921,55927,55931,55933,55949,55967,55987,55997,56003,56009,56039,56041,56053,56081,56087,56093,56099,56101,56113,56123,56131,56149,56167,56171,56179,56197,56207,56209,56237,56239,56249,56263,56267,56269,56299,56311,56333,56359,56369,56377,56383,56393,56401,56417,56431,56437,56443,56453,56467,56473,56477,56479,56489,56501,56503,56509,56519,56527,56531,56533,56543,56569,56591,56597,56599,56611,56629,56633,56659,56663,56671,56681,56687,56701,56711,56713,56731,56737,56747,56767,56773,56779,56783,56807,56809,56813,56821,56827,56843,56857,56873,56891,56893,56897,56909,56911,56921,56923,56929,56941,56951,56957,56963,56983,56989,56993,56999,57037,57041,57047,57059,57073,57077,57089,57097,57107,57119,57131,57139,57143,57149,57163,57173,57179,57191,57193,57203,57221,57223,57241,57251,57259,57269,57271,57283,57287,57301,57329,57331,57347,57349,57367,57373,57383,57389,57397,57413,57427,57457,57467,57487,57493,57503,57527,57529,57557,57559,57571,57587,57593,57601,57637,57641,57649,57653,57667,57679,57689,57697,57709,57713,57719,57727,57731,57737,57751,57773,57781,57787,57791,57793,57803,57809,57829,57839,57847,57853,57859,57881,57899,57901,57917,57923,57943,57947,57973,57977,57991,58013,58027,58031,58043,58049,58057,58061,58067,58073,58099,58109,58111,58129,58147,58151,58153,58169,58171,58189,58193,58199,58207,58211,58217,58229,58231,58237,58243,58271,58309,58313,58321,58337,58363,58367,58369,58379,58391,58393,58403,58411,58417,58427,58439,58441,58451,58453,58477,58481,58511,58537,58543,58549,58567,58573,58579,58601,58603,58613,58631,58657,58661,58679,58687,58693,58699,58711,58727,58733,58741,58757,58763,58771,58787,58789,58831,58889,58897,58901,58907,58909,58913,58921,58937,58943,58963,58967,58979,58991,58997,59009,59011,59021,59023,59029,59051,59053,59063,59069,59077,59083,59093,59107,59113,59119,59123,59141,59149,59159,59167,59183,59197,59207,59209,59219,59221,59233,59239,59243,59263,59273,59281,59333,59341,59351,59357,59359,59369,59377,59387,59393,59399,59407,59417,59419,59441,59443,59447,59453,59467,59471,59473,59497,59509,59513,59539,59557,59561,59567,59581,59611,59617,59621,59627,59629,59651,59659,59663,59669,59671,59693,59699,59707,59723,59729,59743,59747,59753,59771,59779,59791,59797,59809,59833,59863,59879,59887,59921,59929,59951,59957,59971,59981,59999,60013,60017,60029,60037,60041,60077,60083,60089,60091,60101,60103,60107,60127,60133,60139,60149,60161,60167,60169,60209,60217,60223,60251,60257,60259,60271,60289,60293,60317,60331,60337,60343,60353,60373,60383,60397,60413,60427,60443,60449,60457,60493,60497,60509,60521,60527,60539,60589,60601,60607,60611,60617,60623,60631,60637,60647,60649,60659,60661,60679,60689,60703,60719,60727,60733,60737,60757,60761,60763,60773,60779,60793,60811,60821,60859,60869,60887,60889,60899,60901,60913,60917,60919,60923,60937,60943,60953,60961,61001,61007,61027,61031,61043,61051,61057,61091,61099,61121,61129,61141,61151,61153,61169,61211,61223,61231,61253,61261,61283,61291,61297,61331,61333,61339,61343,61357,61363,61379,61381,61403,61409,61417,61441,61463,61469,61471,61483,61487,61493,61507,61511,61519,61543,61547,61553,61559,61561,61583,61603,61609,61613,61627,61631,61637,61643,61651,61657,61667,61673,61681,61687,61703,61717,61723,61729,61751,61757,61781,61813,61819,61837,61843,61861,61871,61879,61909,61927,61933,61949,61961,61967,61979,61981,61987,61991,62003,62011,62017,62039,62047,62053,62057,62071,62081,62099,62119,62129,62131,62137,62141,62143,62171,62189,62191,62201,62207,62213,62219,62233,62273,62297,62299,62303,62311,62323,62327,62347,62351,62383,62401,62417,62423,62459,62467,62473,62477,62483,62497,62501,62507,62533,62539,62549,62563,62581,62591,62597,62603,62617,62627,62633,62639,62653,62659,62683,62687,62701,62723,62731,62743,62753,62761,62773,62791,62801,62819,62827,62851,62861,62869,62873,62897,62903,62921,62927,62929,62939,62969,62971,62981,62983,62987,62989,63029,63031,63059,63067,63073,63079,63097,63103,63113,63127,63131,63149,63179,63197,63199,63211,63241,63247,63277,63281,63299,63311,63313,63317,63331,63337,63347,63353,63361,63367,63377,63389,63391,63397,63409,63419,63421,63439,63443,63463,63467,63473,63487,63493,63499,63521,63527,63533,63541,63559,63577,63587,63589,63599,63601,63607,63611,63617,63629,63647,63649,63659,63667,63671,63689,63691,63697,63703,63709,63719,63727,63737,63743,63761,63773,63781,63793,63799,63803,63809,63823,63839,63841,63853,63857,63863,63901,63907,63913,63929,63949,63977,63997,64007,64013,64019,64033,64037,64063,64067,64081,64091,64109,64123,64151,64153,64157,64171,64187,64189,64217,64223,64231,64237,64271,64279,64283,64301,64303,64319,64327,64333,64373,64381,64399,64403,64433,64439,64451,64453,64483,64489,64499,64513,64553,64567,64577,64579,64591,64601,64609,64613,64621,64627,64633,64661,64663,64667,64679,64693,64709,64717,64747,64763,64781,64783,64793,64811,64817,64849,64853,64871,64877,64879,64891,64901,64919,64921,64927,64937,64951,64969,64997,65003,65011,65027,65029,65033,65053,65063,65071,65089,65099,65101,65111,65119,65123,65129,65141,65147,65167,65171,65173,65179,65183,65203,65213,65239,65257,65267,65269,65287,65293,65309,65323,65327,65353,65357,65371,65381,65393,65407,65413,65419,65423,65437,65447,65449,65479,65497,65519,65521]
Math/NumberTheory/Primes/Testing/Certificates.hs view
@@ -1,11 +1,12 @@ -- | -- Module: Math.NumberTheory.Primes.Testing.Certificates+-- Description: Deprecated -- Copyright: (c) 2011 Daniel Fischer -- Licence: MIT -- Maintainer: Daniel Fischer <daniel.is.fischer@googlemail.com> -- -- Certificates for primality or compositeness.-module Math.NumberTheory.Primes.Testing.Certificates+module Math.NumberTheory.Primes.Testing.Certificates {-# DEPRECATED "This module will be removed in the next release" #-} ( -- * Certificates Certificate(..) , argueCertificate
Math/NumberTheory/Primes/Testing/Certificates/Internal.hs view
@@ -33,13 +33,13 @@ import GHC.Integer.GMP.Internals import Math.NumberTheory.Moduli.Class-import Math.NumberTheory.Utils+import Math.NumberTheory.Powers.Squares+import Math.NumberTheory.Primes (unPrime) import Math.NumberTheory.Primes.Factorisation.TrialDivision import Math.NumberTheory.Primes.Factorisation.Montgomery import Math.NumberTheory.Primes.Testing.Probabilistic-import Math.NumberTheory.Primes.Sieve.Eratosthenes-import Math.NumberTheory.Primes.Types (unPrime)-import Math.NumberTheory.Powers.Squares+import Math.NumberTheory.Primes.Sieve.Eratosthenes (primeList, primeSieve)+import Math.NumberTheory.Utils -- | A certificate of either compositeness or primality of an -- 'Integer'. Only numbers @> 1@ can be certified, trying to@@ -248,7 +248,7 @@ ((p,_):_) | p < n -> Composite (Factors n p (n `quot` p)) | otherwise -> Prime (TrialDivision n r2) _ -> error "Impossible"- | otherwise = case smallFactors 100000 n of+ | otherwise = case smallFactors n of ([], Just _) | not (isStrongFermatPP n 2) -> Composite (StrongFermat n 2) | not (lucasTest n) -> Composite (LucasSelfridge n) | otherwise -> Prime (certifyBPSW n) -- if it isn't we error and ask for a report.
Math/NumberTheory/Quadratic/EisensteinIntegers.hs view
@@ -29,16 +29,15 @@ import Control.DeepSeq import Data.Coerce import Data.List (mapAccumL, partition)-import Data.Maybe (fromMaybe)+import Data.Maybe import Data.Ord (comparing)+import qualified Data.Semiring as S import GHC.Generics (Generic) import qualified Math.NumberTheory.Euclidean as ED import Math.NumberTheory.Moduli.Sqrt-import qualified Math.NumberTheory.Primes.Sieve as Sieve-import qualified Math.NumberTheory.Primes.Testing as Testing import Math.NumberTheory.Primes.Types-import qualified Math.NumberTheory.Primes as U+import qualified Math.NumberTheory.Primes as U import Math.NumberTheory.Utils (mergeBy) import Math.NumberTheory.Utils.FromIntegral @@ -75,6 +74,16 @@ fromInteger n = n :+ 0 signum = snd . absSignum +instance S.Semiring EisensteinInteger where+ plus = (+)+ times = (*)+ zero = 0 :+ 0+ one = 1 :+ 0+ fromNatural n = fromIntegral n :+ 0++instance S.Ring EisensteinInteger where+ negate = negate+ -- | Returns an @EisensteinInteger@'s sign, and its associate in the first -- sextant. absSignum :: EisensteinInteger -> (EisensteinInteger, EisensteinInteger)@@ -96,25 +105,26 @@ associates :: EisensteinInteger -> [EisensteinInteger] associates e = map (e *) ids +instance ED.GcdDomain EisensteinInteger+ instance ED.Euclidean EisensteinInteger where- quotRem = divHelper quot- divMod = divHelper div+ degree = fromInteger . norm+ quotRem = divHelper -- | Function that does most of the underlying work for @divMod@ and -- @quotRem@, apart from choosing the specific integer division algorithm. -- This is instead done by the calling function (either @divMod@ which uses -- @div@, or @quotRem@, which uses @quot@.) divHelper- :: (Integer -> Integer -> Integer)- -> EisensteinInteger+ :: EisensteinInteger -> EisensteinInteger -> (EisensteinInteger, EisensteinInteger)-divHelper divide g h =- let nr :+ ni = g * conjugate h+divHelper g h = (q, r)+ where+ nr :+ ni = g * conjugate h denom = norm h- q = divide nr denom :+ divide ni denom- p = h * q- in (q, g - p)+ q = ((nr + signum nr * denom `quot` 2) `quot` denom) :+ ((ni + signum ni * denom `quot` 2) `quot` denom)+ r = g - h * q -- | Conjugate a Eisenstein integer. conjugate :: EisensteinInteger -> EisensteinInteger@@ -133,8 +143,8 @@ -- Special case, @1 - ω@ is the only Eisenstein prime with norm @3@, -- and @abs (1 - ω) = 2 + ω@. | a' == 2 && b' == 1 = True- | b' == 0 && a' `mod` 3 == 2 = Testing.isPrime a'- | nE `mod` 3 == 1 = Testing.isPrime nE+ | b' == 0 && a' `mod` 3 == 2 = isJust $ U.isPrime a'+ | nE `mod` 3 == 1 = isJust $ U.isPrime nE | otherwise = False where nE = norm e a' :+ b' = abs e@@ -179,7 +189,7 @@ findPrime :: Prime Integer -> U.Prime EisensteinInteger findPrime p = case sqrtsModPrime (9*k*k - 1) p of [] -> error "findPrime: argument must be prime p = 6k + 1"- z : _ -> Prime $ ED.gcd (unPrime p :+ 0) ((z - 3 * k) :+ 1)+ z : _ -> Prime $ abs $ ED.gcd (unPrime p :+ 0) ((z - 3 * k) :+ 1) where k :: Integer k = unPrime p `div` 6@@ -194,7 +204,7 @@ primes = coerce $ (2 :+ 1) : mergeBy (comparing norm) l r where leftPrimes, rightPrimes :: [Prime Integer]- (leftPrimes, rightPrimes) = partition (\p -> unPrime p `mod` 3 == 2) Sieve.primes+ (leftPrimes, rightPrimes) = partition (\p -> unPrime p `mod` 3 == 2) [U.nextPrime 2 ..] rightPrimes' = filter (\prime -> unPrime prime `mod` 3 == 1) $ tail rightPrimes l = [unPrime p :+ 0 | p <- leftPrimes] r = [g | p <- rightPrimes', let x :+ y = unPrime (findPrime p), g <- [x :+ y, x :+ (x - y)]]@@ -255,7 +265,7 @@ | unPrime p `mod` 3 == 2 = let e' = e `quot` 2 in (z `quotI` (unPrime p ^ e'), [(Prime (unPrime p :+ 0), e')]) - -- The @`mod` 3 == 0@ case need not be verified because the+ -- The @`rem` 3 == 0@ case need not be verified because the -- only Eisenstein primes whose norm are a multiple of 3 -- are @1 - ω@ and its associates, which have already been -- removed by the above @go z (3, e)@ pattern match.
Math/NumberTheory/Quadratic/GaussianIntegers.hs view
@@ -24,18 +24,16 @@ import Control.DeepSeq (NFData) import Data.Coerce import Data.List (mapAccumL, partition)-import Data.Maybe (fromMaybe)+import Data.Maybe import Data.Ord (comparing)+import qualified Data.Semiring as S import GHC.Generics - import qualified Math.NumberTheory.Euclidean as ED import Math.NumberTheory.Moduli.Sqrt import Math.NumberTheory.Powers (integerSquareRoot) import Math.NumberTheory.Primes.Types-import qualified Math.NumberTheory.Primes.Sieve as Sieve-import qualified Math.NumberTheory.Primes.Testing as Testing-import qualified Math.NumberTheory.Primes as U+import qualified Math.NumberTheory.Primes as U import Math.NumberTheory.Utils (mergeBy) import Math.NumberTheory.Utils.FromIntegral @@ -70,6 +68,16 @@ fromInteger n = n :+ 0 signum = snd . absSignum +instance S.Semiring GaussianInteger where+ plus = (+)+ times = (*)+ zero = 0 :+ 0+ one = 1 :+ 0+ fromNatural n = fromIntegral n :+ 0++instance S.Ring GaussianInteger where+ negate = negate+ absSignum :: GaussianInteger -> (GaussianInteger, GaussianInteger) absSignum z@(a :+ b) | a == 0 && b == 0 = (z, 0) -- origin@@ -78,21 +86,22 @@ | a < 0 && b <= 0 = ((-a) :+ (-b), -1) -- third quadrant: (-inf, 0) x (-inf, 0]i | otherwise = ((-b) :+ a, -ι) -- fourth quadrant: [0, inf) x (-inf, 0)i +instance ED.GcdDomain GaussianInteger+ instance ED.Euclidean GaussianInteger where- quotRem = divHelper quot- divMod = divHelper div+ degree = fromInteger . norm+ quotRem = divHelper divHelper- :: (Integer -> Integer -> Integer)- -> GaussianInteger+ :: GaussianInteger -> GaussianInteger -> (GaussianInteger, GaussianInteger)-divHelper divide g h =- let nr :+ ni = g * conjugate h+divHelper g h = (q, r)+ where+ nr :+ ni = g * conjugate h denom = norm h- q = divide nr denom :+ divide ni denom- p = h * q- in (q, g - p)+ q = ((nr + signum nr * denom `quot` 2) `quot` denom) :+ ((ni + signum ni * denom `quot` 2) `quot` denom)+ r = g - h * q -- |Conjugate a Gaussian integer. conjugate :: GaussianInteger -> GaussianInteger@@ -105,9 +114,9 @@ -- |Compute whether a given Gaussian integer is prime. isPrime :: GaussianInteger -> Bool isPrime g@(x :+ y)- | x == 0 && y /= 0 = abs y `mod` 4 == 3 && Testing.isPrime y- | y == 0 && x /= 0 = abs x `mod` 4 == 3 && Testing.isPrime x- | otherwise = Testing.isPrime $ norm g+ | x == 0 && y /= 0 = abs y `mod` 4 == 3 && isJust (U.isPrime y)+ | y == 0 && x /= 0 = abs x `mod` 4 == 3 && isJust (U.isPrime x)+ | otherwise = isJust $ U.isPrime $ norm g -- |An infinite list of the Gaussian primes. Uses primes in Z to exhaustively -- generate all Gaussian primes (up to associates), in order of ascending@@ -116,7 +125,7 @@ primes = coerce $ (1 :+ 1) : mergeBy (comparing norm) l r where leftPrimes, rightPrimes :: [Prime Integer]- (leftPrimes, rightPrimes) = partition (\p -> unPrime p `mod` 4 == 3) (tail Sieve.primes)+ (leftPrimes, rightPrimes) = partition (\p -> unPrime p `mod` 4 == 3) [U.nextPrime 3 ..] l = [unPrime p :+ 0 | p <- leftPrimes] r = [g | p <- rightPrimes, let Prime (x :+ y) = findPrime p, g <- [x :+ y, y :+ x]]
− Math/NumberTheory/Recurrencies.hs
@@ -1,17 +0,0 @@--- |--- Module: Math.NumberTheory.Recurrencies--- Description: Deprecated--- Copyright: (c) 2018 Alexandre Rodrigues Baldé--- Licence: MIT--- Maintainer: Alexandre Rodrigues Baldé <alexandrer_b@outlook.com>-----module Math.NumberTheory.Recurrencies {-# DEPRECATED "Use `Math.NumberTheory.Recurrences` instead." #-}- ( module Math.NumberTheory.Recurrences.Linear- , module Math.NumberTheory.Recurrences.Bilinear- , module Math.NumberTheory.Recurrences.Pentagonal- ) where--import Math.NumberTheory.Recurrences.Bilinear-import Math.NumberTheory.Recurrences.Linear-import Math.NumberTheory.Recurrences.Pentagonal (partition)
− Math/NumberTheory/Recurrencies/Bilinear.hs
@@ -1,38 +0,0 @@--- |--- Module: Math.NumberTheory.Recurrencies.Bilinear--- Description: Deprecated--- Copyright: (c) 2016 Andrew Lelechenko--- Licence: MIT--- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com>------ Bilinear recurrent sequences and Bernoulli numbers,--- roughly covering Ch. 5-6 of /Concrete Mathematics/--- by R. L. Graham, D. E. Knuth and O. Patashnik.------ #memory# __Note on memory leaks and memoization.__--- Top-level definitions in this module are polymorphic, so the results of computations are not retained in memory.--- Make them monomorphic to take advantages of memoization. Compare------ >>> :set +s--- >>> binomial !! 1000 !! 1000 :: Integer--- 1--- (0.01 secs, 1,385,512 bytes)--- >>> binomial !! 1000 !! 1000 :: Integer--- 1--- (0.01 secs, 1,381,616 bytes)------ against------ >>> let binomial' = binomial :: [[Integer]]--- >>> binomial' !! 1000 !! 1000 :: Integer--- 1--- (0.01 secs, 1,381,696 bytes)--- >>> binomial' !! 1000 !! 1000 :: Integer--- 1--- (0.01 secs, 391,152 bytes)--module Math.NumberTheory.Recurrencies.Bilinear {-# DEPRECATED "Use `Math.NumberTheory.Recurrences.Bilinear` instead." #-}- ( module Math.NumberTheory.Recurrences.Bilinear- ) where--import Math.NumberTheory.Recurrences.Bilinear
− Math/NumberTheory/Recurrencies/Linear.hs
@@ -1,14 +0,0 @@--- |--- Module: Math.NumberTheory.Recurrencies.Linear--- Description: Deprecated--- Copyright: (c) 2011 Daniel Fischer--- Licence: MIT--- Maintainer: Daniel Fischer <daniel.is.fischer@googlemail.com>------ Efficient calculation of linear recurrent sequences, including Fibonacci and Lucas sequences.--module Math.NumberTheory.Recurrencies.Linear {-# DEPRECATED "Use `Math.NumberTheory.Recurrences.Linear` instead." #-}- ( module Math.NumberTheory.Recurrences.Linear- ) where--import Math.NumberTheory.Recurrences.Linear
Math/NumberTheory/SmoothNumbers.hs view
@@ -10,7 +10,9 @@ -- over a set {3, 4}, and 24 is not. -- +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-} module Math.NumberTheory.SmoothNumbers ( -- * Create a smooth basis@@ -29,12 +31,14 @@ ) where import Prelude hiding (div, mod, gcd)+import Data.Bits (Bits) import Data.Coerce import Data.List (nub)+import Data.Semiring (isZero) import qualified Data.Set as S+ import qualified Math.NumberTheory.Euclidean as E-import Math.NumberTheory.Primes (unPrime)-import Math.NumberTheory.Primes.Sieve (primes)+import Math.NumberTheory.Primes -- | An abstract representation of a smooth basis. -- It consists of a set of numbers ≥2.@@ -49,8 +53,9 @@ -- Just (SmoothBasis {unSmoothBasis = [2,4]}) -- >>> fromSet (Set.fromList [1, 3]) -- should be >= 2 -- Nothing-fromSet :: E.Euclidean a => S.Set a -> Maybe (SmoothBasis a)+fromSet :: (Eq a, E.GcdDomain a) => S.Set a -> Maybe (SmoothBasis a) fromSet s = if isValid l then Just (SmoothBasis l) else Nothing where l = S.elems s+{-# DEPRECATED fromSet "Use 'fromList' instead " #-} -- | Build a 'SmoothBasis' from a list of numbers ≥2. --@@ -62,7 +67,7 @@ -- Just (SmoothBasis {unSmoothBasis = [2,4]}) -- >>> fromList [1, 3] -- should be >= 2 -- Nothing-fromList :: E.Euclidean a => [a] -> Maybe (SmoothBasis a)+fromList :: (Eq a, E.GcdDomain a) => [a] -> Maybe (SmoothBasis a) fromList l = if isValid l' then Just (SmoothBasis l') else Nothing where l' = nub l@@ -73,10 +78,14 @@ -- Just (SmoothBasis {unSmoothBasis = [2,3,5,7]}) -- >>> fromSmoothUpperBound 1 -- Nothing-fromSmoothUpperBound :: Integral a => a -> Maybe (SmoothBasis a)-fromSmoothUpperBound n = if (n < 2)- then Nothing- else Just $ SmoothBasis $ takeWhile (<= n) $ map unPrime primes+fromSmoothUpperBound+ :: (Integral a, Enum (Prime a), Bits a, UniqueFactorisation a)+ => a+ -> Maybe (SmoothBasis a)+fromSmoothUpperBound n+ | n < 2 = Nothing+ | otherwise = Just $ SmoothBasis $ map unPrime [nextPrime 2 .. precPrime n]+{-# DEPRECATED fromSmoothUpperBound "Use 'fromList' with an appropriate list of primes instead " #-} -- | Helper used by @smoothOver@ (@Integral@ constraint) and @smoothOver'@ -- (@Euclidean@ constraint) Since the typeclass constraint is just@@ -85,10 +94,14 @@ -- This function relies on the fact that for any element of a smooth basis @p@ -- and any @a@ it is true that @norm (a * p) > norm a@. -- This condition is not checked.-smoothOver' :: forall a b . (Eq a, Num a, Ord b) => (a -> b) -> SmoothBasis a -> [a]+smoothOver'+ :: forall a b. (Eq a, Num a, Ord b)+ => (a -> b)+ -> SmoothBasis a+ -> [a] smoothOver' norm pl = foldr- (\p l -> mergeListLists $ iterate (map $ abs . (p*)) l)+ (\p l -> mergeListLists $ iterate (map (* p)) l) [1] (nub $ unSmoothBasis pl) where@@ -104,9 +117,9 @@ go2 a [] = a go2 [] b = b go2 a@(ah:at) b@(bh:bt)- | norm bh < norm ah = bh : (go2 a bt)- | ah == bh = ah : (go2 at bt)- | otherwise = ah : (go2 at b)+ | norm bh < norm ah = bh : (go2 a bt)+ | abs ah == abs bh = ah : (go2 at bt)+ | otherwise = ah : (go2 at b) -- | Generate an infinite ascending list of -- <https://en.wikipedia.org/wiki/Smooth_number smooth numbers>@@ -115,7 +128,7 @@ -- >>> import Data.Maybe -- >>> take 10 (smoothOver (fromJust (fromList [2, 5]))) -- [1,2,4,5,8,10,16,20,25,32]-smoothOver :: Integral a => SmoothBasis a -> [a]+smoothOver :: (Ord a, Num a) => SmoothBasis a -> [a] smoothOver = smoothOver' abs -- | Generate an ascending list of@@ -129,12 +142,12 @@ -- >>> import Data.Maybe -- >>> smoothOverInRange (fromJust (fromList [2, 5])) 100 200 -- [100,125,128,160,200]-smoothOverInRange :: forall a. Integral a => SmoothBasis a -> a -> a -> [a]+smoothOverInRange :: (Ord a, Num a) => SmoothBasis a -> a -> a -> [a] smoothOverInRange s lo hi = takeWhile (<= hi) $ dropWhile (< lo)- $ coerce- $ smoothOver (coerce s :: SmoothBasis (E.WrappedIntegral a))+ $ smoothOver s+{-# DEPRECATED smoothOverInRange "Use 'smoothOver' instead" #-} -- | Generate an ascending list of -- <https://en.wikipedia.org/wiki/Smooth_number smooth numbers>@@ -150,7 +163,7 @@ -- >>> smoothOverInRangeBF (fromJust (fromList [2, 5])) 100 200 -- [100,125,128,160,200] smoothOverInRangeBF- :: forall a. (Enum a, E.Euclidean a)+ :: (Eq a, Enum a, E.GcdDomain a) => SmoothBasis a -> a -> a@@ -159,23 +172,19 @@ = coerce $ filter (isSmooth prs) $ coerce [lo..hi]+{-# DEPRECATED smoothOverInRangeBF "Use filtering by 'isSmooth' instead" #-} --- | isValid assumes that the list is sorted and unique and then checks if the list is suitable to be a SmoothBasis.-isValid :: (Eq a, Num a) => [a] -> Bool-isValid pl = length pl /= 0 && v' pl- where- v' :: (Eq a, Num a) => [a] -> Bool- v' [] = True- v' (x:xs) = x /= 0 && abs x /= 1 && abs x == x && v' xs+isValid :: (Eq a, E.GcdDomain a) => [a] -> Bool+isValid [] = False+isValid xs = all (\x -> not (isZero x) && not (E.isUnit x)) xs -- | @isSmooth@ checks if a given number is smooth under a certain @SmoothBasis@. -- Does not check if the @SmoothBasis@ is valid.-isSmooth :: forall a . E.Euclidean a => SmoothBasis a -> a -> Bool-isSmooth prs x = mf (unSmoothBasis prs) x+isSmooth :: (Eq a, E.GcdDomain a) => SmoothBasis a -> a -> Bool+isSmooth prs x = not (isZero x) && go (unSmoothBasis prs) x where- mf :: [a] -> a -> Bool- mf _ 0 = False- mf [] n = abs n == 1 -- mf means manually factor- mf pl@(p:ps) n = if E.mod n p == 0- then mf pl (E.div n p)- else mf ps n+ go :: (Eq a, E.GcdDomain a) => [a] -> a -> Bool+ go [] n = E.isUnit n+ go pps@(p:ps) n = case n `E.divide` p of+ Nothing -> go ps n+ Just q -> go pps q || go ps n
− Math/NumberTheory/UniqueFactorisation.hs
@@ -1,13 +0,0 @@--- |--- Module: Math.NumberTheory.Recurrencies--- Description: Deprecated--- Copyright: (c) 2019 Andrew Lelechenko--- Licence: MIT--- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com>-----module Math.NumberTheory.UniqueFactorisation {-# DEPRECATED "Use `Math.NumberTheory.Primes` instead." #-}- ( module Math.NumberTheory.Primes- ) where--import Math.NumberTheory.Primes
Math/NumberTheory/Utils.hs view
@@ -39,6 +39,7 @@ import GHC.Natural import Data.Bits+import Data.Semiring (Semiring(..), isZero) import Math.NumberTheory.Euclidean uncheckedShiftR :: Word -> Int -> Word@@ -160,12 +161,13 @@ bitCountInt :: Int -> Int bitCountInt = popCount -splitOff :: Euclidean a => a -> a -> (Word, a)-splitOff _ 0 = (0, 0) -- prevent infinite loop-splitOff p n = go 0 n+splitOff :: (Eq a, GcdDomain a) => a -> a -> (Word, a)+splitOff p n+ | isZero n = (0, zero) -- prevent infinite loop+ | otherwise = go 0 n where- go !k m = case m `quotRem` p of- (q, 0) -> go (k + 1) q+ go !k m = case m `divide` p of+ Just q -> go (k + 1) q _ -> (k, m) {-# INLINABLE splitOff #-} @@ -194,7 +196,7 @@ -- | Work around https://ghc.haskell.org/trac/ghc/ticket/14085 recipMod :: Integer -> Integer -> Maybe Integer-recipMod x m = case recipModInteger (x `mod` m) m of+recipMod x m = case recipModInteger (x `P.mod` m) m of 0 -> Nothing y -> Just y
Math/NumberTheory/Utils/DirichletSeries.hs view
@@ -27,6 +27,7 @@ import Data.Coerce import Data.Map (Map) import qualified Data.Map.Strict as M+import Data.Maybe import Data.Semiring (Semiring(..)) import Numeric.Natural @@ -65,7 +66,7 @@ -- and all a_i and b_i are divisors of n. Return Dirichlet series cs, -- which contains all terms as * bs = sum_i m_i/c_i^s such that c_i divides n. timesAndCrop- :: (Euclidean a, Ord a, Semiring b)+ :: (Num a, Euclidean a, Ord a, Semiring b) => a -> DirichletSeries a b -> DirichletSeries a b@@ -78,7 +79,7 @@ | (b, fb) <- M.assocs bs , let nb = n `quot` b , (a, fa) <- takeWhile ((<= nb) . fst) (M.assocs as)- , nb `rem` a == 0+ , isJust (nb `divide` a) ] {-# SPECIALISE timesAndCrop :: Semiring b => Int -> DirichletSeries Int b -> DirichletSeries Int b -> DirichletSeries Int b #-} {-# SPECIALISE timesAndCrop :: Semiring b => Word -> DirichletSeries Word b -> DirichletSeries Word b -> DirichletSeries Word b #-}
Math/NumberTheory/Zeta.hs view
@@ -1,21 +1,24 @@ -- | -- Module: Math.NumberTheory.Zeta--- Copyright: (c) 2018 Andrew Lelechenko+-- Copyright: (c) 2018 Alexandre Rodrigues Baldé, Andrew Lelechenko -- Licence: MIT -- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com> ----- Interface to work with Riemann zeta-function and Dirichlet beta-function.+-- Numeric evaluation of various zeta-functions. {-# LANGUAGE ScopedTypeVariables #-} module Math.NumberTheory.Zeta- ( module Math.NumberTheory.Zeta.Dirichlet- , module Math.NumberTheory.Zeta.Hurwitz- , module Math.NumberTheory.Zeta.Riemann- , module Math.NumberTheory.Zeta.Utils+ ( -- * Riemann zeta-function+ zetas+ , zetasEven+ -- * Dirichlet beta-function+ , betas+ , betasOdd+ -- * Hurwitz zeta-functions+ , zetaHurwitz ) where import Math.NumberTheory.Zeta.Dirichlet import Math.NumberTheory.Zeta.Hurwitz import Math.NumberTheory.Zeta.Riemann-import Math.NumberTheory.Zeta.Utils
Math/NumberTheory/Zeta/Dirichlet.hs view
@@ -8,6 +8,8 @@ {-# LANGUAGE ScopedTypeVariables #-} +{-# OPTIONS_HADDOCK hide #-}+ module Math.NumberTheory.Zeta.Dirichlet ( betas , betasEven@@ -52,11 +54,6 @@ -- | Infinite sequence of approximate (up to given precision) -- values of Dirichlet beta-function at integer arguments, starting with @β(0)@.------ The algorithm previously used to compute @β@ for even arguments was derived--- from <https://arxiv.org/pdf/0910.5004.pdf An Euler-type formula for β(2n) and closed-form expressions for a class of zeta series>--- by F. M. S. Lima, formula (12), but is now based on the--- 'Math.NumberTheory.Zeta.Hurwitz.zetaHurwitz' recurrence. -- -- >>> take 5 (betas 1e-14) :: [Double] -- [0.5,0.7853981633974483,0.9159655941772189,0.9689461462593694,0.9889445517411051]
Math/NumberTheory/Zeta/Hurwitz.hs view
@@ -8,6 +8,8 @@ {-# LANGUAGE ScopedTypeVariables #-} +{-# OPTIONS_HADDOCK hide #-}+ module Math.NumberTheory.Zeta.Hurwitz ( zetaHurwitz ) where@@ -15,13 +17,11 @@ import Math.NumberTheory.Recurrences (bernoulli, factorial) import Math.NumberTheory.Zeta.Utils (skipEvens, skipOdds) --- | Values of Hurwitz zeta function evaluated at @ζ(s, a)@ with--- @forall t1 . (Floating t1, Ord t1) => a ∈ t1@, and @s ∈ [0, 1 ..]@.+-- | Values of Hurwitz zeta function evaluated at @ζ(s, a)@ for @s ∈ [0, 1 ..]@. -- -- The algorithm used was based on the Euler-Maclaurin formula and was derived -- from <http://fredrikj.net/thesis/thesis.pdf Fast and Rigorous Computation of Special Functions to High Precision> -- by F. Johansson, chapter 4.8, formula 4.8.5.--- -- The error for each value in this recurrence is given in formula 4.8.9 as an -- indefinite integral, and in formula 4.8.12 as a closed form formula. --@@ -29,8 +29,8 @@ -- the type chosen. -- -- For instance, when using @Double@s, it does not make sense--- to provide a number @ε >= 1e-53@ as the desired precision. For @Float@s,--- providing an @ε >= 1e-24@ also does not make sense.+-- to provide a number @ε < 1e-53@ as the desired precision. For @Float@s,+-- providing an @ε < 1e-24@ also does not make sense. -- Example of how to call the function: -- -- >>> zetaHurwitz 1e-15 0.25 !! 5
Math/NumberTheory/Zeta/Riemann.hs view
@@ -8,6 +8,8 @@ {-# LANGUAGE ScopedTypeVariables #-} +{-# OPTIONS_HADDOCK hide #-}+ module Math.NumberTheory.Zeta.Riemann ( zetas , zetasEven@@ -45,11 +47,6 @@ -- | Infinite sequence of approximate (up to given precision) -- values of Riemann zeta-function at integer arguments, starting with @ζ(0)@.------ Computations for odd arguments were formerly performed in accordance to--- <https://cr.yp.to/bib/2000/borwein.pdf Computational strategies for the Riemann zeta function>--- by J. M. Borwein, D. M. Bradley, R. E. Crandall, formula (57), but now use--- the 'Math.NumberTheory.Zeta.Hurwitz.zetaHurwitz' recurrence. -- -- >>> take 5 (zetas 1e-14) :: [Double] -- [-0.5,Infinity,1.6449340668482264,1.2020569031595942,1.0823232337111381]
arithmoi.cabal view
@@ -1,15 +1,15 @@ name: arithmoi-version: 0.9.0.0+version: 0.10.0.0 cabal-version: >=1.10 build-type: Simple license: MIT license-file: LICENSE-copyright: (c) 2011 Daniel Fischer, 2016-2018 Andrew Lelechenko, Carter Schonwald-maintainer: Carter Schonwald carter at wellposed dot com,- Andrew Lelechenko andrew dot lelechenko at gmail dot com+copyright: (c) 2016-2019 Andrew Lelechenko, Carter Schonwald, 2011 Daniel Fischer+maintainer: Andrew Lelechenko andrew dot lelechenko at gmail dot com,+ Carter Schonwald carter at wellposed dot com stability: Provisional-homepage: https://github.com/cartazio/arithmoi-bug-reports: https://github.com/cartazio/arithmoi/issues+homepage: https://github.com/Bodigrim/arithmoi+bug-reports: https://github.com/Bodigrim/arithmoi/issues synopsis: Efficient basic number-theoretic functions. description: A library of basic functionality needed for@@ -18,14 +18,14 @@ Primes and related things (totients, factorisation), powers (integer roots and tests, modular exponentiation). category: Math, Algorithms, Number Theory-author: Daniel Fischer+author: Andrew Lelechenko, Daniel Fischer tested-with: GHC ==8.0.2 GHC ==8.2.2 GHC ==8.4.4 GHC ==8.6.5 GHC ==8.8.1 extra-source-files: Changes source-repository head type: git- location: https://github.com/cartazio/arithmoi+ location: https://github.com/Bodigrim/arithmoi flag check-bounds description:@@ -38,6 +38,7 @@ base >=4.9 && <5, array >=0.5 && <0.6, containers >=0.5 && <0.7,+ constraints, deepseq, exact-pi >=0.5, ghc-prim <0.6,@@ -45,7 +46,7 @@ integer-logarithms >=1.0, random >=1.0 && <1.2, transformers >=0.4 && <0.6,- semirings >= 0.2,+ semirings >= 0.4.2, vector >= 0.12 exposed-modules: GHC.TypeNats.Compat@@ -65,6 +66,7 @@ Math.NumberTheory.Moduli.Equations Math.NumberTheory.Moduli.Jacobi Math.NumberTheory.Moduli.PrimitiveRoot+ Math.NumberTheory.Moduli.Singleton Math.NumberTheory.Moduli.Sqrt Math.NumberTheory.MoebiusInversion Math.NumberTheory.MoebiusInversion.Int@@ -81,27 +83,22 @@ Math.NumberTheory.Primes.Factorisation Math.NumberTheory.Primes.Factorisation.Certified Math.NumberTheory.Primes.Sieve+ Math.NumberTheory.Primes.Small Math.NumberTheory.Primes.Testing Math.NumberTheory.Primes.Testing.Certificates Math.NumberTheory.Quadratic.GaussianIntegers Math.NumberTheory.Quadratic.EisensteinIntegers Math.NumberTheory.Recurrences- Math.NumberTheory.Recurrencies Math.NumberTheory.Recurrences.Bilinear- Math.NumberTheory.Recurrencies.Bilinear Math.NumberTheory.Recurrences.Linear- Math.NumberTheory.Recurrencies.Linear Math.NumberTheory.SmoothNumbers- Math.NumberTheory.UniqueFactorisation Math.NumberTheory.Zeta Math.NumberTheory.Zeta.Dirichlet Math.NumberTheory.Zeta.Hurwitz Math.NumberTheory.Zeta.Riemann other-modules: Math.NumberTheory.ArithmeticFunctions.Class- Math.NumberTheory.ArithmeticFunctions.SieveBlock.Unboxed Math.NumberTheory.ArithmeticFunctions.Standard- Math.NumberTheory.Moduli.SqrtOld Math.NumberTheory.Primes.Counting.Approximate Math.NumberTheory.Primes.Counting.Impl Math.NumberTheory.Primes.Factorisation.Montgomery@@ -155,8 +152,8 @@ Math.NumberTheory.Moduli.EquationsTests Math.NumberTheory.Moduli.JacobiTests Math.NumberTheory.Moduli.PrimitiveRootTests+ Math.NumberTheory.Moduli.SingletonTests Math.NumberTheory.Moduli.SqrtTests- Math.NumberTheory.MoebiusInversion.IntTests Math.NumberTheory.MoebiusInversionTests Math.NumberTheory.Powers.CubesTests Math.NumberTheory.Powers.FourthTests@@ -191,6 +188,7 @@ base, arithmoi, array,+ constraints, containers, deepseq, gauge,
benchmark/Math/NumberTheory/DiscreteLogarithmBench.hs view
@@ -1,6 +1,7 @@ {-# LANGUAGE ExistentialQuantification #-}-{-# LANGUAGE RankNTypes #-}-{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-} {-# OPTIONS_GHC -fno-warn-type-defaults #-} @@ -11,6 +12,7 @@ ) where import Gauge.Main+import Control.Monad import Data.Maybe import GHC.TypeNats.Compat import Data.Proxy@@ -18,7 +20,8 @@ import Math.NumberTheory.Moduli.Class (isMultElement, KnownNat, MultMod, multElement, getVal,Mod) import Math.NumberTheory.Moduli.DiscreteLogarithm (discreteLogarithm)-import Math.NumberTheory.Moduli.PrimitiveRoot (PrimitiveRoot, isPrimitiveRoot, unPrimitiveRoot, cyclicGroupFromModulo)+import Math.NumberTheory.Moduli.PrimitiveRoot+import Math.NumberTheory.Moduli.Singleton data Case = forall m. KnownNat m => Case (PrimitiveRoot m) (MultMod m) String @@ -31,7 +34,7 @@ makeCase (a,b,n,s) = case someNatVal n of SomeNat (_ :: Proxy m) ->- Case <$> isPrimitiveRoot a' <*> isMultElement b' <*> pure s+ Case <$> join (isPrimitiveRoot @Integer <$> cyclicGroup <*> pure a') <*> isMultElement b' <*> pure s where a' = fromInteger a :: Mod m b' = fromInteger b @@ -45,15 +48,16 @@ rangeCases :: Natural -> Int -> [Case] rangeCases start num = take num $ do n <- [start..]- _cg <- maybeToList $ cyclicGroupFromModulo n case someNatVal n of- SomeNat (_ :: Proxy m) -> do- a <- take 1 $ mapMaybe isPrimitiveRoot [2 :: Mod m .. maxBound]- b <- take 1 $ filter (/= unPrimitiveRoot a) $ mapMaybe isMultElement [2 .. maxBound]- return $ Case a b (show n)+ SomeNat (_ :: Proxy m) -> case cyclicGroup :: Maybe (CyclicGroup Integer m) of+ Nothing -> []+ Just cg -> do+ a <- take 1 $ mapMaybe (isPrimitiveRoot cg) [2 :: Mod m .. maxBound]+ b <- take 1 $ filter (/= unPrimitiveRoot a) $ mapMaybe isMultElement [2 .. maxBound]+ return $ Case a b (show n) discreteLogarithm' :: Case -> Natural-discreteLogarithm' (Case a b _) = discreteLogarithm a b+discreteLogarithm' (Case a b _) = discreteLogarithm (fromJust cyclicGroup) a b benchSuite :: Benchmark benchSuite = bgroup "Discrete logarithm"
benchmark/Math/NumberTheory/InverseBench.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE TypeApplications #-} {-# OPTIONS_GHC -fno-warn-type-defaults #-}@@ -7,6 +8,7 @@ ) where import Gauge.Main+import Data.Bits (Bits) import Numeric.Natural import Math.NumberTheory.ArithmeticFunctions.Inverse@@ -22,7 +24,7 @@ countInverseTotient :: (Ord a, Euclidean a, UniqueFactorisation a) => a -> Word countInverseTotient = inverseTotient (const 1) -countInverseSigma :: (Integral a, Euclidean a, UniqueFactorisation a) => a -> Word+countInverseSigma :: (Integral a, Euclidean a, UniqueFactorisation a, Enum (Prime a), Bits a) => a -> Word countInverseSigma = inverseSigma (const 1) benchSuite :: Benchmark
benchmark/Math/NumberTheory/PrimesBench.hs view
@@ -8,7 +8,7 @@ import System.Random import Math.NumberTheory.Logarithms (integerLog2)-import Math.NumberTheory.Primes.Factorisation+import Math.NumberTheory.Primes (factorise) import Math.NumberTheory.Primes.Testing genInteger :: Int -> Int -> Integer
benchmark/Math/NumberTheory/PrimitiveRootsBench.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE RankNTypes #-}+ {-# OPTIONS_GHC -fno-warn-type-defaults #-} module Math.NumberTheory.PrimitiveRootsBench@@ -5,20 +7,29 @@ ) where import Gauge.Main+import Data.Constraint import Data.Maybe import Math.NumberTheory.Moduli.PrimitiveRoot+import Math.NumberTheory.Moduli.Singleton import Math.NumberTheory.Primes primRootWrap :: Integer -> Word -> Integer -> Bool-primRootWrap p k g = isPrimitiveRoot' (CGOddPrimePower p' k) g- where p' = fromJust $ isPrime p+primRootWrap p k g = case fromJust $ cyclicGroupFromFactors [(p', k)] of+ Some cg -> case proofFromCyclicGroup cg of+ Sub Dict -> isJust $ isPrimitiveRoot cg (fromInteger g)+ where+ p' = fromJust $ isPrime p primRootWrap2 :: Integer -> Word -> Integer -> Bool-primRootWrap2 p k g = isPrimitiveRoot' (CGDoubleOddPrimePower p' k) g- where p' = fromJust $ isPrime p+primRootWrap2 p k g = case fromJust $ cyclicGroupFromFactors [(two, 1), (p', k)] of+ Some cg -> case proofFromCyclicGroup cg of+ Sub Dict -> isJust $ isPrimitiveRoot cg (fromInteger g)+ where+ two = fromJust $ isPrime 2+ p' = fromJust $ isPrime p -cyclicWrap :: Integer -> Maybe (CyclicGroup Integer)+cyclicWrap :: Integer -> Maybe (Some (CyclicGroup Integer)) cyclicWrap = cyclicGroupFromModulo benchSuite :: Benchmark
benchmark/Math/NumberTheory/SequenceBench.hs view
@@ -1,4 +1,5 @@ {-# OPTIONS_GHC -fno-warn-type-defaults #-}+{-# OPTIONS_GHC -fno-warn-deprecations #-} module Math.NumberTheory.SequenceBench ( benchSuite@@ -11,8 +12,8 @@ import Data.Bits import Math.NumberTheory.Primes (Prime(..))-import Math.NumberTheory.Primes.Sieve as P-import Math.NumberTheory.Primes.Testing as P+import Math.NumberTheory.Primes.Sieve+import Math.NumberTheory.Primes.Testing filterIsPrime :: (Integer, Integer) -> Integer filterIsPrime (p, q) = sum $ takeWhile (<= q) $ dropWhile (< p) $ filter isPrime (map toPrim [toIdx p .. toIdx q])
benchmark/Math/NumberTheory/SmoothNumbersBench.hs view
@@ -4,19 +4,18 @@ ( benchSuite ) where -import Data.List (genericTake) import Data.Maybe import Gauge.Main -import Math.NumberTheory.Euclidean (Euclidean)+import Math.NumberTheory.Primes import Math.NumberTheory.SmoothNumbers -doBench :: (Euclidean a, Integral a) => a -> a-doBench lim = sum $ genericTake lim $ smoothOver $ fromJust $ fromSmoothUpperBound lim+doBench :: Int -> Int+doBench lim = sum $ take lim $ smoothOver $ fromJust $ fromList $ map unPrime [nextPrime 2 .. precPrime lim] benchSuite :: Benchmark benchSuite = bgroup "SmoothNumbers"- [ bench "100" $ nf doBench (100 :: Int)- , bench "1000" $ nf doBench (1000 :: Int)- , bench "10000" $ nf doBench (10000 :: Int)+ [ bench "100" $ nf doBench 100+ , bench "1000" $ nf doBench 1000+ , bench "10000" $ nf doBench 10000 ]
test-suite/Math/NumberTheory/ArithmeticFunctions/InverseTests.hs view
@@ -20,6 +20,7 @@ import Test.Tasty import Test.Tasty.HUnit +import Data.Bits (Bits) import qualified Data.Set as S import Math.NumberTheory.ArithmeticFunctions@@ -134,10 +135,10 @@ ------------------------------------------------------------------------------- -- Sigma -sigmaProperty1 :: forall a. (Euclidean a, UniqueFactorisation a, Integral a) => Positive a -> Bool+sigmaProperty1 :: forall a. (Euclidean a, UniqueFactorisation a, Integral a, Enum (Prime a), Bits a) => Positive a -> Bool sigmaProperty1 (Positive x) = x `S.member` asSetOfPreimages inverseSigma (sigma 1 x) -sigmaProperty2 :: (Euclidean a, UniqueFactorisation a, Integral a) => Positive a -> Bool+sigmaProperty2 :: (Euclidean a, UniqueFactorisation a, Integral a, Enum (Prime a), Bits a) => Positive a -> Bool sigmaProperty2 (Positive x) = all (== x) (S.map (sigma 1) (asSetOfPreimages inverseSigma x)) -- | http://oeis.org/A055486
test-suite/Math/NumberTheory/ArithmeticFunctions/SieveBlockTests.hs view
@@ -23,22 +23,15 @@ import Data.Semigroup #endif import qualified Data.Vector as V-import qualified Data.Vector.Unboxed as U import Math.NumberTheory.ArithmeticFunctions import Math.NumberTheory.ArithmeticFunctions.SieveBlock-import Math.NumberTheory.Primes (unPrime) pointwiseTest :: (Eq a, Show a) => ArithmeticFunction Word a -> Word -> Word -> IO () pointwiseTest f lowIndex len = assertEqual "pointwise" (runFunctionOverBlock f lowIndex len) (V.generate (fromIntegral len) (runFunction f . (+ lowIndex) . fromIntegral)) -unboxedTest :: (Eq a, U.Unbox a, Show a) => SieveBlockConfig a -> IO ()-unboxedTest config = assertEqual "unboxed"- (sieveBlock config 1 1000)- (U.convert $ sieveBlockUnboxed config 1 1000)- moebiusTest :: Word -> Word -> Bool moebiusTest m n = m == 0@@ -68,13 +61,6 @@ pairToTest :: Word -> Word -> TestTree pairToTest m n = testCase (show m ++ "," ++ show n) $ assertBool "should be equal" $ moebiusTest m n -multiplicativeConfig :: (Word -> Word -> Word) -> SieveBlockConfig Word-multiplicativeConfig f = SieveBlockConfig- { sbcEmpty = 1- , sbcAppend = (*)- , sbcFunctionOnPrimePower = f . unPrime- }- moebiusConfig :: SieveBlockConfig Moebius moebiusConfig = SieveBlockConfig { sbcEmpty = MoebiusP@@ -95,12 +81,6 @@ , testCase "smallOmega" $ pointwiseTest smallOmegaA 1 1000 , testCase "bigOmega" $ pointwiseTest bigOmegaA 1 1000 , testCase "carmichael" $ pointwiseTest carmichaelA 1 1000- ]- , testGroup "unboxed"- [ testCase "id" $ unboxedTest $ multiplicativeConfig (^)- , testCase "tau" $ unboxedTest $ multiplicativeConfig (\_ a -> succ a )- , testCase "moebius" $ unboxedTest moebiusConfig- , testCase "totient" $ unboxedTest $ multiplicativeConfig (\p a -> (p - 1) * p ^ (a - 1)) ] , testGroup "special moebius" moebiusSpecialCases ]
test-suite/Math/NumberTheory/ArithmeticFunctionsTests.hs view
@@ -46,7 +46,7 @@ -- | All divisors of n truly divides n. divisorsProperty3 :: NonZero Natural -> Bool-divisorsProperty3 (NonZero n) = all (\d -> n `mod` d == 0) (runFunction divisorsA n)+divisorsProperty3 (NonZero n) = all (\d -> n `rem` d == 0) (runFunction divisorsA n) -- | 'divisorsA' matches 'divisorsSmallA' divisorsProperty4 :: NonZero Int -> Bool@@ -129,10 +129,10 @@ -- | congruences 1,2,3,4 from https://en.wikipedia.org/wiki/Ramanujan_tau_function ramanujanCongruence1 :: NonZero Natural -> Bool ramanujanCongruence1 (NonZero n)- | k == 1 = (ramanujan n' - sigma 11 n') `mod` (2^11) == 0- | k == 3 = (ramanujan n' - 1217 * sigma 11 n') `mod` (2^13) == 0- | k == 5 = (ramanujan n' - 1537 * sigma 11 n') `mod` (2^12) == 0- | k == 7 = (ramanujan n' - 705 * sigma 11 n') `mod` (2^14) == 0+ | k == 1 = (ramanujan n' - sigma 11 n') `rem` (2^11) == 0+ | k == 3 = (ramanujan n' - 1217 * sigma 11 n') `rem` (2^13) == 0+ | k == 5 = (ramanujan n' - 1537 * sigma 11 n') `rem` (2^12) == 0+ | k == 7 = (ramanujan n' - 705 * sigma 11 n') `rem` (2^14) == 0 | otherwise = True where k = n `mod` 8 n' = fromIntegral n :: Integer@@ -140,8 +140,8 @@ -- | congruences 8,9 from https://en.wikipedia.org/wiki/Ramanujan_tau_function ramanujanCongruence2 :: NonZero Natural -> Bool ramanujanCongruence2 (NonZero n)- | (n `mod` 7) `elem` [0,1,2,4] = m `mod` 7 == 0- | otherwise = m `mod` 49 == 0+ | (n `mod` 7) `elem` [0,1,2,4] = m `rem` 7 == 0+ | otherwise = m `rem` 49 == 0 where m = ramanujan n' - n' * sigma 9 n' n' = fromIntegral n :: Integer @@ -228,7 +228,7 @@ -- | carmichaeil divides totient carmichaelProperty1 :: NonZero Natural -> Bool-carmichaelProperty1 (NonZero n) = runFunction totientA n `mod` runFunction carmichaelA n == 0+carmichaelProperty1 (NonZero n) = runFunction totientA n `rem` runFunction carmichaelA n == 0 -- | carmichael matches baseline from OEIS. carmichaelOeis :: Assertion
test-suite/Math/NumberTheory/EisensteinIntegersTests.hs view
@@ -17,11 +17,11 @@ import Test.Tasty (TestTree, testGroup) import Test.Tasty.HUnit (Assertion, assertEqual, testCase)+import Test.Tasty.QuickCheck as QC hiding (Positive(..)) import qualified Math.NumberTheory.Euclidean as ED import qualified Math.NumberTheory.Quadratic.EisensteinIntegers as E import Math.NumberTheory.Primes-import Math.NumberTheory.Primes.Sieve (primes) import Math.NumberTheory.TestUtils (Positive (..), testSmallAndQuick) @@ -43,22 +43,10 @@ inFirstSextant = x' > y' && y' >= 0 isAssociate = z' `elem` map (\e -> z * (1 E.:+ 1) ^ e) [0 .. 5] --- | Verify that @div@ and @mod@ are what `divMod` produces.-divModProperty1 :: E.EisensteinInteger -> E.EisensteinInteger -> Bool-divModProperty1 x y = y == 0 || (q == q' && r == r')- where- (q, r) = ED.divMod x y- q' = ED.div x y- r' = ED.mod x y---- | Verify that @divModE` produces the right quotient and remainder.-divModProperty2 :: E.EisensteinInteger -> E.EisensteinInteger -> Bool-divModProperty2 x y = (y == 0) || (x `ED.div` y) * y + (x `ED.mod` y) == x---- | Verify that @divModE@ produces a remainder smaller than the divisor with+-- | Verify that @rem@ produces a remainder smaller than the divisor with -- regards to the Euclidean domain's function.-modProperty1 :: E.EisensteinInteger -> E.EisensteinInteger -> Bool-modProperty1 x y = (y == 0) || (E.norm $ x `ED.mod` y) < (E.norm y)+remProperty1 :: E.EisensteinInteger -> E.EisensteinInteger -> Bool+remProperty1 x y = (y == 0) || (E.norm $ x `ED.rem` y) < (E.norm y) -- | Verify that @quot@ and @rem@ are what `quotRem` produces. quotRemProperty1 :: E.EisensteinInteger -> E.EisensteinInteger -> Bool@@ -76,7 +64,7 @@ gcdEProperty1 :: E.EisensteinInteger -> E.EisensteinInteger -> Bool gcdEProperty1 z1 z2 = z1 == 0 && z2 == 0- || z1 `ED.rem` z == 0 && z2 `ED.rem` z == 0 && z == abs z+ || z1 `ED.rem` z == 0 && z2 `ED.rem` z == 0 where z = ED.gcd z1 z2 @@ -91,7 +79,7 @@ -- | A special case that tests rounding/truncating in GCD. gcdESpecialCase1 :: Assertion-gcdESpecialCase1 = assertEqual "gcd" 1 $ ED.gcd (12 E.:+ 23) (23 E.:+ 34)+gcdESpecialCase1 = assertEqual "gcd" (1 E.:+ 1) $ ED.gcd (12 E.:+ 23) (23 E.:+ 34) findPrimesProperty1 :: Positive Int -> Bool findPrimesProperty1 (Positive index) =@@ -152,13 +140,10 @@ testSuite :: TestTree testSuite = testGroup "EisensteinIntegers" $ [ testSmallAndQuick "forall z . z == signum z * abs z" signumAbsProperty- , testSmallAndQuick "abs z always returns an @EisensteinInteger@ in the\- \ first sextant of the complex plane" absProperty+ , testSmallAndQuick "abs z rotates to the first sextant" absProperty , testGroup "Division"- [ testSmallAndQuick "divE and modE work properly" divModProperty1- , testSmallAndQuick "divModE works properly" divModProperty2- , testSmallAndQuick "The remainder's norm is smaller than the divisor's"- modProperty1+ [ testSmallAndQuick "The remainder's norm is smaller than the divisor's"+ remProperty1 , testSmallAndQuick "quotE and remE work properly" quotRemProperty1 , testSmallAndQuick "quotRemE works properly" quotRemProperty2@@ -167,24 +152,21 @@ , testGroup "g.c.d." [ testSmallAndQuick "The g.c.d. of two Eisenstein integers divides them" gcdEProperty1- , testSmallAndQuick "A common divisor of two Eisenstein integers always\- \ divides the g.c.d. of those two integers"+ -- smallcheck takes too long+ , QC.testProperty "Common divisor divides gcd" gcdEProperty2 , testCase "g.c.d. (12 :+ 23) (23 :+ 34)" gcdESpecialCase1 ] , testSmallAndQuick "The Eisenstein norm function is multiplicative" euclideanDomainProperty1 , testGroup "Primality"- [ testSmallAndQuick "Eisenstein primes found by the norm search used in\- \ findPrime are really prime"+ [ testSmallAndQuick "findPrime returns prime" findPrimesProperty1- , testSmallAndQuick "Eisenstein primes generated by `primes` are actually\- \ primes"+ , testSmallAndQuick "primes are actually prime" primesProperty1- , testSmallAndQuick "The infinite list of Eisenstein primes produced by\- \ `primes` is ordered. "+ , testSmallAndQuick "primes is ordered" primesProperty2- , testSmallAndQuick "All generated primes are in the first sextant"+ , testSmallAndQuick "primes are in the first sextant" primesProperty3 ]
test-suite/Math/NumberTheory/EuclideanTests.hs view
@@ -9,6 +9,7 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-} {-# OPTIONS_GHC -fno-warn-type-defaults #-} {-# OPTIONS_GHC -fno-warn-unused-imports #-}@@ -21,6 +22,7 @@ import Prelude hiding (gcd) import Test.Tasty import Test.Tasty.HUnit+import Test.Tasty.QuickCheck as QC hiding (Positive(..)) import Control.Arrow import Data.Bits@@ -31,10 +33,11 @@ import Math.NumberTheory.Euclidean import Math.NumberTheory.Euclidean.Coprimes+import Math.NumberTheory.Quadratic.GaussianIntegers import Math.NumberTheory.TestUtils -- | Check that 'extendedGCD' is consistent with documentation.-extendedGCDProperty :: forall a. (Bits a, Euclidean a, Ord a) => AnySign a -> AnySign a -> Bool+extendedGCDProperty :: forall a. (Bits a, Num a, GcdDomain a, Euclidean a, Ord a) => AnySign a -> AnySign a -> Bool extendedGCDProperty (AnySign a) (AnySign b) | isNatural a = True -- extendedGCD does not make sense for Natural | otherwise =@@ -50,43 +53,62 @@ isNatural a = isNothing (bitSizeMaybe a) && not (isSigned a) -- | Check that numbers are coprime iff their gcd equals to 1.-coprimeProperty :: (Euclidean a) => AnySign a -> AnySign a -> Bool+coprimeProperty :: (Eq a, Num a, GcdDomain a, Euclidean a) => AnySign a -> AnySign a -> Bool coprimeProperty (AnySign a) (AnySign b) = coprime a b == (gcd a b == 1) -splitIntoCoprimesProperty1 :: [(Positive Natural, Power Word)] -> Bool+splitIntoCoprimesProperty1+ :: (Eq a, Num a, GcdDomain a)+ => [(a, Power Word)]+ -> Bool splitIntoCoprimesProperty1 fs' = factorback fs == factorback (unCoprimes $ splitIntoCoprimes fs) where- fs = map (getPositive *** getPower) fs'- factorback = product . map (uncurry (^))+ fs = map (id *** getPower) fs'+ factorback = abs . product . map (uncurry (^)) -splitIntoCoprimesProperty2 :: [(Positive Natural, Power Word)] -> Bool+splitIntoCoprimesProperty2+ :: (Eq a, Num a, GcdDomain a)+ => [(NonZero a, Power Word)]+ -> Bool splitIntoCoprimesProperty2 fs' = multiplicities fs <= multiplicities (unCoprimes $ splitIntoCoprimes fs) where- fs = map (getPositive *** getPower) fs'- multiplicities = sum . map snd . filter ((/= 1) . fst)+ fs = map (getNonZero *** getPower) fs'+ multiplicities = sum . map snd . filter ((/= 1) . abs . fst) -splitIntoCoprimesProperty3 :: [(Positive Natural, Power Word)] -> Bool+splitIntoCoprimesProperty3+ :: (Eq a, Num a, GcdDomain a)+ => [(a, Power Word)]+ -> Bool splitIntoCoprimesProperty3 fs' = and [ coprime x y | (x : xs) <- tails fs, y <- xs ] where- fs = map fst $ unCoprimes $ splitIntoCoprimes $ map (getPositive *** getPower) fs'+ fs = map fst $ unCoprimes $ splitIntoCoprimes $ map (id *** getPower) fs' -- | Check that evaluation never freezes.-splitIntoCoprimesProperty4 :: [(Integer, Word)] -> Bool+splitIntoCoprimesProperty4+ :: (Eq a, Num a, GcdDomain a)+ => [(a, Word)]+ -> Bool splitIntoCoprimesProperty4 fs' = fs == fs where fs = splitIntoCoprimes fs' +splitIntoCoprimesProperty5+ :: (Eq a, Num a, GcdDomain a)+ => [(a, Word)]+ -> Bool+splitIntoCoprimesProperty5 =+ all ((/= 1) . abs . fst) . unCoprimes . splitIntoCoprimes+ -- | This is an undefined behaviour, but at least it should not -- throw exceptions or loop forever. splitIntoCoprimesSpecialCase1 :: Assertion splitIntoCoprimesSpecialCase1 =- assertBool "should not fail" $ splitIntoCoprimesProperty4 [(0, 0), (0, 0)]+ assertBool "should not fail" $ splitIntoCoprimesProperty4 @Integer [(0, 0), (0, 0)] -- | This is an undefined behaviour, but at least it should not -- throw exceptions or loop forever. splitIntoCoprimesSpecialCase2 :: Assertion splitIntoCoprimesSpecialCase2 =- assertBool "should not fail" $ splitIntoCoprimesProperty4 [(0, 1), (-2, 0)]+ assertBool "should not fail" $ splitIntoCoprimesProperty4 @Integer [(0, 1), (-2, 0)] toListReturnsCorrectValues :: Assertion toListReturnsCorrectValues = assertEqual@@ -117,32 +139,62 @@ expected = [(2,10), (5,2), (7,1)] in assertEqual "should be equal" expected actual -unionProperty1 :: [(Positive Natural, Power Word)] -> [(Positive Natural, Power Word)] -> Bool+unionProperty1+ :: (Ord a, GcdDomain a)+ => [(a, Power Word)]+ -> [(a, Power Word)]+ -> Bool unionProperty1 xs ys = sort (unCoprimes (splitIntoCoprimes (xs' <> ys'))) == sort (unCoprimes (splitIntoCoprimes xs' <> splitIntoCoprimes ys')) where- xs' = map (getPositive *** getPower) xs- ys' = map (getPositive *** getPower) ys+ xs' = map (id *** getPower) xs+ ys' = map (id *** getPower) ys testSuite :: TestTree testSuite = testGroup "Euclidean" [ testSameIntegralProperty "extendedGCD" extendedGCDProperty , testSameIntegralProperty "coprime" coprimeProperty , testGroup "splitIntoCoprimes"- [ testSmallAndQuick "preserves product of factors" splitIntoCoprimesProperty1- , testSmallAndQuick "number of factors is non-decreasing" splitIntoCoprimesProperty2- , testSmallAndQuick "output factors are coprime" splitIntoCoprimesProperty3-- , testCase "does not freeze 1" splitIntoCoprimesSpecialCase1- , testCase "does not freeze 2" splitIntoCoprimesSpecialCase2- , testSmallAndQuick "does not freeze random" splitIntoCoprimesProperty4+ [ testGroup "preserves product of factors"+ [ testSmallAndQuick "Natural" (splitIntoCoprimesProperty1 @Natural)+ , testSmallAndQuick "Integer" (splitIntoCoprimesProperty1 @Integer)+ , testSmallAndQuick "Gaussian" (splitIntoCoprimesProperty1 @GaussianInteger)+ ]+ , testGroup "number of factors is non-decreasing"+ [ testSmallAndQuick "Natural" (splitIntoCoprimesProperty2 @Natural)+ , testSmallAndQuick "Integer" (splitIntoCoprimesProperty2 @Integer)+ , testSmallAndQuick "Gaussian" (splitIntoCoprimesProperty2 @GaussianInteger)+ ]+ , testGroup "output factors are coprime"+ [ testSmallAndQuick "Natural" (splitIntoCoprimesProperty3 @Natural)+ , testSmallAndQuick "Integer" (splitIntoCoprimesProperty3 @Integer)+ , testSmallAndQuick "Gaussian" (splitIntoCoprimesProperty3 @GaussianInteger)+ ]+ , testGroup "does not freeze"+ [ testCase "case 1" splitIntoCoprimesSpecialCase1+ , testCase "case 2" splitIntoCoprimesSpecialCase2+ , testSmallAndQuick "Natural" (splitIntoCoprimesProperty4 @Natural)+ -- smallcheck for Integer and GaussianInteger takes too long+ , QC.testProperty "Integer" (splitIntoCoprimesProperty4 @Integer)+ , QC.testProperty "Gaussian" (splitIntoCoprimesProperty4 @GaussianInteger)+ ]+ , testGroup "output factors are non-unit"+ [ testSmallAndQuick "Natural" (splitIntoCoprimesProperty5 @Natural)+ -- smallcheck for Integer and GaussianInteger takes too long+ , QC.testProperty "Integer" (splitIntoCoprimesProperty5 @Integer)+ , QC.testProperty "Gaussian" (splitIntoCoprimesProperty5 @GaussianInteger)+ ] ] , testGroup "Coprimes" [ testCase "test equality" toListReturnsCorrectValues , testCase "test union" unionReturnsCorrectValues , testCase "test insert with coprime base" insertReturnsCorrectValuesWhenCoprimeBase , testCase "test insert with non-coprime base" insertReturnsCorrectValuesWhenNotCoprimeBase- , testSmallAndQuick "property union" unionProperty1+ , testGroup "property union"+ [ testSmallAndQuick "Natural" (unionProperty1 @Natural)+ -- smallcheck for Integer takes too long+ , QC.testProperty "Integer" (unionProperty1 @Integer)+ ] ] ]
test-suite/Math/NumberTheory/GaussianIntegersTests.hs view
@@ -18,6 +18,7 @@ import Data.Maybe (fromJust, mapMaybe) import Test.Tasty import Test.Tasty.HUnit+import Test.Tasty.QuickCheck as QC hiding (NonNegative(..), Positive(..)) import qualified Math.NumberTheory.Euclidean as ED import Math.NumberTheory.Quadratic.GaussianIntegers@@ -112,8 +113,8 @@ numberOfPrimes :: Assertion numberOfPrimes = assertEqual "counting primes: OEIS A091100"- [16,100,668,4928,38404,313752,2658344]- [4 * (length $ takeWhile ((<= 10^n) . norm . unPrime) primes) | n <- [1..7]]+ [16,100,668,4928,38404,313752]+ [4 * (length $ takeWhile ((<= 10^n) . norm . unPrime) primes) | n <- [1..6]] -- | signum and abs should satisfy: z == signum z * abs z signumAbsProperty :: GaussianInteger -> Bool@@ -128,10 +129,15 @@ inFirstQuadrant = x' > 0 && y' >= 0 -- first quadrant includes the positive real axis, but not the origin or the positive imaginary axis isAssociate = z' `elem` map (\e -> z * (0 :+ 1) ^ e) [0 .. 3] +-- | Verify that @rem@ produces a remainder smaller than the divisor with+-- regards to the Euclidean domain's function.+remProperty :: GaussianInteger -> GaussianInteger -> Bool+remProperty x y = (y == 0) || (norm $ x `ED.rem` y) < (norm y)+ gcdGProperty1 :: GaussianInteger -> GaussianInteger -> Bool gcdGProperty1 z1 z2 = z1 == 0 && z2 == 0- || z1 `ED.rem` z == 0 && z2 `ED.rem` z == 0 && z == abs z+ || z1 `ED.rem` z == 0 && z2 `ED.rem` z == 0 where z = ED.gcd z1 z2 @@ -145,8 +151,11 @@ -- | a special case that tests rounding/truncating in GCD. gcdGSpecialCase1 :: Assertion-gcdGSpecialCase1 = assertEqual "gcdG" 1 $ ED.gcd (12 :+ 23) (23 :+ 34)+gcdGSpecialCase1 = assertEqual "gcdG" (-1) $ ED.gcd (12 :+ 23) (23 :+ 34) +gcdGSpecialCase2 :: Assertion+gcdGSpecialCase2 = assertEqual "gcdG" (0 :+ (-1)) $ ED.gcd (0 :+ 3) (2 :+ 2)+ testSuite :: TestTree testSuite = testGroup "GaussianIntegers" $ [ testGroup "factorise" (@@ -167,9 +176,12 @@ , testCase "counting primes" numberOfPrimes , testSmallAndQuick "signumAbsProperty" signumAbsProperty , testSmallAndQuick "absProperty" absProperty+ , testSmallAndQuick "remProperty" remProperty , testGroup "gcd" [ testSmallAndQuick "is divisor" gcdGProperty1- , testSmallAndQuick "is greatest" gcdGProperty2+ -- smallcheck takes too long+ , QC.testProperty "is greatest" gcdGProperty2 , testCase "(12 :+ 23) (23 :+ 34)" gcdGSpecialCase1+ , testCase "(0 :+ 3) (2 :+ 2)" gcdGSpecialCase2 ] ]
test-suite/Math/NumberTheory/Moduli/ChineseTests.hs view
@@ -37,14 +37,17 @@ -- | Check that 'chineseRemainder' matches 'chineseRemainder2'. chineseRemainder2Property :: Integer -> Positive Integer -> Integer -> Positive Integer -> Bool-chineseRemainder2Property r1 (Positive m1) r2 (Positive m2) = gcd m1 m2 /= 1- || Just (chineseRemainder2 (r1, m1) (r2, m2)) == chineseRemainder [(r1, m1), (r2, m2)]+chineseRemainder2Property r1 (Positive m1) r2 (Positive m2)+ | gcd m1 m2 /= 1 = True+ | otherwise = case chineseRemainder [(r1, m1), (r2, m2)] of+ Nothing -> False+ Just ch -> (ch - chineseRemainder2 (r1, m1) (r2, m2)) `rem` (m1 * m2) == 0 chineseCoprimeProperty :: Integer -> Positive Integer -> Integer -> Positive Integer -> Bool chineseCoprimeProperty n1 (Positive m1) n2 (Positive m2) = case gcd m1 m2 of 1 -> case chineseCoprime (n1, m1) (n2, m2) of Nothing -> False- Just n -> n `mod` m1 == n1 `mod` m1 && n `mod` m2 == n2 `mod` m2+ Just n -> (n - n1) `rem` m1 == 0 && (n - n2) `rem` m2 == 0 _ -> case chineseCoprime (n1, m1) (n2, m2) of Nothing -> True Just{} -> False@@ -53,13 +56,13 @@ chineseProperty n1 (Positive m1) n2 (Positive m2) = if compatible then case chinese (n1, m1) (n2, m2) of Nothing -> False- Just n -> n `mod` m1 == n1 `mod` m1 && n `mod` m2 == n2 `mod` m2+ Just n -> (n - n1) `rem` m1 == 0 && (n - n2) `rem` m2 == 0 else case chineseCoprime (n1, m1) (n2, m2) of Nothing -> True Just{} -> False where g = gcd m1 m2- compatible = n1 `mod` g == n2 `mod` g+ compatible = (n1 - n2) `rem` g == 0 testSuite :: TestTree
test-suite/Math/NumberTheory/Moduli/ClassTests.hs view
@@ -34,7 +34,7 @@ -- | Check that 'invertMod' inverts numbers modulo. invertModProperty :: AnySign Integer -> Positive Integer -> Bool invertModProperty (AnySign k) (Positive m) = case invertMod k m of- Nothing -> k `mod` m == 0 || gcd k m > 1+ Nothing -> k `rem` m == 0 || gcd k m > 1 Just InfMod{} -> False Just (SomeMod inv) -> gcd k m == 1 && k * getVal inv `mod` m == 1
test-suite/Math/NumberTheory/Moduli/DiscreteLogarithmTests.hs view
@@ -13,10 +13,11 @@ import Data.Proxy import GHC.TypeNats.Compat +import Math.NumberTheory.ArithmeticFunctions (totient) import Math.NumberTheory.Moduli.Class-import Math.NumberTheory.Moduli.PrimitiveRoot import Math.NumberTheory.Moduli.DiscreteLogarithm-import Math.NumberTheory.ArithmeticFunctions (totient)+import Math.NumberTheory.Moduli.PrimitiveRoot+import Math.NumberTheory.Moduli.Singleton import Math.NumberTheory.TestUtils -- | Ensure 'discreteLogarithm' returns in the appropriate range.@@ -24,27 +25,30 @@ discreteLogRange (Positive m) a b = case someNatVal m of SomeNat (_ :: Proxy m) -> fromMaybe True $ do- a' <- isPrimitiveRoot (fromInteger a :: Mod m)+ cg <- cyclicGroup :: Maybe (CyclicGroup Integer m)+ a' <- isPrimitiveRoot cg (fromInteger a) b' <- isMultElement (fromInteger b)- return $ discreteLogarithm a' b' < totient m+ return $ discreteLogarithm cg a' b' < totient m -- | Check that 'discreteLogarithm' inverts exponentiation. discreteLogarithmProperty :: Positive Natural -> Integer -> Integer -> Bool discreteLogarithmProperty (Positive m) a b = case someNatVal m of SomeNat (_ :: Proxy m) -> fromMaybe True $ do- a' <- isPrimitiveRoot (fromInteger a :: Mod m)+ cg <- cyclicGroup :: Maybe (CyclicGroup Integer m)+ a' <- isPrimitiveRoot cg (fromInteger a) b' <- isMultElement (fromInteger b)- return $ discreteLogarithm a' b' `stimes` unPrimitiveRoot a' == b'+ return $ discreteLogarithm cg a' b' `stimes` unPrimitiveRoot a' == b' -- | Check that 'discreteLogarithm' inverts exponentiation in the other direction. discreteLogarithmProperty' :: Positive Natural -> Integer -> Natural -> Bool discreteLogarithmProperty' (Positive m) a k = case someNatVal m of SomeNat (_ :: Proxy m) -> fromMaybe True $ do- a'' <- isPrimitiveRoot (fromInteger a :: Mod m)+ cg <- cyclicGroup :: Maybe (CyclicGroup Integer m)+ a'' <- isPrimitiveRoot cg (fromInteger a) let a' = unPrimitiveRoot a''- return $ discreteLogarithm a'' (k `stimes` a') == k `mod` totient m+ return $ discreteLogarithm cg a'' (k `stimes` a') == k `mod` totient m testSuite :: TestTree testSuite = testGroup "Discrete logarithm"
test-suite/Math/NumberTheory/Moduli/EquationsTests.hs view
@@ -20,6 +20,7 @@ import Math.NumberTheory.Moduli.Class import Math.NumberTheory.Moduli.Equations+import Math.NumberTheory.Moduli.Singleton import Math.NumberTheory.TestUtils solveLinearProp :: KnownNat m => Mod m -> Mod m -> Bool@@ -31,7 +32,7 @@ SomeNat (_ :: Proxy t) -> solveLinearProp (fromInteger a :: Mod t) (fromInteger b) solveQuadraticProp :: KnownNat m => Mod m -> Mod m -> Mod m -> Bool-solveQuadraticProp a b c = sort (solveQuadratic a b c) ==+solveQuadraticProp a b c = sort (solveQuadratic sfactors a b c) == filter (\x -> a * x * x + b * x + c == 0) [minBound .. maxBound] solveQuadraticProperty1 :: Positive Natural -> Integer -> Integer -> Integer -> Bool
test-suite/Math/NumberTheory/Moduli/PrimitiveRootTests.hs view
@@ -18,28 +18,27 @@ import Prelude hiding (gcd) import Test.Tasty+import Test.Tasty.HUnit import qualified Data.Set as S import Data.List (genericTake, genericLength) import Data.Maybe (isJust, isNothing, mapMaybe)-import Control.Arrow (first) import Numeric.Natural import Data.Proxy import GHC.TypeNats.Compat import Math.NumberTheory.ArithmeticFunctions (totient) import Math.NumberTheory.Euclidean-import Math.NumberTheory.Euclidean.Coprimes-import Math.NumberTheory.Moduli.Class (Mod, SomeMod(..), modulo)+import Math.NumberTheory.Moduli.Class import Math.NumberTheory.Moduli.PrimitiveRoot-import Math.NumberTheory.Prefactored (fromFactors, prefFactors, prefValue, Prefactored)+import Math.NumberTheory.Moduli.Singleton import Math.NumberTheory.Primes import Math.NumberTheory.TestUtils -cyclicGroupProperty1 :: (Euclidean a, Integral a, UniqueFactorisation a) => AnySign a -> Bool-cyclicGroupProperty1 (AnySign n) = case cyclicGroupFromModulo n of+cyclicGroupProperty1 :: (Euclidean a, Integral a, UniqueFactorisation a) => Positive a -> Bool+cyclicGroupProperty1 (Positive n) = case cyclicGroupFromModulo n of Nothing -> True- Just cg -> prefValue (cyclicGroupToModulo cg) == n+ Just (Some cg) -> factorBack (unSFactors (cyclicGroupToSFactors cg)) == n -- | Multiplicative groups modulo primes are always cyclic. cyclicGroupProperty2 :: (Integral a, UniqueFactorisation a) => Positive a -> Bool@@ -54,6 +53,9 @@ Just _ -> 2 * n < n {- overflow check -} || isJust (cyclicGroupFromModulo n) +cyclicGroupSpecialCase1 :: Assertion+cyclicGroupSpecialCase1 = assertBool "should be non-cyclic" $ isNothing $ cyclicGroupFromModulo (8 :: Integer)+ allUnique :: Ord a => [a] -> Bool allUnique = go S.empty where@@ -61,68 +63,64 @@ go acc (x : xs) = if x `S.member` acc then False else go (S.insert x acc) xs isPrimitiveRoot'Property1- :: (Euclidean a, Integral a, UniqueFactorisation a)- => AnySign a -> CyclicGroup a -> Bool-isPrimitiveRoot'Property1 (AnySign n) cg- = gcd (toInteger n) (prefValue (castPrefactored (cyclicGroupToModulo cg))) == 1- || not (isPrimitiveRoot' cg n)--castPrefactored :: Integral a => Prefactored a -> Prefactored Integer-castPrefactored = fromFactors . splitIntoCoprimes . map (first toInteger) . unCoprimes . prefFactors+ :: forall a. (Euclidean a, Integral a, UniqueFactorisation a)+ => AnySign a+ -> Positive Natural+ -> Bool+isPrimitiveRoot'Property1 (AnySign n) (Positive m) = case someNatVal m of+ SomeNat (_ :: Proxy m) -> case cyclicGroup :: Maybe (CyclicGroup a m) of+ Nothing -> True+ Just cg -> case isPrimitiveRoot cg (fromIntegral n) of+ Nothing -> True+ Just rt -> gcd (toInteger m) (getVal (multElement (unPrimitiveRoot rt))) == 1 isPrimitiveRootProperty1 :: AnySign Integer -> Positive Natural -> Bool-isPrimitiveRootProperty1 (AnySign n) (Positive m)- = case n `modulo` m of- SomeMod n' -> gcd n (toInteger m) == 1- || isNothing (isPrimitiveRoot n')- InfMod{} -> False+isPrimitiveRootProperty1 (AnySign n) (Positive m) = case someNatVal m of+ SomeNat (_ :: Proxy m) -> case cyclicGroup :: Maybe (CyclicGroup Integer m) of+ Nothing -> True+ Just cg -> gcd n (toInteger m) == 1+ || isNothing (isPrimitiveRoot cg (fromInteger n)) isPrimitiveRootProperty2 :: Positive Natural -> Bool-isPrimitiveRootProperty2 (Positive m)- = isNothing (cyclicGroupFromModulo m)- || case someNatVal m of- SomeNat (_ :: Proxy t) -> any (isJust . isPrimitiveRoot) [(minBound :: Mod t) .. maxBound]+isPrimitiveRootProperty2 (Positive m) = case someNatVal m of+ SomeNat (_ :: Proxy m) -> case cyclicGroup :: Maybe (CyclicGroup Integer m) of+ Nothing -> True+ Just cg -> any (isJust . isPrimitiveRoot cg) [minBound..maxBound] isPrimitiveRootProperty3 :: AnySign Integer -> Positive Natural -> Bool-isPrimitiveRootProperty3 (AnySign n) (Positive m)- = case n `modulo` m of- SomeMod n' -> isNothing (isPrimitiveRoot n')- || allUnique (genericTake (totient m - 1) (iterate (* n') 1))- InfMod{} -> False--isPrimitiveRootProperty4 :: AnySign Integer -> Positive Natural -> Bool-isPrimitiveRootProperty4 (AnySign n) (Positive m)- = isJust (cyclicGroupFromModulo m)- || case n `modulo` m of- SomeMod n' -> isNothing (isPrimitiveRoot n')- InfMod{} -> False+isPrimitiveRootProperty3 (AnySign n) (Positive m) = case someNatVal m of+ SomeNat (_ :: Proxy m) -> case cyclicGroup :: Maybe (CyclicGroup Integer m) of+ Nothing -> True+ Just cg -> let n' = fromInteger n+ in isNothing (isPrimitiveRoot cg n')+ || allUnique (genericTake (totient m - 1) (iterate (* n') 1)) isPrimitiveRootProperty5 :: Positive Natural -> Bool-isPrimitiveRootProperty5 (Positive m)- = isNothing (cyclicGroupFromModulo m)- || case someNatVal m of- SomeNat (_ :: Proxy t) -> genericLength (mapMaybe isPrimitiveRoot [(minBound :: Mod t) .. maxBound]) == totient (totient m)+isPrimitiveRootProperty5 (Positive m) = case someNatVal m of+ SomeNat (_ :: Proxy m) -> case cyclicGroup :: Maybe (CyclicGroup Integer m) of+ Nothing -> True+ Just cg -> genericLength (mapMaybe (isPrimitiveRoot cg) [minBound..maxBound]) == totient (totient m) testSuite :: TestTree testSuite = testGroup "Primitive root" [ testGroup "CyclicGroup"- [ testIntegralProperty "cyclicGroupToModulo . cyclicGroupFromModulo" cyclicGroupProperty1+ [ testIntegralProperty "cyclicGroupFromModulo" cyclicGroupProperty1 , testIntegralProperty "cyclic group mod p" cyclicGroupProperty2 , testIntegralProperty "cyclic group mod 2p" cyclicGroupProperty3+ , testCase "cyclic group mod 8" cyclicGroupSpecialCase1 ] , testGroup "isPrimitiveRoot'" [ testGroup "primitive root is coprime with modulo"- [ testSmallAndQuick "Integer" (isPrimitiveRoot'Property1 :: AnySign Integer -> CyclicGroup Integer -> Bool)- , testSmallAndQuick "Natural" (isPrimitiveRoot'Property1 :: AnySign Natural -> CyclicGroup Natural -> Bool)- , testSmallAndQuick "Int" (isPrimitiveRoot'Property1 :: AnySign Int -> CyclicGroup Int -> Bool)- , testSmallAndQuick "Word" (isPrimitiveRoot'Property1 :: AnySign Word -> CyclicGroup Word -> Bool)+ [ testSmallAndQuick "Integer" (isPrimitiveRoot'Property1 :: AnySign Integer -> Positive Natural -> Bool)+ , testSmallAndQuick "Natural" (isPrimitiveRoot'Property1 :: AnySign Natural -> Positive Natural -> Bool)+ , testSmallAndQuick "Int" (isPrimitiveRoot'Property1 :: AnySign Int -> Positive Natural -> Bool)+ , testSmallAndQuick "Word" (isPrimitiveRoot'Property1 :: AnySign Word -> Positive Natural -> Bool) ] ] , testGroup "isPrimitiveRoot" [ testSmallAndQuick "primitive root is coprime with modulo" isPrimitiveRootProperty1 , testSmallAndQuick "cyclic group has a primitive root" isPrimitiveRootProperty2 , testSmallAndQuick "primitive root generates cyclic group" isPrimitiveRootProperty3- , testSmallAndQuick "no primitive root in non-cyclic group" isPrimitiveRootProperty4 , testSmallAndQuick "cyclic group has right number of primitive roots" isPrimitiveRootProperty5 ] ]
+ test-suite/Math/NumberTheory/Moduli/SingletonTests.hs view
@@ -0,0 +1,46 @@+-- |+-- Module: Math.NumberTheory.Moduli.SingletonTests+-- Copyright: (c) 2019 Andrew Lelechenko+-- Licence: MIT+-- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com>+--+-- Tests for Math.NumberTheory.Moduli.Singleton+--++{-# LANGUAGE TypeApplications #-}++{-# OPTIONS_GHC -fno-warn-type-defaults #-}++module Math.NumberTheory.Moduli.SingletonTests+ ( testSuite+ ) where++import Test.Tasty++import qualified Data.Map as M++import Math.NumberTheory.Moduli.Singleton+import Math.NumberTheory.Primes+import Math.NumberTheory.TestUtils++someSFactorsProperty1+ :: (Ord a, Num a)+ => [(Prime a, Word)]+ -> Bool+someSFactorsProperty1 xs = case someSFactors xs of+ Some sm -> unSFactors sm == M.assocs (M.fromListWith (+) xs)++cyclicGroupFromModuloProperty1+ :: (Integral a, UniqueFactorisation a)+ => Positive a+ -> Bool+cyclicGroupFromModuloProperty1 (Positive m) = mcg1 == mcg2+ where+ mcg1 = cyclicGroupFromModulo m+ mcg2 = cyclicGroupFromFactors (factorise m)++testSuite :: TestTree+testSuite = testGroup "Singleton"+ [ testSmallAndQuick "unSFactors . someSFactors = id" (someSFactorsProperty1 @Integer)+ , testIntegralPropertyNoLarge "cyclicGroupFromModulo = cyclicGroupFromFactors . factorise" cyclicGroupFromModuloProperty1+ ]
test-suite/Math/NumberTheory/Moduli/SqrtTests.hs view
@@ -25,6 +25,7 @@ import Numeric.Natural import Math.NumberTheory.Moduli hiding (invertMod)+import Math.NumberTheory.Moduli.Singleton import Math.NumberTheory.Primes (unPrime, isPrime, Prime) import Math.NumberTheory.TestUtils @@ -39,10 +40,10 @@ sqrtsModPrimeProperty1 :: AnySign Integer -> Prime Integer -> Bool sqrtsModPrimeProperty1 (AnySign n) p'@(unPrime -> p) = case sqrtsModPrime n p' of [] -> jacobi n p == MinusOne- rt : _ -> (p == 2 || jacobi n p /= MinusOne) && rt ^ 2 `mod` p == n `mod` p+ rt : _ -> (p == 2 || jacobi n p /= MinusOne) && (rt ^ 2 - n) `rem` p == 0 sqrtsModPrimeProperty2 :: AnySign Integer -> Prime Integer -> Bool-sqrtsModPrimeProperty2 (AnySign n) p'@(unPrime -> p) = all (\rt -> rt ^ 2 `mod` p == n `mod` p) (sqrtsModPrime n p')+sqrtsModPrimeProperty2 (AnySign n) p'@(unPrime -> p) = all (\rt -> (rt ^ 2 - n) `rem` p == 0) (sqrtsModPrime n p') sqrtsModPrimeProperty3 :: AnySign Integer -> Prime Integer -> Bool sqrtsModPrimeProperty3 (AnySign n) p'@(unPrime -> p) = nubOrd rts == sort rts@@ -58,7 +59,7 @@ rt : _ = sqrtsModPrime n p' tonelliShanksProperty2 :: Prime Integer -> Bool-tonelliShanksProperty2 p'@(unPrime -> p) = p `mod` 4 /= 1 || rt ^ 2 `mod` p == n `mod` p+tonelliShanksProperty2 p'@(unPrime -> p) = p `mod` 4 /= 1 || (rt ^ 2 - n) `rem` p == 0 where n = head $ filter (\s -> jacobi s p == One) [2..p-1] rt : _ = sqrtsModPrime n p'@@ -80,7 +81,7 @@ sqrtsModPrimePowerProperty1 :: AnySign Integer -> (Prime Integer, Power Word) -> Bool sqrtsModPrimePowerProperty1 (AnySign n) (p'@(unPrime -> p), Power e) = gcd n p > 1- || all (\rt -> rt ^ 2 `mod` (p ^ e) == n `mod` (p ^ e)) (sqrtsModPrimePower n p' e)+ || all (\rt -> (rt ^ 2 - n) `rem` (p ^ e) == 0) (sqrtsModPrimePower n p' e) sqrtsModPrimePowerProperty2 :: AnySign Integer -> Power Word -> Bool sqrtsModPrimePowerProperty2 n e = sqrtsModPrimePowerProperty1 n (fromJust $ isPrime (2 :: Integer), e)@@ -152,7 +153,7 @@ sqrtsModFactorisationProperty1 :: AnySign Integer -> [(Prime Integer, Power Word)] -> Bool sqrtsModFactorisationProperty1 (AnySign n) (take 10 . map unwrapPP -> pes'@(map (first unPrime) -> pes)) = nubOrd ps /= sort ps || all- (\rt -> all (\(p, e) -> rt ^ 2 `mod` (p ^ e) == n `mod` (p ^ e)) pes)+ (\rt -> all (\(p, e) -> (rt ^ 2 - n) `rem` (p ^ e) == 0) pes) (take 1000 $ sqrtsModFactorisation n pes') where ps = map fst pes@@ -185,7 +186,7 @@ sqrtsModProperty1 :: AnySign Integer -> Positive Natural -> Bool sqrtsModProperty1 (AnySign n) (Positive m) = case n `modulo` m of- SomeMod x -> sort (sqrtsMod x) == filter (\rt -> rt * rt == x) [minBound .. maxBound]+ SomeMod x -> sort (sqrtsMod sfactors x) == filter (\rt -> rt * rt == x) [minBound .. maxBound] InfMod{} -> True testSuite :: TestTree
− test-suite/Math/NumberTheory/MoebiusInversion/IntTests.hs
@@ -1,52 +0,0 @@--- |--- Module: Math.NumberTheory.MoebiusInversion.IntTests--- Copyright: (c) 2016 Andrew Lelechenko--- Licence: MIT--- Maintainer: Andrew Lelechenko <andrew.lelechenko@gmail.com>------ Tests for Math.NumberTheory.MoebiusInversion.Int-----{-# OPTIONS_GHC -fno-warn-type-defaults #-}--module Math.NumberTheory.MoebiusInversion.IntTests- ( testSuite- ) where--import Test.Tasty-import Test.Tasty.HUnit-import Test.Tasty.QuickCheck as QC hiding (Positive)--import Math.NumberTheory.MoebiusInversion.Int-import Math.NumberTheory.ArithmeticFunctions-import Math.NumberTheory.TestUtils--totientSumProperty :: Positive Int -> Bool-totientSumProperty (Positive n) = toInteger (totientSum n) == sum (map totient [1 .. toInteger n])--totientSumSpecialCase1 :: Assertion-totientSumSpecialCase1 = assertEqual "totientSum" 4496 (totientSum 121)--totientSumSpecialCase2 :: Assertion-totientSumSpecialCase2 = assertEqual "totientSum" 0 (totientSum (-9001))--totientSumZero :: Assertion-totientSumZero = assertEqual "totientSum" 0 (totientSum 0)--generalInversionProperty :: (Int -> Int) -> Positive Int -> Bool-generalInversionProperty g (Positive n)- = g n == sum [f (n `quot` k) | k <- [1 .. n]]- && f n == sum [runMoebius (moebius k) * g (n `quot` k) | k <- [1 .. n]]- where- f = generalInversion g--testSuite :: TestTree-testSuite = testGroup "Int"- [ testGroup "totientSum"- [ testSmallAndQuick "matches definitions" totientSumProperty- , testCase "special case 1" totientSumSpecialCase1- , testCase "special case 2" totientSumSpecialCase2- , testCase "zero" totientSumZero- ]- , QC.testProperty "generalInversion" generalInversionProperty- ]
test-suite/Math/NumberTheory/MoebiusInversionTests.hs view
@@ -17,35 +17,37 @@ import Test.Tasty.HUnit import Test.Tasty.QuickCheck as QC hiding (Positive) +import Data.Proxy+import Data.Vector.Unboxed (Vector)+ import Math.NumberTheory.MoebiusInversion import Math.NumberTheory.ArithmeticFunctions import Math.NumberTheory.TestUtils -totientSumProperty :: Positive Int -> Bool-totientSumProperty (Positive n) = totientSum n == sum (map totient [1 .. toInteger n])+proxy :: Proxy Vector+proxy = Proxy -totientSumSpecialCase1 :: Assertion-totientSumSpecialCase1 = assertEqual "totientSum" 4496 (totientSum 121)+totientSumProperty :: AnySign Word -> Bool+totientSumProperty (AnySign n) = (totientSum proxy n :: Word) == sum (map totient [1..n]) -totientSumSpecialCase2 :: Assertion-totientSumSpecialCase2 = assertEqual "totientSum" 0 (totientSum (-9001))+totientSumSpecialCase1 :: Assertion+totientSumSpecialCase1 = assertEqual "totientSum" 4496 (totientSum proxy 121 :: Word) totientSumZero :: Assertion-totientSumZero = assertEqual "totientSum" 0 (totientSum 0)+totientSumZero = assertEqual "totientSum" 0 (totientSum proxy 0 :: Word) -generalInversionProperty :: (Int -> Integer) -> Positive Int -> Bool+generalInversionProperty :: (Word -> Word) -> Positive Word -> Bool generalInversionProperty g (Positive n) = g n == sum [f (n `quot` k) | k <- [1 .. n]] && f n == sum [runMoebius (moebius k) * g (n `quot` k) | k <- [1 .. n]] where- f = generalInversion g+ f = generalInversion proxy g testSuite :: TestTree testSuite = testGroup "MoebiusInversion" [ testGroup "totientSum" [ testSmallAndQuick "matches definitions" totientSumProperty , testCase "special case 1" totientSumSpecialCase1- , testCase "special case 2" totientSumSpecialCase2 , testCase "zero" totientSumZero ] , QC.testProperty "generalInversion" generalInversionProperty
test-suite/Math/NumberTheory/PrefactoredTests.hs view
@@ -21,12 +21,12 @@ import Data.List (tails) import Numeric.Natural -import Math.NumberTheory.Euclidean (Euclidean, coprime)+import Math.NumberTheory.Euclidean import Math.NumberTheory.Euclidean.Coprimes import Math.NumberTheory.Prefactored import Math.NumberTheory.TestUtils -isValid :: Euclidean a => Prefactored a -> Bool+isValid :: (Eq a, Num a, GcdDomain a, Euclidean a) => Prefactored a -> Bool isValid pref = abs n == abs (product (map (uncurry (^)) fs)) && and [ coprime g h | ((g, _) : gs) <- tails fs, (h, _) <- gs ]
test-suite/Math/NumberTheory/Primes/CountingTests.hs view
@@ -36,7 +36,7 @@ , (10^10, 455052511) , (10^11, 4118054813) , (10^12, 37607912018)- , (10^13, 346065536839)+ -- , (10^13, 346065536839) -- , (10^14, 3204941750802) -- , (10^15, 29844570422669) -- , (10^16, 279238341033925)
test-suite/Math/NumberTheory/Primes/FactorisationTests.hs view
@@ -16,11 +16,12 @@ import Test.Tasty import Test.Tasty.HUnit +import Control.Arrow import Control.Monad (zipWithM_) import Data.List (nub, sort)+import Data.Maybe -import Math.NumberTheory.Primes.Factorisation-import Math.NumberTheory.Primes.Testing+import Math.NumberTheory.Primes import Math.NumberTheory.TestUtils specialCases :: [(Integer, [(Integer, Word)])]@@ -43,7 +44,7 @@ , (16757651897802863152387219654541878160,[(2,4),(5,1),(12323,1),(1424513,1),(6205871923,1),(1922815011093901,1)]) , (16757651897802863152387219654541878162,[(2,1),(29,1),(78173,1),(401529283,1),(1995634649,1),(4612433663779,1)]) , (16757651897802863152387219654541878163,[(11,1),(31,1),(112160981904206269,1),(438144115295608147,1)])- , (16757651897802863152387219654541878166,[(2,1),(23,1),(277,1),(505353699591289,1),(2602436338718275457,1)])+ -- , (16757651897802863152387219654541878166,[(2,1),(23,1),(277,1),(505353699591289,1),(2602436338718275457,1)]) , ((10 ^ 80 - 1) `div` 9, [(11,1),(17,1),(41,1),(73,1),(101,1),(137,1),(271,1),(3541,1),(9091,1),(27961,1), (1676321,1),(5070721,1),(5882353,1),(5964848081,1),(19721061166646717498359681,1)]) ]@@ -58,13 +59,13 @@ ] factoriseProperty1 :: Assertion-factoriseProperty1 = assertEqual "0" [] (factorise 1)+factoriseProperty1 = assertEqual "0" [] (factorise (1 :: Int)) factoriseProperty2 :: Positive Integer -> Bool factoriseProperty2 (Positive n) = factorise n == factorise (negate n) factoriseProperty3 :: Positive Integer -> Bool-factoriseProperty3 (Positive n) = all (isPrime . fst) (factorise n)+factoriseProperty3 (Positive n) = all (isJust . isPrime . unPrime . fst) (factorise n) factoriseProperty4 :: Positive Integer -> Bool factoriseProperty4 (Positive n) = bases == nub (sort bases)@@ -72,13 +73,13 @@ bases = map fst $ factorise n factoriseProperty5 :: Positive Integer -> Bool-factoriseProperty5 (Positive n) = product (map (uncurry (^)) (factorise n)) == n+factoriseProperty5 (Positive n) = product (map (\(p, k) -> unPrime p ^ k) (factorise n)) == n factoriseProperty6 :: (Integer, [(Integer, Word)]) -> Assertion-factoriseProperty6 (n, fs) = assertEqual (show n) (sort fs) (sort (factorise n))+factoriseProperty6 (n, fs) = assertEqual (show n) (sort fs) (sort $ map (first unPrime) $ factorise n) factoriseProperty7 :: (Integer, [(Integer, Word)]) -> Assertion-factoriseProperty7 (n, fs) = zipWithM_ (assertEqual (show n)) fs (factorise n)+factoriseProperty7 (n, fs) = zipWithM_ (assertEqual (show n)) fs (map (first unPrime) $ factorise n) testSuite :: TestTree testSuite = testGroup "Factorisation"
test-suite/Math/NumberTheory/Recurrences/BilinearTests.hs view
@@ -139,7 +139,7 @@ = case signum (bernoulli !! m) of 1 -> m == 0 || m `mod` 4 == 2 0 -> m /= 1 && odd m- -1 -> m == 1 || (m /= 0 && m `mod` 4 == 0)+ -1 -> m == 1 || (m /= 0 && m `rem` 4 == 0) _ -> False bernoulliProperty2 :: NonNegative Int -> Bool
test-suite/Math/NumberTheory/SmoothNumbersTests.hs view
@@ -13,27 +13,23 @@ ( testSuite ) where -import Prelude hiding (mod)+import Prelude hiding (mod, rem) import Test.Tasty import Test.Tasty.HUnit import Data.Coerce-import Data.List (genericDrop, nub, sort)+import Data.List (nub) import Data.Maybe (fromJust)-import qualified Data.Set as S import Numeric.Natural -import Math.NumberTheory.Euclidean (Euclidean (..), WrappedIntegral (..))+import Math.NumberTheory.Euclidean import Math.NumberTheory.Primes (Prime (..)) import qualified Math.NumberTheory.Quadratic.GaussianIntegers as G import qualified Math.NumberTheory.Quadratic.EisensteinIntegers as E-import Math.NumberTheory.SmoothNumbers+import Math.NumberTheory.SmoothNumbers (SmoothBasis, fromList, isSmooth, smoothOver, smoothOver') import Math.NumberTheory.TestUtils -fromSetListProperty :: (Euclidean a, Ord a) => [a] -> Bool-fromSetListProperty xs = fromSet (S.fromList xs) == fromList (sort xs)--isSmoothPropertyHelper :: Euclidean a => (a -> Integer) -> [a] -> Int -> Int -> Bool+isSmoothPropertyHelper :: (Eq a, Num a, Euclidean a) => (a -> Integer) -> [a] -> Int -> Int -> Bool isSmoothPropertyHelper norm primes' i1 i2 = let primes = take i1 primes' basis = fromJust (fromList primes)@@ -47,19 +43,29 @@ isSmoothProperty2 (Positive i1) (Positive i2) = isSmoothPropertyHelper E.norm (map unPrime E.primes) i1 i2 -fromSmoothUpperBoundProperty :: Integral a => Positive a -> Bool-fromSmoothUpperBoundProperty (Positive n') = case fromSmoothUpperBound n of- Nothing -> n < 2- Just sb -> head (genericDrop (n - 1) (smoothOver (coerce sb))) == n- where- n = WrappedIntegral n' `mod` 5000+smoothOverInRange :: (Ord a, Num a) => SmoothBasis a -> a -> a -> [a]+smoothOverInRange s lo hi+ = takeWhile (<= hi)+ $ dropWhile (< lo)+ $ smoothOver s +smoothOverInRangeBF+ :: (Eq a, Enum a, GcdDomain a)+ => SmoothBasis a+ -> a+ -> a+ -> [a]+smoothOverInRangeBF prs lo hi+ = coerce+ $ filter (isSmooth prs)+ $ coerce [lo..hi]+ smoothOverInRangeProperty :: Integral a => SmoothBasis a -> Positive a -> Positive a -> Bool smoothOverInRangeProperty s (Positive lo') (Positive diff') = xs == ys where- lo = WrappedIntegral lo' `mod` 2^18- diff = WrappedIntegral diff' `mod` 2^18+ lo = WrapIntegral lo' `rem` 2^18+ diff = WrapIntegral diff' `rem` 2^18 hi = lo + diff xs = smoothOverInRange (coerce s) lo hi ys = smoothOverInRangeBF (coerce s) lo hi@@ -76,17 +82,14 @@ b = fromJust $ fromList [1+3*G.ι,6+8*G.ι] l = take 10 $ map abs $ smoothOver' G.norm b +isSmoothSpecialCase2 :: Assertion+isSmoothSpecialCase2 = assertBool "should be smooth" $ isSmooth b 6+ where+ b = fromJust $ fromList [4, 3, 6, 10, 7::Int] testSuite :: TestTree testSuite = testGroup "SmoothNumbers"- [ testGroup "fromSet == fromList"- [ testSmallAndQuick "Int" (fromSetListProperty :: [Int] -> Bool)- , testSmallAndQuick "Word" (fromSetListProperty :: [Word] -> Bool)- , testSmallAndQuick "Integer" (fromSetListProperty :: [Integer] -> Bool)- , testSmallAndQuick "Natural" (fromSetListProperty :: [Natural] -> Bool)- ]- , testIntegralProperty "fromSmoothUpperBound" fromSmoothUpperBoundProperty- , testGroup "smoothOverInRange == smoothOverInRangeBF"+ [ testGroup "smoothOverInRange == smoothOverInRangeBF" [ testSmallAndQuick "Int" (smoothOverInRangeProperty :: SmoothBasis Int -> Positive Int -> Positive Int -> Bool) , testSmallAndQuick "Word"@@ -103,11 +106,11 @@ (smoothNumbersAreUniqueProperty :: SmoothBasis Natural -> Positive Int -> Bool) ] , testGroup "Quadratic rings (Gaussian/Eisenstein)"- [ testGroup "Check that a list of smooth numbers generated by `smoothOver` \- \ only contains valid smooth numbers for the generated basis."+ [ testGroup "smoothOver generates valid smooth numbers" [ testSmallAndQuick "Gaussian" isSmoothProperty1 , testSmallAndQuick "Eisenstein" isSmoothProperty2 ] , testCase "all distinct for base [1+3*i,6+8*i]" isSmoothSpecialCase1+ , testCase "6 is smooth for base [4,3,6,10,7]" isSmoothSpecialCase2 ] ]
test-suite/Math/NumberTheory/TestUtils.hs view
@@ -59,8 +59,7 @@ import Math.NumberTheory.Euclidean import qualified Math.NumberTheory.Quadratic.EisensteinIntegers as E (EisensteinInteger(..)) import Math.NumberTheory.Quadratic.GaussianIntegers (GaussianInteger(..))-import Math.NumberTheory.Moduli.PrimitiveRoot (CyclicGroup(..))-import Math.NumberTheory.Primes (UniqueFactorisation, Prime, unPrime)+import Math.NumberTheory.Primes (Prime, UniqueFactorisation) import qualified Math.NumberTheory.SmoothNumbers as SN import Math.NumberTheory.TestUtils.MyCompose@@ -85,43 +84,12 @@ series = cons2 (:+) ---------------------------------------------------------------------------------- Cyclic group--instance (Eq a, Num a, UniqueFactorisation a, Arbitrary a) => Arbitrary (CyclicGroup a) where- arbitrary = frequency- [ (1, pure CG2)- , (1, pure CG4)- , (9, CGOddPrimePower- <$> (arbitrary :: Gen (Prime a)) `suchThatMap` isOddPrime- <*> (getPower <$> arbitrary))- , (9, CGDoubleOddPrimePower- <$> (arbitrary :: Gen (Prime a)) `suchThatMap` isOddPrime- <*> (getPower <$> arbitrary))- ]--instance (Monad m, Eq a, Num a, UniqueFactorisation a, Serial m a) => Serial m (CyclicGroup a) where- series = pure CG2- \/ pure CG4- \/ (CGOddPrimePower- <$> (series :: Series m (Prime a)) `suchThatMapSerial` isOddPrime- <*> (getPower <$> series))- \/ (CGDoubleOddPrimePower- <$> (series :: Series m (Prime a)) `suchThatMapSerial` isOddPrime- <*> (getPower <$> series))--isOddPrime- :: forall a. (Eq a, Num a, UniqueFactorisation a)- => Prime a- -> Maybe (Prime a)-isOddPrime p = if (unPrime p :: a) == 2 then Nothing else Just p--------------------------------------------------------------------------------- -- SmoothNumbers -instance (Ord a, Euclidean a, Arbitrary a) => Arbitrary (SN.SmoothBasis a) where+instance (Ord a, Num a, Euclidean a, Arbitrary a) => Arbitrary (SN.SmoothBasis a) where arbitrary = (fmap getPositive <$> arbitrary) `suchThatMap` SN.fromList -instance (Ord a, Euclidean a, Serial m a) => Serial m (SN.SmoothBasis a) where+instance (Ord a, Num a, Euclidean a, Serial m a) => Serial m (SN.SmoothBasis a) where series = (fmap getPositive <$> series) `suchThatMapSerial` SN.fromList -------------------------------------------------------------------------------@@ -152,7 +120,7 @@ testIntegralProperty :: forall wrapper bool. (TestableIntegral wrapper, SC.Testable IO bool, QC.Testable bool)- => String -> (forall a. (Euclidean a, Semiring a, Integral a, Bits a, UniqueFactorisation a, Show a) => wrapper a -> bool) -> TestTree+ => String -> (forall a. (GcdDomain a, Euclidean a, Semiring a, Integral a, Bits a, UniqueFactorisation a, Show a) => wrapper a -> bool) -> TestTree testIntegralProperty name f = testGroup name [ SC.testProperty "smallcheck Int" (f :: wrapper Int -> bool) , SC.testProperty "smallcheck Word" (f :: wrapper Word -> bool)@@ -170,7 +138,7 @@ testIntegralPropertyNoLarge :: forall wrapper bool. (TestableIntegral wrapper, SC.Testable IO bool, QC.Testable bool)- => String -> (forall a. (Euclidean a, Semiring a, Integral a, Bits a, UniqueFactorisation a, Show a) => wrapper a -> bool) -> TestTree+ => String -> (forall a. (Euclidean a, Semiring a, Integral a, Bits a, UniqueFactorisation a, Show a, Enum (Prime a)) => wrapper a -> bool) -> TestTree testIntegralPropertyNoLarge name f = testGroup name [ SC.testProperty "smallcheck Int" (f :: wrapper Int -> bool) , SC.testProperty "smallcheck Word" (f :: wrapper Word -> bool)@@ -184,7 +152,7 @@ testSameIntegralProperty :: forall wrapper1 wrapper2 bool. (TestableIntegral wrapper1, TestableIntegral wrapper2, SC.Testable IO bool, QC.Testable bool)- => String -> (forall a. (Euclidean a, Integral a, Bits a, UniqueFactorisation a, Show a) => wrapper1 a -> wrapper2 a -> bool) -> TestTree+ => String -> (forall a. (GcdDomain a, Euclidean a, Integral a, Bits a, UniqueFactorisation a, Show a) => wrapper1 a -> wrapper2 a -> bool) -> TestTree testSameIntegralProperty name f = testGroup name [ SC.testProperty "smallcheck Int" (f :: wrapper1 Int -> wrapper2 Int -> bool) , SC.testProperty "smallcheck Word" (f :: wrapper1 Word -> wrapper2 Word -> bool)
test-suite/Math/NumberTheory/TestUtils/Wrappers.hs view
@@ -28,18 +28,19 @@ import Control.Applicative import Data.Coerce import Data.Functor.Classes+import Data.Semiring (Semiring) import Test.Tasty.QuickCheck as QC hiding (Positive, NonNegative, generate, getNonNegative, getPositive) import Test.SmallCheck.Series (Positive(..), NonNegative(..), Serial(..), Series) -import Math.NumberTheory.Euclidean (Euclidean)+import Math.NumberTheory.Euclidean (GcdDomain, Euclidean) import Math.NumberTheory.Primes (Prime, UniqueFactorisation(..)) ------------------------------------------------------------------------------- -- AnySign newtype AnySign a = AnySign { getAnySign :: a }- deriving (Eq, Ord, Read, Show, Num, Enum, Bounded, Integral, Real, Functor, Foldable, Traversable, Arbitrary, Euclidean)+ deriving (Eq, Ord, Read, Show, Num, Enum, Bounded, Integral, Real, Functor, Foldable, Traversable, Arbitrary, Semiring, GcdDomain, Euclidean) instance (Monad m, Serial m a) => Serial m (AnySign a) where series = AnySign <$> series@@ -57,6 +58,8 @@ -- Positive from smallcheck deriving instance Functor Positive+deriving instance Semiring a => Semiring (Positive a)+deriving instance GcdDomain a => GcdDomain (Positive a) deriving instance Euclidean a => Euclidean (Positive a) instance (Num a, Ord a, Arbitrary a) => Arbitrary (Positive a) where@@ -80,6 +83,8 @@ -- NonNegative from smallcheck deriving instance Functor NonNegative+deriving instance Semiring a => Semiring (NonNegative a)+deriving instance GcdDomain a => GcdDomain (NonNegative a) deriving instance Euclidean a => Euclidean (NonNegative a) instance (Num a, Ord a, Arbitrary a) => Arbitrary (NonNegative a) where@@ -134,7 +139,7 @@ -- Power newtype Power a = Power { getPower :: a }- deriving (Eq, Ord, Read, Show, Num, Enum, Bounded, Integral, Real, Functor, Foldable, Traversable, Euclidean)+ deriving (Eq, Ord, Read, Show, Num, Enum, Bounded, Integral, Real, Functor, Foldable, Traversable, Semiring, GcdDomain, Euclidean) instance (Monad m, Num a, Ord a, Serial m a) => Serial m (Power a) where series = Power <$> series `suchThatSerial` (> 0)
test-suite/Test.hs view
@@ -12,10 +12,10 @@ import qualified Math.NumberTheory.Moduli.EquationsTests as ModuliEquations import qualified Math.NumberTheory.Moduli.JacobiTests as ModuliJacobi import qualified Math.NumberTheory.Moduli.PrimitiveRootTests as ModuliPrimitiveRoot+import qualified Math.NumberTheory.Moduli.SingletonTests as ModuliSingleton import qualified Math.NumberTheory.Moduli.SqrtTests as ModuliSqrt import qualified Math.NumberTheory.MoebiusInversionTests as MoebiusInversion-import qualified Math.NumberTheory.MoebiusInversion.IntTests as MoebiusInversionInt import qualified Math.NumberTheory.Powers.CubesTests as Cubes import qualified Math.NumberTheory.Powers.FourthTests as Fourth@@ -72,12 +72,10 @@ , ModuliEquations.testSuite , ModuliJacobi.testSuite , ModuliPrimitiveRoot.testSuite+ , ModuliSingleton.testSuite , ModuliSqrt.testSuite ]- , testGroup "MoebiusInversion"- [ MoebiusInversion.testSuite- , MoebiusInversionInt.testSuite- ]+ , MoebiusInversion.testSuite , Prefactored.testSuite , testGroup "Primes" [ Primes.testSuite