Maths
Maths includes mathematical functions not defined in the standard Go math package.
Install / Use
/learn @theriault/MathsREADME
Maths
maths includes mathematical functions not defined in the standard Go math package. Most functions support any primitive integer or float type through generics.
Installation
go get github.com/theriault/maths
What's Included
Combinatorics
import "github.com/theriault/maths/combinatorics"
Factorial
Source | Tests | Wikipedia | OEIS
$\displaystyle n! \; = \; \prod_{i=1}^{n} i$
combinatorics.Factorial(10) // will return uint64(3628800)
Falling Factorial
$\displaystyle x^{\underline{n}} \; = \; \prod _{k=1}^{n}(x-k+1)$
combinatorics.FallingFactorial(8, 3) // will return uint64(336)
combinatorics.PartialPermutations(8, 3) // will return uint64(336)
Rising Factorial
$\displaystyle x^{\overline{n}} \; = \; \prod _{k=1}^{n}(x+k-1)$
combinatorics.RisingFactorial(2, 3) // will return uint64(24)
Number Theory
import "github.com/theriault/maths/numbertheory"
Aliquot Sum
Source | Tests | Wikipedia | OEIS
$\displaystyle s(n) = \sigma_1(n) - n = \sum_{\substack{i = 1 \\ i | n}}^{n-1} i$
numbertheory.AliquotSum(60) // will return uint64(108)
Coprime
$\displaystyle f(a,b) = \begin{cases}\text{true} &\text{if}\ \gcd(a,b) = 1 \\ \text{false} &\text{else} \end{cases}$
numbertheory.Coprime(3*5*7, 11*13*17) // will return true
Digit Sum
$\displaystyle f_b(n) = \sum_{i=0}^{\lfloor \log_b{n} \rfloor} \frac{n \bmod b^{i+1} - n \bmod b^i}{b^i}$
numbertheory.DigitSum(9045, 10) // will return int(18)
Digital Root
$\displaystyle f_{b}(n)={\begin{cases} 0 &\text{if}\ n=0\\ n\ \bmod (b-1)&{\text{if}}\ n\not \equiv 0{\pmod {b-1}} \\ b-1 &\text{else} \end{cases}}$
numbertheory.DigitalRoot(9045, 10) // will return int(9)
Divisors function
$\displaystyle \sigma_z(n) = \sum_{\substack{i = 1 \\ i | n}}^{n} i^{z}$
Number-of-divisors (z = 0)
Source | Tests | Wikipedia | OEIS
numbertheory.NumberOfDivisors(48) // will return uint64(10)
Sum-of-divisors (z = 1)
Source | Tests | Wikipedia | OEIS
numbertheory.SumOfDivisors(48) // will return uint64(124)
Greatest Common Divisor
numbertheory.GCD(48,18) // will return int(6)
Least Common Multiple
numbertheory.LCM(48,18) // will return int(144)
Möbius function
Source | Tests | Wikipedia | OEIS
$\displaystyle \mu(n) = \begin{cases} +1 & n \text{ is square-free with even number of prime factors} \\ -1 & n \text{ is square-free with odd number of prime factors} \\ 0 & n \text{ is not square-free} \end{cases}$
numbertheory.Mobius(70) // will return int8(-1)
Politeness
Source | Tests | Wikipedia | OEIS
$\displaystyle p(n) = \left( \prod_{\substack{p |n \\ p \neq 2}}^{n} v_p(n) + 1\right)-1$
where $v_p(n)$ is the $p$-adic order
numbertheory.Politeness(32) // will return uint64(0)
Polygonal Numbers
$\displaystyle p(s, n) = \frac{(s-2)n^2-(s-4)n}{2}$
numbertheory.PolygonalNumber(3, 4) // will return uint64(10)
Finding $n$:
$\displaystyle p(s, x) = \frac{\sqrt{8(s-2)x+(s-4)^2}+(s-4)}{2(s-2)}$
numbertheory.PolygonalRoot(3, 10) // will return float64(4)
Finding $s$:
$\displaystyle p(n, x) = 2+\frac{2}{n} \cdot \frac{x-n}{n-1}$
numbertheory.PolygonalSides(4, 10) // will return float64(3)
Prime Factorization
numbertheory.PrimeFactorization(184756) // will return []uint64{2, 2, 11, 13, 17, 19}
Primorial
Source | Tests | Wikipedia | OEIS
$\displaystyle n\# = \prod_{\substack{i=2 \\ i \in \mathbb{P}}}^{n} i$
numbertheory.Primorial(30) // will return uint64(6469693230)
Radical
Source | Tests | Wikipedia | OEIS
$\displaystyle rad(n) = \prod_{p | n}p$
numbertheory.Radical(60) // will return uint64(30)
Totient
Euler's Totient
Source | Tests | Wikipedia | OEIS
$\displaystyle \varphi(n) = n \prod_{p | n} \left(1 - \frac{1}{p}\right) $
numbertheory.Totient(68) // will return uint64(32)
Jordan's Totient
$\displaystyle J_k(n) = n^k \prod_{p | n} \left(1 - \frac{1}{p^k}\right) $
numbertheory.TotientK(60, 2) // will return uint64(2304)
Statistics
import "github.com/theriault/maths/statistics"
Average/Mean
Generalized Mean
statistics.GeneralizedMean([]float64{1, 1000}, 2) // will return float64(707.1071347398497)
statistics.RootMeanSquare(1, 1000) // will return float64(707.1071347398497)
Arithmetic Mean
$\displaystyle \bar{x} = \frac{1}{n}\left (\sum_{i=1}^n{x_i}\right ) = \frac{x_1+x_2+\cdots +x_n}{n}$
statistics.Mean(1, 1000) // will return float64(500.5)
Geometric Mean
$\displaystyle \bar{x} = \left( \prod_{i=1}^n{x_i} \right )^\frac{1}{n} = \exp{\left( {\frac{1}{n}\sum\limits_{i=1}^{n}\ln x_i} \right)} = \left(x_1 x_2 \cdots x_n \right)^\frac{1}{n}$
statistics.GeometricMean(1, 1000) // will return float64(31.62...)
Harmonic Mean
$\displaystyle \bar{x} = n \left ( \sum_{i=1}^n \frac{1}{x_i} \right ) ^{-1}$
statistics.HarmonicMean(1, 1000) // will return float64(1.99...)
Central Moment
statistics.CentralMoment([]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 2) // returns float64(8.25)
Interquartile Range (IQR)
statistics.InterquartileRange(3, 6, 7, 8, 8, 10, 13, 15, 16, 20) // returns float64(7.25)
Kurtosis
Population
statistics.Kurtosis(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // returns float64(1.5133)
