Prime numbers and related number theoretical stuff.
- primes :: [Integer]
- primesSimple :: [Integer]
- primesTMWE :: [Integer]
- groupIntegerFactors :: [Integer] -> [(Integer, Int)]
- integerFactorsTrialDivision :: Integer -> [Integer]
- integerLog2 :: Integer -> Integer
- ceilingLog2 :: Integer -> Integer
- isSquare :: Integer -> Bool
- integerSquareRoot :: Integer -> Integer
- ceilingSquareRoot :: Integer -> Integer
- integerSquareRoot' :: Integer -> (Integer, Integer)
- integerSquareRootNewton' :: Integer -> (Integer, Integer)
- powerMod :: Integer -> Integer -> Integer -> Integer
- millerRabinPrimalityTest :: Integer -> Integer -> Bool
- isProbablyPrime :: Integer -> Bool
- isVeryProbablyPrime :: Integer -> Bool
List of prime numbers
A relatively simple but still quite fast implementation of list of primes. By Will Ness http://www.haskell.org/pipermail/haskell-cafe/2009-November/068441.html
Groups integer factors. Example: from [2,2,2,3,3,5] we produce [(2,3),(3,2),(5,1)]
Integer square root
Integer square root (largest integer whose square is smaller or equal to the input) using Newton's method, with a faster (for large numbers) inital guess based on bit shifts.
Smallest integer whose square is larger or equal to the input
We also return the excess residue; that is
(a,r) = integerSquareRoot' n
a*a + r = n a*a <= n < (a+1)*(a+1)
Newton's method without an initial guess. For very small numbers (<10^10) it is somewhat faster than the above version.
Efficient powers modulo m.
powerMod a k m == (a^k) `mod` m
Miller-Rabin Primality Test (taken from Haskell wiki).
We test the primality of the first argument
n by using the second argument
a as a candidate witness.
If it returs
n is composite. If it returns
n is either prime or composite.
A random choice between
(n-2) is a good choice for
For very small numbers, we use trial division, for larger numbers, we apply the
Miller-Rabin primality test
log4(n) times, with candidate witnesses derived
n using a pseudo-random sequence
(which should be based on a cryptographic hash function, but isn't, yet).
Thus the candidate witnesses should behave essentially like random, but the resulting function is still a deterministic, pure function.
TODO: implement the hash sequence, at the moment we use