euler-0.1.0.0

Safe HaskellSafe
LanguageHaskell2010

Euler.Util.Prime

Contents

Synopsis

Documentation

>>> import Test.QuickCheck
>>> import Data.List.Ordered (isSorted)

factorizations :: Integral a => a -> Map a [a] Source #

factorizations n gives the prime factorizations of numbers in [1..n]. Each factorization is sorted.

factorizations 50 == Map'.fromList [(n, primeFactors n) | n <- [1..50]]

factorizationsAsc :: Integral a => [[a]] Source #

>>> take 9 factorizationsAsc
[[],[2],[3],[2,2],[5],[2,3],[7],[2,2,2],[3,3]]
take 50 factorizationsAsc == (primeFactors <$> [1..50])

primes :: Integral int => [int] #

This global constant is an infinite list of prime numbers. It is generated by a lazy wheel sieve and shared across the whole program run. If you are concerned about the memory requirements of sharing many primes you can call the function wheelSieve directly.

isPrime :: Integral int => int -> Bool #

Checks whether a given number is prime.

This function uses trial division to check for divisibility with all primes below the square root of the given number. It is impractical for numbers with a very large smallest prime factor.

Finding prime factors

primeFactors :: (Integral a, Integral b) => a -> [b] Source #

forAll (listOf $ elements $ take 20 primes) (\fs -> primeFactors (product fs) == sort fs)
\(Positive n) -> isSorted (primeFactors n)

smallestPrimeFactor :: (Integral a, Integral b) => a -> b Source #

smallestPrimeFactor n is the smallest prime factor p such that p divides n.

largestPrimeFactor :: (Integral a, Integral b) => a -> b Source #

largestPrimeFactor n is the largest prime factor p such that p divides n.

>>> largestPrimeFactor 2
2
>>> largestPrimeFactor 3
3
>>> largestPrimeFactor 4
2
>>> largestPrimeFactor 99
11
forAll (elements $ take 50 primes) (\p -> largestPrimeFactor p == p)

Divisors

divisors :: Integral a => a -> [a] Source #

The divisors of a number, in no particular order.

countDivisors :: (Integral a, Integral b) => a -> b Source #

countDivisors n is the number of integers d from [1..n] such that d divides n.

divisorsOfPrimeProduct :: Integral a => [a] -> [a] Source #

The divisors of product fs, where fs are all prime, in no particular order.

Proper divisors

"Proper divisors" are divisors of a number, other than the number itself.

properDivisors :: Integral a => a -> [a] Source #

The proper divisors of a number, in no particular order.

Example from Euler problem 21:

>>> sort (properDivisors 220)
[1,2,4,5,10,11,20,22,44,55,110]

properDivisorsOfPrimeProduct :: Integral a => [a] -> [a] Source #

The proper divisors of product fs, where fs are all prime, in no particular order.