A library that provides useful number-theoretical functions for dealing with integers, primes, divisibility, et cetera.
Get all primes in the given range [0..n-1], using the Sieve of
Eratosthenes.
primesBelow 4 == [2, 3]
primesBelow 17 == [2, 3, 5, 7, 11, 13]Return a list of all prime factors for a given positive integer, in ascending order. If the input is less than 2, the empty list is returned.
primeFactors 24 == [2, 2, 2, 3]
primeFactors 767 == [13, 59]
primeFactors 1 == []Return a list of all prime-exponent pairs for a given positive integer's prime decomposition, with the primes in ascending order. If the input is less than 2, the empty list is returned.
primeExponents 24 == [(2, 3), (5, 2)] -- 2^3 * 5^2
primeExponents 531764 == [(2, 1), (11, 2), (13, 3)] -- 2^1 * 11^2 * 13^3
primeExponents 1 == [] -- empty productTest whether an integer is even.
isEven 2 == True
isEven 3 == FalseTest whether an integer is odd.
isOdd 2 == False
isOdd 3 == TrueTest whether one number divides another.
10 `divides` 120 == True
10 `divides` 125 == FalseTest whether one number is a multiple of another.
120 `isMultipleOf` 10 == True
125 `isMultipleOf` 10 == FalseGet all divisors of a number, in ascending order.
divisors 20 == [1, 2, 4, 5, 10, 20]Get all proper divisors (i.e., divisors less than the input) of a number, in ascending order.
properDivisors 20 == [1, 2, 4, 5, 10]Get the number of divisors of a number (counting itself).
Calculate the greatest common divisor of two integers. gcd x 0 and
gcd 0 x both return x. Negative arguments are made positive first.
gcd 56 80 == 8Calculate the least common multiple of two integers. lcm x 0 and
lcm 0 x both return 0. Negative arguments are made positive first.
lcm 56 80 == 560Test whether two integers are coprime.
56 `isCoprimeTo` 80 == False
5 `isCoprimeTo` 8 Compute Euler's totient function φ(n): the number of positive integers
1 <= k <= n for which gcd(n, k) == 1. The input is made positive first.
totient 99 == 60
totient 1450 == 560Convert a number to a list of digits in the given base. The input number is made positive first.
toBase 2 42 = [1, 0, 1, 0, 1, 0] -- 42 in binaryInterpret a list of digits as a number in the given base. The input is
expected to consist of integers d for which 0 <= d < base.
fromBase 2 [1, 0, 1, 0, 1, 0] = 42Integer square root, rounding down.
intSquareRoot 20 == 4
intSquareRoot 25 == 5Integer square root returning Nothing if the given number is not a
square.
exactIntSquareRoot 20 == Nothing
exactIntSquareRoot 25 == Just 5Test whether a number is a square.
isSquare 20 == False
isSquare 25 == TrueTake the cube root of a number.
cbrt 15.625 == 2.5Integer cube root, rounding down.
intCubeRoot 800 == 9
intCubeRoot 1000 == 10Integer cube root, returning Nothing if the given number is not a cube.
exactIntCubeRoot 800 == Nothing
exactIntCubeRoot 1000 == Just 10Test whether a number is a cube.
isCube 800 == False
isCube 1000 == TruepowerMod b e m efficiently calculates b ^ e (modulo m). It assumes
b >= 0, e >= 0 and m >= 1.
For example, to compute 4147 ^ 8671 % 1000 efficiently:
powerMod 4147 8671 1000 == 803
Test whether an integer is a positive prime.