lynn/elm-arithmetic/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

Arithmetic

A library that provides useful number-theoretical functions for dealing with integers, primes, divisibility, et cetera.

Primes

isPrime : Int -> Bool

Test whether an integer is a positive prime.

isPrime 2357 == True
isPrime 500 == False
primesBelow : Int -> List Int

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]
primeFactors : Int -> List Int

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 == []
primeExponents : Int -> List ( Int, Int )

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 product

Parity

isEven : Int -> Bool

Test whether an integer is even.

isEven 2 == True
isEven 3 == False
isOdd : Int -> Bool

Test whether an integer is odd.

isOdd 2 == False
isOdd 3 == True

Divisors

divides : Int -> Int -> Bool

Test whether one number divides another.

10 `divides` 120 == True
10 `divides` 125 == False
isMultipleOf : Int -> Int -> Bool

Test whether one number is a multiple of another.

120 `isMultipleOf` 10 == True
125 `isMultipleOf` 10 == False
divisors : Int -> List Int

Get all divisors of a number, in ascending order.

divisors 20 == [1, 2, 4, 5, 10, 20]
properDivisors : Int -> List Int

Get all proper divisors (i.e., divisors less than the input) of a number, in ascending order.

properDivisors 20 == [1, 2, 4, 5, 10]
numDivisors : Int -> Int

Get the number of divisors of a number (counting itself).

GCD and LCM

gcd : Int -> Int -> Int

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 == 8
lcm : Int -> Int -> Int

Calculate 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 == 560
isCoprimeTo : Int -> Int -> Bool

Test whether two integers are coprime.

56 `isCoprimeTo` 80 == False
5 `isCoprimeTo` 8 
totient : Int -> Int

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 == 560

Base conversion

toBase : Int -> Int -> List Int

Convert 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 binary
fromBase : Int -> List Int -> Int

Interpret 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] = 42

Squares

intSquareRoot : Int -> Int

Integer square root, rounding down.

intSquareRoot 20 == 4
intSquareRoot 25 == 5
exactIntSquareRoot : Int -> Maybe Int

Integer square root returning Nothing if the given number is not a square.

exactIntSquareRoot 20 == Nothing
exactIntSquareRoot 25 == Just 5
isSquare : Int -> Bool

Test whether a number is a square.

isSquare 20 == False
isSquare 25 == True

Cubes

cbrt : Float -> Float

Take the cube root of a number.

cbrt 15.625 == 2.5
intCubeRoot : Int -> Int

Integer cube root, rounding down.

intCubeRoot 800 == 9
intCubeRoot 1000 == 10
exactIntCubeRoot : Int -> Maybe Int

Integer cube root, returning Nothing if the given number is not a cube.

exactIntCubeRoot 800 == Nothing
exactIntCubeRoot 1000 == Just 10
isCube : Int -> Bool

Test whether a number is a cube.

isCube 800 == False
isCube 1000 == True

Modular arithmetic

powerMod : Int -> Int -> Int -> Int

powerMod 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