Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- factorizations :: Integral a => a -> Map a [a]
- factorizationsAsc :: Integral a => [[a]]
- primes :: Integral int => [int]
- isPrime :: Integral int => int -> Bool
- primeFactors :: (Integral a, Integral b) => a -> [b]
- smallestPrimeFactor :: (Integral a, Integral b) => a -> b
- largestPrimeFactor :: (Integral a, Integral b) => a -> b
- divisors :: Integral a => a -> [a]
- countDivisors :: (Integral a, Integral b) => a -> b
- divisorsOfPrimeProduct :: Integral a => [a] -> [a]
- properDivisors :: Integral a => a -> [a]
- properDivisorsOfPrimeProduct :: Integral a => [a] -> [a]
Documentation
>>>
import Test.QuickCheck
>>>
import Data.List.Ordered (isSorted)
factorizations :: Integral a => a -> Map a [a] Source #
gives the prime factorizations of numbers in factorizations
n[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 #
is the smallest prime factor smallestPrimeFactor
np
such that p
divides
n
.
largestPrimeFactor :: (Integral a, Integral b) => a -> b Source #
is the largest prime factor largestPrimeFactor
np
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
countDivisors :: (Integral a, Integral b) => a -> b Source #
is the number of integers d from countDivisors
n[1..n]
such that d
divides n
.
divisorsOfPrimeProduct :: Integral a => [a] -> [a] Source #
The divisors of
, where product
fsfs
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
, where product
fsfs
are all prime, in no
particular order.