chunky v0.13.0 Chunky.Math View Source
Integer math, number theory, factorization, prime numbers, and numerical analysis.
A seperate module of predicate functions (inspection and analysis of individual integers, like
testing for primality) are available in Chunky.Math.Predicates
.
Modular Arithmetic
Pure integer operations for Modular Arithmetic.
pow/3
- Integer power in modular exponentiation
Generating Constants
digits_of_pi/1
- Generaten
digits of pi, as a large integernext_digit_of_pi/0
andnext_digit_of_pi/1
- A state carrying digit generator for Pi
Integer Arithmetic
Arithmetic functions for pure integer operations.
double_factorial/1
- Double factorial (n!!
) ofn
factorial/1
- Factorial (n!
) ofn
falling_factorial/2
- Falling factorial of(n)m
integer_nth_root?/3
- Determine if an integer has a specificn-th
rootinteger_nth_root/3
- Find then
-th integer root, if it existslcm/2
- Least common multiple ofn
andm
lcm/1
- Least common multiple of a list of integersnth_integer_root/2
- Nearestm
-th root ofn
such thatroot^m <= n
, returning a tuplenth_integer_root!/2
- Nearestm
-th root ofn
such thatroot^m <= n
, returning an integerpow/2
- Integer exponentiationrising_factorial/2
- Rising factorial ofn^(m)
Float Arithmetic
nth_root/3
- Floating pointn-th
root of an integerfloats_equal?/3
- Determine if two floats are equal, within an error bound
Sets and Lists
has_subset_sum?/2
- Does any subset of integers in a list sum ton
?
Factorization and Divisors
Work with divisors and prime factors.
factorization_count/1
- Count the number of possible factorizations ofn
.factor_pairs/2
- Find pair wise factors ofn
.factors/1
- All divisors for an integeris_power_of?/2
- Isn
a power ofm
?is_root_of?/2
- Check ifm
is a k-th root ofn
ordered_factorization_count/1
- Count the number of ordered factorizations ofn
partitions_into_two_squares/1
- Count the number of partitions ofn
into the sum of two squaresprime_factors/1
- Factorize an integer into prime factorsreduced_prime_factors/1
- Find the prime factors and their powers forn
sigma/1
- Sigma-1 function (sum of divisors)tau/1
- Tau function, number of divisors ofn
Digit Checks and Manipulations
contains_digit?/2
- Check ifn
contains the digit in its current base representationis_cyclops_number_in_base?/2
- Isn
a cyclops number in baseb
?digit_count/3
- Count digits inn
in any base representationdigit_runs/2
- Breakn
apart into runs of similar digitsdigit_runs_count/2
- Count the number of digit runs inn
digit_sum/1
- Calculate the sum of the digits ofn
is_in_base?/2
- Is the number or list of digitsn
a valid number in baseb
?is_pandigital_in_base?/2
- Isn
a pandigital number in baseb
?is_plaindrome_in_base?/2
- Doesn
have never decreasing digits in baseb
?is_palindromic_in_base?/2
- Isn
palindromic in baseb
?length_in_base/2
- How many digits long isn
in baseb
?remove_digits!/3
- Remove one or more digits fromn
, returning a reconstituted numberrepunit/1
- Calculate then
th Repunitreverse_number/1
- Reverse the digits ofn
rotations/1
- Enumerate all circular rotations ofn
to_base/2
- Convert a decimal integer to any base, returning an integer or list depending on base
Primes
Analyze, test, and generate prime numbers.
coprimes/1
- Find coprimes ofn
from 2 ton - 1
coprimes/2
- Find coprimes ofn
up toa
greatest_prime_factor/1
- Find the largest prime factor ofn
is_coprime?/2
- Test if two integers are coprime or relatively primeis_euler_jacobi_pseudo_prime?/2
- Euler-Jacobi pseudo-primality ofn
in baseb
is_euler_pseudo_prime?/2
- Euler pseudo-primality ofn
in baseb
is_pseudo_prime?/2
- Fermat pseudo-primality ofn
in baseb
least_prime_factor/1
- Find the smallest prime factor ofn
prime_factor_exponents/1
- Find the exponents of all prime factors ofn
prime_pi/1
- Prime counting function, number of primes less than or equaln
Number Theory
Functions related to Number Theory operations over the integers.
aliquot_sum/1
- Find the Aliquot Sum ofn
bernoulli_number/1
- Find then
-th Bernoulli numberbigomega/1
- Big Omega function - count of distinct primes, with multiplicitychebyshev_triangle_coefficient/2
- Calculate coefficient of Chebyshev's polynomial of the second kinddivisors_of_form_mx_plus_b/3
- Divisors ofn
that conform to values ofmx + b
get_rhonda_to/2
- Find the bases for whichn
is a Rhonda numberhamming_weight/2
- Find the Hamming Weight, the count of digits not0
, in different base representations ofn
is_of_mx_plux_b/3
- Doesn
conform to values ofmx + b
is_narcissistic_in_base?/2
- Is the sum of thek
th power of the digits of ak
length numbern
equal ton
in baseb
?is_rhonda_to_base?/2
- Isn
a Rhonda number to baseb
?jacobi_symbol/2
- Calculate the Jacobi symbol for(a/n)
jordan_totient/2
- Calculate the Jordan totientJ-k(n)
legendre_symbol/2
- Calculate the Legendre symbol for(a/p)
lucas_number/1
- Find then
-th Lucas Numberlucky_numbers/1
- Generate the firstn
Lucky Numbersmobius_function/1
- Classical Mobius Functionomega/1
- Omega function - count of distinct primespartition_count/1
- Number of ways to partitionn
into sumsp_adic_valuation/2
- The p-adic valuation function (for primep
and integern
)pell_number/1
- Find then
-th denominator in the infinite sequence of fractional approximations ofsqrt(2)
pentagonal_number/1
- Find then
-th pentagonal numberperfect_partition_count/1
- Count of perfect partitions ofn
product_of_prime_factor_exponents/1
- Decomposen
to prime factors of the formx^y
, find product of ally
radical/1
- Square-free kernel, orrad(n)
- product of distict prime factorssigma/2
- Generalized Sigma function for integerssquare_pyramidal_number/1
- Number of elements in ann x n
stacked square pyramidstern_diatomic_series/1
- Find then
th number in the diatomic seriestetrahedral_number/1
- Find then
-th tetrahedral numbertotient/1
- Calculate Euler's totient forn
triangle_number/1
- Number of elements in a triangle ofn
rowstriangle_row_for_element/1
- Row in triangle forn
-th elementtriangle_position_for_element/1
- Position in triangle forn
-th element
Polynomials
binomial/2
- Compute the binomial coefficient over(n k)
euler_polynomial/2
- Calculate the Euler polynomialE_m(x)
j_invariant_q_coefficient/1
- Find then
-th coefficient of the q expansion of the modular J invariant function.ramanujan_tau/1
- Find Ramanujan's Tau ofn
Combinatorics
Functions dealing with Combinatorics, permutation calculations, and related topics.
bell_number/1
- Compute the number of partitions of a set of sizen
binary_partitions_count/1
- Find the number of ways to partition2 * n
into powers of2
catalan_number/1
- Find the Catalan number forn
, counts of highly recursive objects and setsderangement_count/1
- Number of derangements of set sizen
, or subfactorial nendomorphism_count/1
- Number of endomorphisms of a set of sizen
euler_number/1
- Find then
-th Euler number. Also writtenEulerE
.eulerian_number/2
-A(n, m)
, the number of permutations of the numbers 1 ton
in which exactlym
elements are greater than the previous elementeuler_zig/1
- Find then
-th Euler zig numbereuler_zig_zag/1
- Calculate the size of certain set permutationsfubini_number/1
- Find then
-th Fubini number, the number of ordered partitions of a set sizen
hipparchus_number/1
- Find then
-th Hipparchus/Schroeder/super-Catalan numberinvolutions_count/1
- Number of self-inverse permutations ofn
elementsjacobsthal_number/1
- Calculate then
-th Jacobsthal numbermotzkin_number/1
- The number of different ways of drawing non-intersecting chords betweenn
points on a circlen_choose_k/2
- Calculate the number ofk
element sets that can be obtained from ann
element setordered_subsets_count/1
- Count the number of partitions of a set of sizen
into any number of ordered lists.pancake_cut_max/1
- Count the maximum number of pieces that can be made fromn
cuts of a disk.plane_partition_count/1
- Number of plane partitions with sumn
schroder_number/1
- Find then
th Schröder number.stirling_partition_number/2
- Number of non-empty sets of sizek
that can be made from a set of sizen
total_partitions/1
- Count the number of total, or series reduced tree, partitions ofn
elementstwo_color_bracelet_with_period_count/2
- The number of bracelet permutations forn
beads, with periodn
two_color_bracelet_count/2
- The number of bracelet permutations forn
beads of two colorswedderburn_etherington_number/1
- Calculate the size of certain binary tree sets
Graph Theory
Analyze numbers related to graph theory and trees.
cayley_number/1
- Number of trees forn
labeled verticeslabeled_rooted_forests_count/1
- Number of labeled, rooted forests withn
nodeslabeled_rooted_trees_count/1
- Number of labeled, rooted trees withn
nodesplanted_3_trees_count/1
- The number of planted 3 trees of height less thann
rooted_tree_count/1
- The number of unlabeled, or planted, trees withn
nodes
Fractals
Integer fractals, and related number sets.
start_kolakoski_sequence/1
- Initialize the structure for a Kolakoski sequenceextend_kolakoski_sequence/1
- Extend a Kolakoski sequence by one iterationextend_kolakoski_sequence_to_length/2
- Extend a Kolakoski sequence to be at least the given length
Abstract Algebra
Functions, numbers, and set counting related to Abstract Algebra.
abelian_group_count/1
- Number of Abelian groups of ordern
Differential Topology
Manifolds, differential geometry, and differential topology functions.
hurwitz_radon_number/1
- Calculate the Hurwitz-Radon number forn
Cryptography
Functions related to cryptographc analysis, factorization in cryptography, and numeric constructions.
is_b_smooth?/2
- Isn
prime factor smooth up tob
- all prime factors <=b
Number Generation
Number sequence iteration functions used by the Chunky.Sequence
library.
next_number/2
- Use a number theory predicate to find the next integer in a sequence
Link to this section Summary
Functions
Count the number of Abelian groups of order n
.
Find the Aliquot Sum of n
.
Calculate the Bell Number of n
, or the number of possible partitions of a set of size n
.
Calculate the n
-th Bernoulli number, and return it as a Fraction.
Calculate Ω(n)
- number of distinct prime factors, with multiplicity.
Find the number of ways to partition 2 * n
into powers of 2
.
Calculate the binomial coefficient (n k).
Find the Catalan number of n
, C(n)
.
Calculate Cayley's formula for n
- the number of trees on n
labeled vertices.
Calculate Chebyshev's triangle of coefficients at S(n, k)
.
Check if a number n
contains the number m
in it's decimal
digit representation.
Find all positive coprimes of n
, from 2
up to n
.
Find all positive coprimes of n
from 2
up to d
.
Find the number of derangements of a set of size n
.
Count the number of specific digits in n
.
Break apart n
into runs of digits.
Count the number of digit runs in n
.
Calculate the sum of the digits of n
.
Generate n
digits of pi, as a single large integer.
Find all divisors of n
of the form mx + b
.
Calculate the double factorial of n
, n!!
.
Count the number of endofunctions (as endomorphisms) for a set of size n
.
Find the n
-th Euler number. Also written EulerE
.
Calculate the Euler polynomial E_m(x)
.
Find the n
-th Euler zig number.
Calculate the Euler zig zag, or up/down, number for n
.
Calculate the Eulerian Number A(n, m)
, the number of permutations of the numbers 1 to n
in which exactly m
elements are greater than the previous element.
Extend a Kolakoski sequence by one iteration.
Extend a Kolakoski sequence by successive iterations until the sequence is at least the given length.
Find all pairs of factors of n
, with or without duplicates.
The factorial of n
, or n!
.
Count the number of possible factorizations of n
.
Factorize an integer into all divisors.
Calculate the falling factorial (n)m
.
Compare two floating points number using an epsilon error boundary.
Find the n
-th Fubini number, the number of ordered partitions of a set size n
.
Find the bases for which n
is a Rhonda number.
Find the gpf(n)
or greatest prime factor.
Find the Hamming Weight of n
in a specific numeric base.
Does a list of numbers contain any subset that sums to n
?
Find the n
-th Hipparchus number.
Calculate the Hurwitz-Radon number for n
, the number of independent vector
fields on a sphere in n
-dimensional euclidean space.
Determine if the n-th root of a number is a whole integer, returning a boolean and the root value.
Predicate version of integer_nth_root/3
- does x
have an integer n
-th root.
Find the number of involutions, or self-inverse permutations, on n
elements.
Determine if an integer n
is b
-smooth, a composite of prime factors less than or equal to b
.
Determine if two numbers, a
and b
, are co-prime.
Is n
a cyclops number in base b
?
Check if n
is an Euler-Jacobi pseudo-prime to base a
.
Check if n
is an Euler pseudo-prime in base a
.
Check if n
is a valid number in base b
.
A narcissistic number n
of length k
is equal to the sum of the digits of n
to the k
-th power.
Determine if n
is a value of the form mx + b
or mk + b
, for specific
values of m
and b
.
Check if n
is palindromic in base b
.
Determine if n
is pandigital in base b
.
Check if a number n
in numeric base b
is a plaindrome. A plaindrome has digits that
never decrease in value when read from left to right.
Check if n
is a power of m
.
Determine if n
is a Fermat pseudo-prime to base a
.
Check if n
is a Rhonda number to the base b
.
Check if n
is any k
-th root of m
, where k > 2
.
Find the n
-th coefficient of the q expansion of the modular J invariant function.
Calculate the Jacobi Symbol (n/k)
.
Find the n
-th Jacobsthal number.
Jordan totient function Jk(n)
.
Count the number of labeled, rooted forests with n
nodes.
Count the number of labeled, rooted trees with n
nodes.
Find the least common multiple of a list of integers.
Find the least commom multiple of two integers.
Find the lpf(n)
or least prime factor.
Calculate the Legendre Symbol of (a/p)
, where p
is prime.
Determine the number of digits, or length, of a number n
in base b
.
Find the n
-th Lucas Number.
Generate the first n
Lucky Numbers.
The classical Möbius function μ(n)
.
Calculate the n
-th Motzkin number.
Determine the number of subsets of n
of k
elements. Also written nCr
.
Carry forward calculation of the next digit of Pi.
Apply a number theoretic property test to integers to find the next number in a sequence.
Find the nearest integer n
th root of x
, such that root^n <= x
.
Find the nearest integer n
th root of x
, such that root^n <= x
.
Generalized floating point nth root, from
Calculate ω(n)
- the number of distinct prime factors of n
.
Count the number of ordered factorizations of n
.
Count the number of partitions of a set into any number of ordered lists.
Find the p-adic valuation of n
.
Count the maximum number of pieces that can be made from n
cuts of a disk.
Count the number of partitions of n
.
Count the number of ways n
can be partitioned into the sum of two squares.
Find the Pell Number for n
.
Find the n
-th pentagonal number.
Count the number of perfect partitions of n
.
Count the number of planar partitions with sum n
.
Count the number of planted 3-trees of height < n
.
Integer exponentiation, x^y
.
Integer power/exponentiation in Modular Arithmetic.
Count the exponents of the prime factors of n
.
Decompose an integer to prime factors.
Count the number of primes less than or equal to n
.
Find the product of the exponents of the prime factors of n
.
Find the radical of an integer n
.
Calculate the Ramanujan Tau function for n
.
Find the prime factors of n
as the factors to a power.
Remove all occurances of one or more digits from n
.
Calculate the n
th Repunit, or R_n
.
Reverse the digits of n
.
Caculate the rising factorial n^(m)
.
The number of unlabeled, or planted, trees with n
nodes.
Enumerate all of the rotations of n
.
Find the n
th Schröder number.
Calculate the sigma-1 (or σ1(n)
), also known as sum-of-divisors of an integer.
Calculate a sigma function of an integer, for any p
-th powers.
Find the n
-th square pyramidal number.
Create a Kolakoski Sequence over the default alphabet of [1, 2].
Find the n
th term of Stern's diatomic series.
Find the Stirling partition number (or Stirling number of the second kind) {n, k}
.
The tau (number of divisors) function.
Find the n
-th tetrahedral number.
Convert a decimal integer into another base.
Count the number of total, or series reduced tree, partitions of n
elements.
Euler's totient function for n
.
Find the triangle or triangular number of n
.
Find the triangle row and offset for the n
th item in a triangle.
Calculate the row in which the n
-th element would be in an element triangle.
Count the number of bracelet permutations for n
beads of two colors.
Count the number of bracelet permutations for n
beads, with primitive period of n
, with two colors.
Calculate the Wedderburn-Etherington number for n
.
Link to this section Functions
Count the number of Abelian groups of order n
.
An Abelian group is a commutative group of elements in Abstract Algebra; this function counts the number of Abelian groups of a certain size.
This implementation of size counting for Abelian groups of order n
is based on finding
the number of partitions (see partition_count/1
) of the exponents of the prime factors
of n
. For instance, when n
is 144
, the prime factorization is 2^4 * 3^2
, with exponents 4
and 2
. Finding the product of the partitions of the exponents via p(4) * p(2)
yields 5 * 2
, or 10
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000688/1
- Number of Abelian groups of ordern
Examples
iex> Math.abelian_groups_count(1)
1
iex> Math.abelian_groups_count(9984)
22
Find the Aliquot Sum of n
.
An Aliquot Sum of an integer n
is the sum of
the proper divisors (all divisors except n
) of n
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001065/1
- Aliquot sum ofn
Examples
iex> Math.aliquot_sum(1)
0
iex> Math.aliquot_sum(10)
8
iex> Math.aliquot_sum(48)
76
Calculate the Bell Number of n
, or the number of possible partitions of a set of size n
.
This function implementation relies on caching for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000110/1
- Bell or exponential numbers
Examples
iex> Math.bell_number(3)
5
iex> Math.bell_number(10)
115975
iex> Math.bell_number(15)
1382958545
iex> Math.bell_number(35)
281600203019560266563340426570
Calculate the n
-th Bernoulli number, and return it as a Fraction.
Bernoulli numbers are used throughout number theory for analysis, series construction,
and topology. While the odd Bernoulli numbers greater than B_1
are technically 0
,
this function returns a zero value fraction (the reduced value 0/1
).
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a027642/1
- Denominator of Bernoulli numbers
Examples
iex> Math.bernoulli_number(1)
%Fraction{num: 1, den: 2}
iex> Math.bernoulli_number(4)
%Fraction{num: -1, den: 30}
iex> Math.bernoulli_number(7)
%Fraction{num: 0, den: 1}
iex> Math.bernoulli_number(8)
%Fraction{num: -1, den: 30}
iex> Math.bernoulli_number(20)
%Fraction{num: -174611, den: 330}
Calculate Ω(n)
- number of distinct prime factors, with multiplicity.
See also omega/1
- number of distinct prime factors.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001222/1
- Number of prime divisors ofn
counting multiplicity
Examples
iex> Math.bigomega(3)
1
iex> Math.bigomega(15)
2
iex> Math.bigomega(25)
2
iex> Math.bigomega(99960)
8
Find the number of ways to partition 2 * n
into powers of 2
.
As this function is highly recursive, and uses a recurrence relationship, it uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000123/1
- Number of binary partitions
Examples
iex> Math.binary_partitions_count(1)
2
iex> Math.binary_partitions_count(5)
14
iex> Math.binary_partitions_count(12)
94
iex> Math.binary_partitions_count(37)
3074
Calculate the binomial coefficient (n k).
The binomial coefficient function determines the coefficient on the x^k
term in the polynomial expansion
of (1 + x)^n
.
Rather than run a full expansion, this function relies on the simple formula:
As the factorial/1
function in Chunky.Math uses a cached speed up strategy, the calculation of the
binomial by this method is fairly efficient.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000984/1
- Central binomial coefficients
Examples
iex> Math.binomial(7, 3)
35
iex> Math.binomial(20, 3)
1140
iex> Math.binomial(20, 10)
184756
iex> Math.binomial(100, 50)
100891344545564193334812497256
Find the Catalan number of n
, C(n)
.
In combinatorial math, the Catalan numbers occur in a wide range of counting problems.
Rather than the factorial or binomial expansion, this implementation uses a product over fractional parts to avoid recursion and precision loss.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000108/1
- Catalan NumbersChunky.Sequence.OEIS.Combinatorics.create_sequence_a159981/1
- Catalan numbers read modulo 4Chunky.Sequence.OEIS.Combinatorics.create_sequence_a159984/1
- Catalan numbers read modulo 5Chunky.Sequence.OEIS.Combinatorics.create_sequence_a159986/1
- Catalan numbers read modulo 7Chunky.Sequence.OEIS.Combinatorics.create_sequence_a159987/1
- Catalan numbers read modulo 8Chunky.Sequence.OEIS.Combinatorics.create_sequence_a159988/1
- Catalan numbers read modulo 11Chunky.Sequence.OEIS.Combinatorics.create_sequence_a159989/1
- Catalan numbers read modulo 12Chunky.Sequence.OEIS.Combinatorics.create_sequence_a289682/1
- Catalan numbers read modulo 16
Examples
iex> Math.catalan_number(2)
2
iex> Math.catalan_number(20)
6564120420
iex> Math.catalan_number(100)
896519947090131496687170070074100632420837521538745909320
iex> Math.catalan_number(256)
1838728806050447178945542295919013188631170099776194095631629802153953581076132688111479765113051517392441367036708073775588228430597313880732554755142
Calculate Cayley's formula for n
- the number of trees on n
labeled vertices.
This formula also works for:
- number of spanning trees of a complete graph with labeled vertices
- number of transitive subtree acyclic digraphs on n-1 vertices
- counts parking functions
- the number of nilpotent partial bijections (of an n-element set)
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000272/1
- Number of trees onn
labeled nodes
Examples
iex> Math.cayley_number(1)
1
iex> Math.cayley_number(5)
125
iex> Math.cayley_number(18)
121439531096594251776
iex> Math.cayley_number(37)
7710105884424969623139759010953858981831553019262380893
Calculate Chebyshev's triangle of coefficients at S(n, k)
.
The coefficient triangle is used for diophantine polynomial analysis, spherical harmonics, series analysis, and other number theoretic applications.
While a recurrence relationship exists, this function uses a binomial expansion to find values.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a049310/1
- Triangle of coefficients of Chebyshev's polynomials
Examples
iex> Math.chebyshev_triangle_coefficient(0, 0)
1
iex> Math.chebyshev_triangle_coefficient(4, 2)
-3
iex> Math.chebyshev_triangle_coefficient(9, 4)
0
iex> Math.chebyshev_triangle_coefficient(11, 7)
36
Check if a number n
contains the number m
in it's decimal
digit representation.
Examples
iex> Math.contains_number?(34, 3)
true
iex> Math.contains_number?(2048, 20)
true
iex> Math.contains_number?(2048, 49)
false
Find all positive coprimes of n
, from 2
up to n
.
Two numbers a
and b
are coprime if, and only if, the only positive integer factor
that divides both of them is 1
.
If you need comprimes of n
greater than n
, see coprimes/2
. If you only need the
count of coprimes of n
, see totient/1
.
Examples
iex> Math.coprimes(2)
[1]
iex> Math.coprimes(3)
[1, 2]
iex> Math.coprimes(10)
[1, 3, 7, 9]
iex> Math.coprimes(36)
[1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35]
Find all positive coprimes of n
from 2
up to d
.
Two numbers a
and b
are coprime if, and only if, the only positive integer factor
that divides both of them is 1
.
If you only need the coprimes of n
less than or equal to n
, see coprimes/1
.
Examples
iex> Math.coprimes(2, 10)
[1, 3, 5, 7, 9]
iex> Math.coprimes(3, 20)
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20]
iex> Math.coprimes(10, 30)
[1, 3, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29]
iex> Math.coprimes(38, 50)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
Find the number of derangements of a set of size n
.
A derangement of a set is a permutation of the set, such that no element is in its original position. Also called the subfactorial of n, the recontres number, or de Montmor number.
This implementation uses the Euler recurrence, a(n) = n * a(n - 1) + -1^n
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000166/1
- Derangement, or recontres number
Examples
iex> Math.derangement_count(1)
0
iex> Math.derangement_count(8)
14833
iex> Math.derangement_count(17)
130850092279664
iex> Math.derangement_count(134)
733162663744579191293964143415001307906325722892139819974619962654978249255036185299413091417144999745154783570225783145979302466795277487832988219926200862943908125847693470304687165754228414941338831577093697357593753008645129
Count the number of specific digits in n
.
Using the base
option, you can select which base the number is converted to
before counting digits.
The list of digits being counted can have one or more integers, allowing flexible counting of different combinations of digits (see examples).
Options
base
- Integer. Default10
. Numeric base to convertn
to before counting.
Examples
Count how many 2
s are in a number:
iex> Math.digit_count(200454232, [2])
3
Count the even digits in a number:
iex> Math.digit_count(123456789, [2, 4, 6, 8])
4
Count the 1
s and 2
s in the ternary expansion (base 3) of a number:
iex> Math.digit_count(245_987_340, [1, 2], base: 3)
12
Count the number of 25
s in the base 60 expansion of a number:
iex> Math.digit_count(1173840858356, [25], base: 60)
2
Break apart n
into runs of digits.
Optionally specify the base (default 10) in which to expand the number n
. A run
of digits is any grouping of identical digits. The groups of digits are returned
as lists, so the final result is a list of lists.
Options
base
- Integer. Default10
.
Examples
iex> Math.digit_runs(1233455)
[[1], [2], [3, 3], [4], [5, 5]]
iex> Math.digit_runs(847, base: 2)
[ [1, 1], [0], [1], [0, 0], [1, 1, 1, 1]]
iex> Math.digit_runs(1442792515, base: 16)
[ [5, 5], [15, 15], [4, 4, 4], [3]]
iex> Math.digit_runs(614482, base: 30)
[ [22, 22, 22, 22] ]
iex> Math.digit_runs(-100200)
[ [1], [0, 0], [2], [0, 0]]
Count the number of digit runs in n
.
Optionally specify the base (default 10) in which to expand n
. See digit_runs/2
for more
details on how digit runs are constructed.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a005811/1
- Number of runs in binary expansion ofn
Examples
iex> Math.digit_runs_count(1233455)
5
iex> Math.digit_runs_count(54321, base: 2)
9
iex> Math.digit_runs_count(1234567890987654321, base: 8)
18
Calculate the sum of the digits of n
.
Examples
iex> Math.digit_sum(1234)
10
iex> Math.digit_sum(2048)
14
iex> Math.digit_sum(1234567890987654321)
90
Generate n
digits of pi, as a single large integer.
This function uses a non-digit extraction version of Bailey-Borwein-Plouffe summation for generating accurate digits of Pi in base 10. This uses a summation over fractional values to maintain precision:
Using this formula, it is possible to create many hundreds of digits of Pi in less than a second. Generating 5,000 digits takes roughly 30 seconds.
Examples
iex> Math.digits_of_pi(3)
314
iex> Math.digits_of_pi(31)
3141592653589793238462643383279
iex> Math.digits_of_pi(45)
314159265358979323846264338327950288419716939
Find all divisors of n
of the form mx + b
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a002654/1
- Number of ways of writingn
as a sum of at most two nonzero squares
Examples
iex> Math.divisors_of_form_mx_plus_b(4, 1, 5)
[1, 5]
iex> Math.divisors_of_form_mx_plus_b(4, 1, 45)
[1, 5, 9, 45]
iex> Math.divisors_of_form_mx_plus_b(4, 3, 4)
[]
iex> Math.divisors_of_form_mx_plus_b(4, 3, 9975)
[3, 7, 15, 19, 35, 75, 95, 175, 399, 475, 1995, 9975]
Calculate the double factorial of n
, n!!
.
The double factorial steps down by 2
each iteration, with 0!!
and 1!!
both equal to 1
. So
the double factorial of 5
is
5!! == 5 * 3 * 1 == 15
This function uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001147/1
- Double factorial of odd numbersChunky.Sequence.OEIS.Core.create_sequence_a006882/1
- Double factorials ofn
Examples
iex> Math.double_factorial(1)
1
iex> Math.double_factorial(5)
15
iex> Math.double_factorial(10)
3840
iex> Math.double_factorial(18)
185794560
iex> Math.double_factorial(29)
6190283353629375
Count the number of endofunctions (as endomorphisms) for a set of size n
.
This counts endofunctions as an endomorphism over the set of size n
, which is equivalent to n^n
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000312/1
- Number of endofunctions
Examples
iex> Math.endomorphism_count(0)
1
iex> Math.endomorphism_count(4)
256
iex> Math.endomorphism_count(40)
12089258196146291747061760000000000000000000000000000000000000000
iex> Math.endomorphism_count(123)
114374367934617190099880295228066276746218078451850229775887975052369504785666896446606568365201542169649974727730628842345343196581134895919942820874449837212099476648958359023796078549041949007807220625356526926729664064846685758382803707100766740220839267
Find the n
-th Euler number. Also written EulerE
.
This calculation of the n
-th Euler number is based on the Euler Polynomial:
E_n(1/2) * 2^n
such that the 6th Euler Number would be:
E_6(1/2) * 2^6
or -61
Examples
iex> Math.euler_number(0)
1
iex> Math.euler_number(3)
0
iex> Math.euler_number(6)
-61
iex> Math.euler_number(16)
19391512145
iex> Math.euler_number(64)
45358103330017889174746887871567762366351861519470368881468843837919695760705
Calculate the Euler polynomial E_m(x)
.
This calculate is based on the explicit double summation:
In this implementation the value of x
is always converted to a fraction before calculations
begin.
Examples
iex> Math.euler_polynomial(6, Fraction.new(1, 2))
%Chunky.Fraction{den: 4096, num: -3904}
iex> Math.euler_polynomial(6, 4) |> Fraction.get_whole()
1332
iex> Math.euler_polynomial(2, 15) |> Fraction.get_whole()
210
iex> Math.euler_polynomial(8, Fraction.new(1, 3))
%Chunky.Fraction{den: 1679616, num: 7869952}
Find the n
-th Euler zig number.
Values for this function are based on the relation of the zig numbers to Euler Numbers, of
the form ezig(n) = abs(EulerE(2n))
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000364/1
- Euler secant (or zig) numbers
Examples
iex> Math.euler_zig(0)
1
iex> Math.euler_zig(2)
5
iex> Math.euler_zig(10)
370371188237525
Calculate the Euler zig zag, or up/down, number for n
.
The zig zag set is used in combinatorics to count the size of alternating sets of permutations.
Other noted uses of the zig zag numbers (via OEIS A000111):
- Number of linear extensions of the "zig-zag" poset.
- Number of increasing 0-1-2 trees on n vertices.
- ... the number of refinements of partitions.
- For n >= 2, a(n-2) = number of permutations w of an ordered n-set
- The number of binary, rooted, unlabeled histories with n+1 leaves
As the calculation of the Euler Zig Zag is multiply recursive, this implementation uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000111/1
- Euler or up/down numbers
Examples
iex> Math.euler_zig_zag(1)
1
iex> Math.euler_zig_zag(10)
50521
iex> Math.euler_zig_zag(20)
370371188237525
iex> Math.euler_zig_zag(99)
45608516616801111821043829531451697185581949239892414478770427720660171869441004793654782298700276817088804993740898668991870306963423232
Calculate the Eulerian Number A(n, m)
, the number of permutations of the numbers 1 to n
in which exactly m
elements are greater than the previous element.
The Eulerian numbers form the Euler triangle:
1
1 1
1 4 1
1 11 11 1
1 26 66 26 1
1 57 302 302 57 1
Where n
is the row (starting at 1) and m
is the offset in the row (starting at 0). So the value 66
is at
row 5, offset 2:
iex> Math.eulerian_number(5, 2)
66
The sum of values at row n
is n!
This implementation of Eulerian Number calculation uses a recursive algorithm with caching for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a008292/1
- Triangle of Eulerian numbers
Examples
iex> Math.eulerian_number(5, 4)
1
iex> Math.eulerian_number(7, 4)
1191
iex> Math.eulerian_number(9, 3)
88234
iex> Math.eulerian_number(25, 13)
3334612565134607644610436
Extend a Kolakoski sequence by one iteration.
Each iteration of the sequence will add one, or more, elements to the sequence.
See start_kolakoski_sequence/1
and extend_kolakoski_sequence_to_length/2
.
Examples
iex> Math.start_kolakoski_sequence() |> Math.extend_kolakoski_sequence()
{[1], 1, {1, 2}}
Extend a Kolakoski sequence by successive iterations until the sequence is at least the given length.
As each iteration of the sequence will add one or more elements to the sequence, the best guarantee that can be made is that the newly extended sequence will have at least a certain number of elements.
Examples
iex> Math.start_kolakoski_sequence() |> Math.extend_kolakoski_sequence_to_length(23)
{[1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 1], 15, {1, 2}}
iex> {seq, _, _} = Math.start_kolakoski_sequence() |> Math.extend_kolakoski_sequence_to_length(26)
iex> length(seq)
27
Find all pairs of factors of n
, with or without duplicates.
This is a variant of the factors/1
function, in that it builds the full pairs
of factors of n
in tuple form.
Options
duplicates
- Boolean. Defaultfalse
. Iftrue
, include the ordered duplicates of factors (see examples)
Examples
iex> Math.factor_pairs(8)
[{1, 8}, {2, 4}]
iex> Math.factor_pairs(8, duplicates: true)
[{1, 8}, {2, 4}, {4, 2}, {8, 1}]
iex> Math.factor_pairs(84)
[1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84]
[{1, 84}, {2, 42}, {3, 28}, {4, 21}, {6, 14}, {7, 12}]
The factorial of n
, or n!
.
A factorial of n
is the product of n * (n - 1) * (n - 2) * ... 1
.
This implementation uses a cache to speed up efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000142/1
- Factorial ofn
Examples
iex> Math.factorial(4)
24
iex> Math.factorial(1)
1
iex> Math.factorial(10)
3628800
iex> Math.factorial(20)
2432902008176640000
iex> Math.factorial(100)
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Count the number of possible factorizations of n
.
This counts a number as a factor of itself, as well as multi-set factorizations. So 8
has 3
factorizations; 8
, 2*4
, and 2*2*2
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001055/1
- The multiplicative partition
Examples
iex> Math.factorization_count(1)
1
iex> Math.factorization_count(30)
5
iex> Math.factorization_count(286)
5
iex> Math.factorization_count(9984)
254
Factorize an integer into all divisors.
This will find all divisors, prime and composite, of an integer. The algorithm used for factorization is not optimal for very large numbers, as it uses a multiple pass calculation for co-factors and composite factors.
Example
iex> Math.factors(2)
[1, 2]
iex> Math.factors(84)
[1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84]
iex> Math.factors(123456)
[1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 192, 643, 1286, 1929, 2572, 3858, 5144, 7716, 10288, 15432, 20576, 30864, 41152, 61728, 123456]
Calculate the falling factorial (n)m
.
Also called the descending factorial, falling sequential product, or lower factorial, this is the polynomial expansion:
Examples
iex> Math.falling_factorial(4, 0)
1
iex> Math.falling_factorial(6, 3)
120
iex> Math.falling_factorial(8, 10)
0
iex> Math.falling_factorial(21, 7)
586051200
Compare two floating points number using an epsilon error boundary.
Example
iex> Math.floats_equal?(3.11, 3.1)
false
iex> Math.floats_equal?(3.11, 3.1, 0.05)
true
iex> Math.floats_equal?(104.9999999, 104.9999996)
true
Find the n
-th Fubini number, the number of ordered partitions of a set size n
.
The Fubini numbers are also useful as (via OEIS A000670):
- the number of preferential arrangements of n labeled elements
- the number of weak orders on n labeled elements
- the number of ways n competitors can rank in a competition, allowing for the possibility of ties
- the number of chains of subsets starting with the empty set and ending with a set of n distinct objects
- the number of 'factor sequences' of N for the case in which N is a product of n distinct primes
This implementation is recursive and relies on binomial/2
, so it uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000670/1
- Fubini numbers
Examples
iex> Math.fubini_number(0)
1
iex> Math.fubini_number(3)
13
iex> Math.fubini_number(19)
92801587319328411133
iex> Math.fubini_number(52)
11012069943086163504795579947992458193990796847590859607173763880168176185195
Find the bases for which n
is a Rhonda number.
Via OEIS:
An integer n is a Rhonda number to base b if the product of its digits in base b equals b*Sum of prime factors of n (including multiplicity).
Numbers can be Rhonda to more than one base, see OEIS A100988. By default the get_rhonda_to/1
function evaluates all bases from 4 to 500. You can specify an alternate set of bases with
the :bases
option.
Options
:bases
- List of Integer. Bases to evaluate.
Examples
iex> Math.get_rhonda_to(1000)
[16, 36]
iex> Math.get_rhonda_to(5670)
[36, 106, 108, 196]
iex> Math.get_rhonda_to(5670, bases: 100..150 |> Enum.to_list())
[106, 108]
Find the gpf(n)
or greatest prime factor.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a006530/1
- Greatest prime factor ofn
Examples
iex> Math.greatest_prime_factor(1)
1
iex> Math.greatest_prime_factor(39)
13
iex> Math.greatest_prime_factor(99973)
389
Find the Hamming Weight of n
in a specific numeric base.
By default, the Hamming Weight is calculated in Base 2.
Hamming weight, binary weight, population count, or (in binary) bit summation, is the number of symbols
in a given base representation of an integer that are not 0
. See Hamming Weight.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000120/1
- Number of 1's in binary expansion ofn
Examples
iex> Math.hamming_weight(29)
4
iex> Math.hamming_weight(29, 10)
2
iex> Math.hamming_weight(100)
3
iex> Math.hamming_weight(100, 10)
1
Does a list of numbers contain any subset that sums to n
?
The subset sum solution is an NP-Complete problem. For this
implementation a series of heuristics is used to quickly eliminate edge cases, and then an exponential time exact
combinatoric algorithm is used to search for solutions. This algoritm is a fast pass recursion - as soon as a
valid solution is found, the algorithm terminates. The best case running time is O(1)
, while the worst case (a full
search of all solutions, with no valid sum) is exponential to the size of the list of numbers, or approximates O(2^N*N)
.
Examples
iex> Math.has_subset_sum?([1, 2, 3, 5, 11], 9)
true
iex> Math.has_subset_sum?([1, 2, 3, 5, 11], 11)
true
iex> Math.has_subset_sum?([1, 2, 3, 5, 11], 23)
false
iex> Math.has_subset_sum?([-3, 2, 4, 11], -1)
true
iex> Math.has_subset_sum?([-3, 2, 4, 11], 14)
true
iex> Math.has_subset_sum?([-3, 2, 4, 11], 10)
true
Find the n
-th Hipparchus number.
Also known as Schröder–Hipparchus numbers, super-Catalan numbers, or the little Schröder numbers.
In combinatorics, the Hipparchus numbers are useful for (via Schröder–Hipparchus number on Wikipedia):
- The nth number in the sequence counts the number of different ways of subdividing of a polygon with
n + 1
sides into smaller polygons by adding diagonals of the original polygon. - The nth number counts the number of different plane trees with
n
leaves and with all internal vertices having two or more children. - The nth number counts the number of different ways of inserting parentheses into a sequence of
n
symbols, with each pair of parentheses surrounding two or more symbols or parenthesized groups, and without any parentheses surrounding the entire sequence. - The nth number counts the number of faces of all dimensions of an associahedron
Kn + 1
of dimensionn − 1
, including the associahedron itself as a face, but not including the empty set. For instance, the two-dimensional associahedron K4 is a pentagon; it has five vertices, five faces, and one whole associahedron, for a total of 11 faces.
Sometimes denoted by S(n)
, this implementation is based on the recurrence relationship:
(n+1) * S(n) = (6*n-3) * S(n-1) - (n-2) * S(n-2)
Because of the double recurrence, this implementation uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001003/1
- Schroeder's second problem
Examples
iex> Math.hipparchus_number(4)
45
iex> Math.hipparchus_number(10)
518859
iex> Math.hipparchus_number(36)
6593381114984955663097869
iex> Math.hipparchus_number(180)
104947841676596807726623444466946904465025819465719020148363699314181613887673617931952223933467760579812079483371393916388262613163133
Calculate the Hurwitz-Radon number for n
, the number of independent vector
fields on a sphere in n
-dimensional euclidean space.
See Vector fields on spheres for more information.
This function uses a set of 2-adic
calculations to compute n
in a closed form.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a003484/1
- Radon function
Examples
iex> Math.hurwitz_radon_number(9)
1
iex> Math.hurwitz_radon_number(32)
10
iex> Math.hurwitz_radon_number(288)
10
iex> Math.hurwitz_radon_number(9600)
16
Determine if the n-th root of a number is a whole integer, returning a boolean and the root value.
If the result n-th root is within epsilon
of a whole
integer, we consider the result an integer n-th root.
This calcualtion runs the fast converging n-th root at a higher
epsilon than it's configured to use for comparison and testing of the
result value.
Options
epsilon
- Float. Default1.0e-6
. Error bounds for calculating float equality.
Examples
iex> Math.integer_nth_root(27, 3)
{true, 3}
iex> Math.integer_nth_root(1234, 6)
{false, :no_integer_nth_root, 3.2750594908836885}
iex> Math.integer_nth_root(33_038_369_407, 5)
{true, 127}
Predicate version of integer_nth_root/3
- does x
have an integer n
-th root.
Examples
iex> Math.integer_nth_root?(27, 3)
true
iex> Math.integer_nth_root?(1234, 6)
false
iex> Math.integer_nth_root?(33_038_369_407, 5)
true
Find the number of involutions, or self-inverse permutations, on n
elements.
Also known as Permutation Involution.
This implementation is based on a recursive calculation, and so uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000085/1
- Number of self-inverse permutations on n letters
Examples
iex> Math.involutions_count(1)
1
iex> Math.involutions_count(10)
9496
iex> Math.involutions_count(100)
24053347438333478953622433243028232812964119825419485684849162710512551427284402176
iex> Math.involutions_count(234)
60000243887036070789348415368171135887062020098670503272477698436854394126572492217644586010812169497365274140196122299728842304082915845220986966530354668079910372211697866503760297656388279100434472952800147699927974040547172024320
Determine if an integer n
is b
-smooth, a composite of prime factors less than or equal to b
.
Numbers can be b
-smooth for any b
that is prime. For instance, the number 8
is 3-smooth, as
it's factors would be: 1^1 * 2^3 * 3^0
, whereas 15
is not 3-smooth, as it's factors would be
1^1 * 2^0 * 3^1 * 5^1
- it has prime factors whose value is greater than 3
.
Shortcut Functions
There are a collection of pre-defined predicate functions (see Chunky.Math.Predicates
) for checking b-smooth for the primes 3
to 23
:
- `is_3_smooth?/1`
- `is_5_smooth?/1`
- `is_7_smooth?/1`
- `is_11_smooth?/1`
- `is_13_smooth?/1`
- `is_17_smooth?/1`
- `is_19_smooth?/1`
- `is_23_smooth?/1`
Examples
iex> Math.is_b_smooth?(1944, 3)
true
iex> Math.is_b_smooth?(101, 5)
false
iex> Math.is_b_smooth?(705, 47)
true
Determine if two numbers, a
and b
, are co-prime.
From Wikipedia:
In number theory, two integers a and b are said to be relatively prime, mutually prime,[1] or coprime (also written co-prime) if the only positive integer (factor) that divides both of them is 1
Examples
iex> Math.is_coprime?(14, 15)
true
iex> Math.is_coprime?(14, 21)
false
iex> Math.is_coprime?(17, 2048)
true
Is n
a cyclops number in base b
?
A cyclops number in a given base has exactly one 0
in its representation, in the exact middle
of the number, with an equal number of digits on each side. This implies that there must
be an odd number of digits.
The provided number n
is converted from base 10 to base b
before being evaluated.
Examples
iex> Math.is_cyclops_number_in_base?(119, 2)
true
iex> Math.is_cyclops_number_in_base?(2146, 5)
true
iex> Math.is_cyclops_number_in_base?(451, 8)
true
iex> Math.is_cyclops_number_in_base?(68413345263, 16)
true
iex> Math.is_cyclops_number_in_base?(956966581810, 60)
true
Check if n
is an Euler-Jacobi pseudo-prime to base a
.
These numbers are like Euler pseudo-primes, but with a stricter congruence:
where is the Jacobi symbol (see
jacobi_symbol/1
).
Examples
iex> Math.is_euler_jacobi_pseudo_prime?(91, 10)
true
iex> Math.is_euler_jacobi_pseudo_prime?(52633, 12)
true
iex> Math.is_euler_jacobi_pseudo_prime?(15, 16)
true
iex> Math.is_euler_jacobi_pseudo_prime?(169, 22)
true
iex> Math.is_euler_jacobi_pseudo_prime?(133, 102)
true
Check if n
is an Euler pseudo-prime in base a
.
Euler pseudo-primes are similar to the more standard definition of Euler-Jacobi pseudo-primes, but only need to
satisfy the more permissive assertion that if a
and n
are coprime:
See also is_euler_pseudo_prime?/1
for implicit base 10 check, or is_euler_jacobi_pseudo_prime?/2
for the more
commonly accepted pseudo-prime check.
Examples
iex> Math.is_euler_pseudo_prime?(185, 5)
false
iex> Math.is_euler_pseudo_prime?(185, 6)
true
iex> Math.is_euler_pseudo_prime?(91, 9)
true
iex> Math.is_euler_pseudo_prime?(1105, 16)
true
Check if n
is a valid number in base b
.
A number n
that contains only valid digits in base b
will be considered
to be a valid number in that base. This test assumes that the value being
provided is already in base b
.
If the value being tested is a number, this will only check number up to base 10. To
check bases above 10, provide a list of digits, like [10, 17, 1, 29]
(285359
in base 30).
Examples
iex> Math.is_in_base?(123456, 5)
false
iex> Math.is_in_base?(101011101, 2)
true
iex> Math.is_in_base?(2430432, 6)
true
iex> Math.is_in_base?([1, 17, 4, 10], 17)
false
iex> Math.is_in_base?([1, 17, 4, 10], 18)
true
A narcissistic number n
of length k
is equal to the sum of the digits of n
to the k
-th power.
For instance in base 10, the number 153
is of length 3
, and is a narcissistic number, because 1^3 + 5^3 + 3^3 = 153
.
In base 4, the number 28
is narcissistic, as the base 4 representation is 130
, and 1^3 + 3^3 + 0^3 = 28
.
Narcissistic numbers are also called Armstrong numbers, or plus-perfect numbers.
OEIS References:
- A161953 - Base-16 Armstrong
- A161952 - Base-15 Armstrong
- A161951 - Base-14 Armstrong
- A161950 - Base-13 Armstrong
- A161949 - Base-12 Armstrong
- A161948 - Base-11 Armstrong
- A005188 - Armstrong numbers
- A010353 - Base-9 Armstrong numbers
- A010354 - Base-8 Armstrong numbers
- A010350 - Base-7 Armstrong numbers
- A010348 - Base-6 Armstrong numbers
- A010346 - Base-5 Armstrong numbers
- A010344 - Base-4 Armstrong numbers
- A114904 - Sorted numbers of digits of any base-10 narcissistic number
- A014576 - Smallest n-digit narcissistic numbers
See also:
Examples
iex> Math.is_narcissistic_in_base?(370, 10)
true
iex> Math.is_narcissistic_in_base?(1741725, 10)
true
iex> Math.is_narcissistic_in_base?(243, 4)
true
iex> Math.is_narcissistic_in_base?(2292, 6)
true
iex> Math.is_narcissistic_in_base?(432, 8)
true
iex> Math.is_narcissistic_in_base?(403584751, 9)
true
Determine if n
is a value of the form mx + b
or mk + b
, for specific
values of m
and b
.
This function checks if an integer n
is of a specific form, and is not
an interpolation of the line formula.
Examples
Check if numbers are of the form 4k + 3
:
iex> Math.is_of_mx_plus_b?(4, 3, 1)
false
iex> Math.is_of_mx_plus_b?(4, 3, 27)
true
iex> Math.is_of_mx_plus_b?(4, 3, 447)
true
Check if n
is palindromic in base b
.
The number n
is converted from base 10 to base b
before being checked as a palindrome.
Examples
iex> Math.is_palindromic_in_base?(27, 2)
true
iex> Math.to_base(27, 2)
11011
iex> Math.is_palindromic_in_base?(105, 20)
true
iex> Math.to_base(105, 20)
[5, 5]
iex> Math.is_palindromic_in_base?(222, 3)
false
iex> Math.to_base(222, 3)
22020
Determine if n
is pandigital in base b
.
A number n
is pandigital when it contains all of the digits used in its base
at least once. So in base 10 1234567888890
is pandigital, but 123456789
is not. The
number n
is treated as base 10, and converted to the base b
before being tested.
Examples
iex> Math.is_pandigital_in_base?(75, 4)
true
iex> Math.is_pandigital_in_base?(1182263086756, 5)
true
iex> Math.is_pandigital_in_base?(2048, 10)
false
Check if a number n
in numeric base b
is a plaindrome. A plaindrome has digits that
never decrease in value when read from left to right.
Examples
iex> Math.is_plaindrome_in_base?(123456, 10)
true
iex> Math.is_plaindrome_in_base?(11111, 10)
true
iex> Math.is_plaindrome_in_base?(111232, 10)
false
iex> Math.is_plaindrome_in_base?(9842, 3)
true
Check if n
is a power of m
.
This is partially the inverse of is_root_of?/2
.
Examples
iex> Math.is_power_of?(8, 2)
true
iex> Math.is_power_of?(243, 3)
true
iex> Math.is_power_of?(9, 2)
false
iex> Math.is_power_of?(2, 2)
true
iex> Math.is_power_of?(1, 17)
true
Determine if n
is a Fermat pseudo-prime to base a
.
The pseudo-primes, or Fermat pseudo-primes, define a relationship between coprimes n
and a
, where n
is a composite
number, and a^(n - 1) % n == 1
.
The pseudo-primes over base 2 are often called the Poulet numbers.
If n
is pseudo-prime to all bases a
that are coprime to n
, it is a Carmichael number.
OEIS References:
Chunky.Sequence.OEIS.Primes
- Fermat pseudo primes to base 2 through 100
Examples
iex> Math.is_pseudo_prime?(33, 10)
true
iex> Math.is_pseudo_prime?(17, 10)
false
iex> Math.is_pseudo_prime?(65, 12)
true
iex> Math.is_pseudo_prime?(27, 12)
false
iex> Math.is_pseudo_prime?(341, 60)
true
iex> Math.is_pseudo_prime?(291, 60)
false
Check if n
is a Rhonda number to the base b
.
Via OEIS:
An integer n is a Rhonda number to base b if the product of its digits in base b equals b*Sum of prime factors of n (including multiplicity).
Examples
iex> Math.is_rhonda_to_base?(1568, 10)
true
iex> Math.is_rhonda_to_base?(2048, 10)
false
iex> Math.is_rhonda_to_base?(855, 6)
true
iex> Math.is_rhonda_to_base?(47652, 9)
true
iex> Math.is_rhonda_to_base?(91224, 60)
true
Check if n
is any k
-th root of m
, where k > 2
.
This function uses a repeated multiplication method to test if n
has any
power k
such that n^k == m
.
Examples
iex> Math.is_root_of?(2, 8)
true
iex> Math.is_root_of?(2, 2048)
true
iex> Math.is_root_of?(7, 16807)
true
iex> Math.is_root_of?(5, 16808)
false
Find the n
-th coefficient of the q expansion of the modular J invariant function.
The Laurent series of the q-expansion begins:
This function finds the n
-th q
coefficient using a recursive relation to the sigma-5 and sigma-3
of components of the expansion.
Because this implementation is recursive, it uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000521/1
- Coefficients of modular functionj
Examples
iex> Math.j_invariant_q_coefficient(-1)
1
iex> Math.j_invariant_q_coefficient(10)
22567393309593600
iex> Math.j_invariant_q_coefficient(20)
189449976248893390028800
iex> Math.j_invariant_q_coefficient(121)
20834019715817024229638765444619811002731409879518705977860
Calculate the Jacobi Symbol (n/k)
.
Via Wikipedia Jacobi Symbol:
The Jacobi symbol is a generalization of the Legendre symbol. Introduced by Jacobi in 1837,[1] it is of theoretical interest in modular arithmetic and other branches of number theory, but its main use is in computational number theory, especially primality testing and integer factorization...
The value of n
can be any integer, while k
must be a positive and odd integer.
Examples
iex> Math.jacobi_symbol(13, 13)
0
iex> Math.jacobi_symbol(2, 7)
1
iex> Math.jacobi_symbol(156, 3)
0
iex> Math.jacobi_symbol(199, 213)
1
Find the n
-th Jacobsthal number.
These numbers are sometimes used in combinatorics for counting tiling variations, as well as having applications in number theory.
The Jacobsthal numbers are a recurrence relation similar to the Fibonacci numbers, following the pattern:
For this implementation, a closed form is used instead of a recurrence.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001045/1
- Jacobsthal Numbers
Examples
iex> Math.jacobsthal_number(0)
0
iex> Math.jacobsthal_number(10)
341
iex> Math.jacobsthal_number(100)
422550200076076467165567735125
iex> Math.jacobsthal_number(250)
603083798111021851164432213586916186735781170133544604372174916707880883541
Jordan totient function Jk(n)
.
The Jordan totient is a generalized form of the Euler totient function, where J1(n) = Φ(n)
. The
Jordan totient is a positive integer m
of k
-tuples that are co-prime to n
.
Calculating the totient is a semi-closed form of a Dirichlet series/Euler product, and is dependent
on the size of n
for factorization and k
for exponentiation.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001615/1
- Dedekindpsi
function
Examples
Finding J2(3)
:
iex> Math.jordan_totient(3, 2)
8
Finding J9(7)
:
iex> Math.jordan_totient(7, 9)
40353606
Finding J10(9999)
:
iex> Math.jordan_totient(9999, 10)
9989835316811664782653775044519099200000
Count the number of labeled, rooted forests with n
nodes.
A rooted forest will have at most one path between any two nodes, and the total number of
such forets with n
nodes is (n + 1)^(n - 1)
(a generalization of the Cayley formula).
Examples
iex> Math.labeled_rooted_forests_count(1)
1
iex> Math.labeled_rooted_forests_count(3)
16
iex> Math.labeled_rooted_forests_count(11)
61917364224
iex> Math.labeled_rooted_forests_count(32)
118558347188026655500106547231096910504441858017
Count the number of labeled, rooted trees with n
nodes.
A rooted tree will have exactly one path between any two nodes, and the total number of such
trees with n
nodes is n^(n - 1)
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000169/1
- Number of labeled rooted trees withn
nodes
Examples
iex> Math.labeled_rooted_trees_count(1)
1
iex> Math.labeled_rooted_trees_count(5)
625
iex> Math.labeled_rooted_trees_count(17)
48661191875666868481
iex> Math.labeled_rooted_trees_count(29)
88540901833145211536614766025207452637361
Find the least common multiple of a list of integers.
By definition LCM is a composable function, such that finding the least common
multiple of a list of integers [a, b, c, d]
is lcm(a, lcm(b, lcm(c, d)))
.
Example
iex> Math.lcm([3, 5, 7, 11, 13, 17])
255255
iex> Math.lcm([2, 4, 8])
8
iex> Math.lcm([1, 3, 27])
27
iex> Math.lcm([1, 3, 37, 0, 145])
0
Find the least commom multiple of two integers.
The LCM of two integers n
and m
is the smallest number b
that is divisible
by both n
and m
with no remainder - or satisfies the relationship b/n = 0, b/m = 0
.
Examples
iex> Math.lcm(2, 3)
6
iex> Math.lcm(10, 5)
10
iex> Math.lcm(17, 29)
493
iex> Math.lcm(14, 230)
1610
iex> Math.lcm(257, 0)
0
Find the lpf(n)
or least prime factor.
OEIS References:
Examples
iex> Math.least_prime_factor(1)
1
iex> Math.least_prime_factor(39)
3
iex> Math.least_prime_factor(99973)
257
Calculate the Legendre Symbol of (a/p)
, where p
is prime.
The Legendre symbol is used in number theory when working with prime numbers and quadratic constructions. It was originally defined via the formula:
Examples
iex> Math.legendre_symbol(12, 3)
0
iex> Math.legendre_symbol(14, 11)
1
iex> Math.legendre_symbol(29, 31)
-1
iex> Math.legendre_symbol(14, 9)
** (ArgumentError) p must be prime
Determine the number of digits, or length, of a number n
in base b
.
Examples
iex> Math.length_in_base(12345, 10)
5
iex> Math.length_in_base(2048, 2)
12
iex> Math.length_in_base(123456789, 60)
5
Find the n
-th Lucas Number.
The Lucas Number is a recursive sequence, similar to the Fibonacci sequence, with alternative starting values. The successive values in the Lucas sequence form a ratio that approaches the Golden Ratio.
This implementation uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000032/1
- Lucas numbers beginning at2
Chunky.Sequence.OEIS.Core.create_sequence_a000204/1
- Lucas numbers beginning at1
Examples
iex> Math.lucas_number(4)
7
iex> Math.lucas_number(203)
2657608295638762232902023676028758508503879
Generate the first n
Lucky Numbers.
The Lucky Numbers are generated as a sequential sieve, like the
prime Sieve of Eratosthenes. This makes generating the n
th term as a digit extraction of negligble utility, as
it would require generating the preceding terms as part of the sieve process.
Instead, this function takes advantage of the fact that the ratio of numbers before and after sieving grows at
approximately the natural log of the size of the starting list. I.e., if we want n
lucky numbers, we need a
starting list of approximately n * log(m)
integers. We can solve for m
via a fast gradient. This will generally
result in calculating more digits than necessary, but only by a small margin - extra digits are truncated in the
returned list.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000959/1
- Lucky numbers
Examples
iex> Math.lucky_numbers(5)
[1, 3, 7, 9, 13]
iex> Math.lucky_numbers(10)
[1, 3, 7, 9, 13, 15, 21, 25, 31, 33]
iex> Math.lucky_numbers(20)
[1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79]
iex> Math.lucky_numbers(30)
[1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, 87, 93, 99, 105, 111, 115, 127, 129, 133, 135]
The classical Möbius function μ(n)
.
From Möbius Function on Wikipedia:
For any positive integer n, define μ(n) as the sum of the primitive nth roots of unity. It has values in {−1, 0, 1} depending on the factorization of n into prime factors:
- μ(n) = 1 if n is a square-free positive integer with an even number of prime factors.
- μ(n) = −1 if n is a square-free positive integer with an odd number of prime factors.
- μ(n) = 0 if n has a squared prime factor.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a008683/1
- Moebius function
Examples
iex> Math.mobius_function(1)
1
iex> Math.mobius_function(24)
0
iex> Math.mobius_function(99999)
0
Calculate the n
-th Motzkin number.
In combinatorics, number theory, and geometry, the Motzkin number is used to find (via Wikipedia and OEIS A001006):
- the number of different ways of drawing non-intersecting chords between
n
points on a circle - the number of routes on the upper right quadrant of a grid from coordinate (0, 0) to coordinate (
n
, 0) inn
steps if one is allowed to move only to the right (up, down or straight) - the number of involutions of {1,2,...,
n
} having genus 0 - a wide variety of limits in sequence combinatorics and sub-sequence generation
Motzkin numbers, for this implementation, are found via binomials (see binomial/2
) and Catalan numbers (see catalan_number/1
):
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001006/1
- Motzkin numbers
Examples
iex> Math.motzkin_number(1)
1
iex> Math.motzkin_number(15)
310572
iex> Math.motzkin_number(57)
5127391665653918424581931
iex> Math.motzkin_number(132)
906269136562156220773088044844995547011445535121944413744427
Determine the number of subsets of n
of k
elements. Also written nCr
.
Also describes Pascals triangle by (row, offset), as well as the binomial expansion (n/k)
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a007318/1
- Pascal's triangle
Examples
iex> Math.n_choose_k(5, 3)
10
iex> Math.n_choose_k(10, 4)
210
iex> Math.n_choose_k(25, 2)
300
iex> Math.n_choose_k(50, 14)
937845656300
Carry forward calculation of the next digit of Pi.
The next_digit_of_pi/0
and next_digit_of_pi/1
functions provide a digit-at-a-time
iterative generation of digits of Pi, accurate to at least 3,000 digits. This is useful
for on demand generation of digits, but it does require a carry forward state value.
Use like:
{digit_0, carry} = next_digit_of_pi()
{digit_1, carry} = next_digit_of_pi(carry)
{digit_2, carry} = next_digit_of_pi(carry)
...
This version of the Pi digit generation function will likely be updated in a future release to use a base-16 algorithm that is accurate for a larger number of digits.
See next_digit_of_pi/0
.
Apply a number theoretic property test to integers to find the next number in a sequence.
Examples
iex> Math.next_number(&Math.Predicates.is_powerful_number?/1, 49)
64
iex> Math.next_number(&Math.Predicates.is_abundant?/1, 60)
66
Find the nearest integer n
th root of x
, such that root^n <= x
.
This is an iterative root method that bypasses any floating point operations, so is suitable
for finding large integer roots. For numbers less than 2^64-1
the nth_root
method may be
faster.
The return value of nth_integer_root
is a tuple of either {:exact, value}
or {:nearest, value}
depending on the root. To get an immediate value, see nth_integer_root!/2
.
Examples
iex> a = 1234567890987654321
iex> Math.nth_integer_root(a * a * a * a * a, 5)
{:exact, 1234567890987654321}
iex> Math.nth_integer_root(100_000, 4)
{:nearest, 17}
iex> Math.nth_integer_root(8, 3)
{:exact, 2}
Find the nearest integer n
th root of x
, such that root^n <= x
.
This is an iterative root method that bypasses any floating point operations, so is suitable
for finding large integer roots. For numbers less than 2^64-1
the nth_root
method may be
faster.
The return value of nth_integer_root
is an integer that is the exact, or nearest, n
th root
of x
. If you need to know if the root is exact or nearest, see nth_integer_root/2
.
Examples
iex> a = 1234567890987654321
iex> Math.nth_integer_root!(a * a * a * a * a, 5)
1234567890987654321
iex> Math.nth_integer_root!(100_000, 4)
17
iex> Math.nth_integer_root!(8, 3)
2
Generalized floating point nth root, from:
https://github.com/acmeism/RosettaCodeData/blob/master/Task/Nth-root/Elixir/nth-root.elixir
based on a fast converging Newton's Method process.
Note: Because the nth_root
function is based on floating point operations, it will lose
precision and introduce error for integers larger than ~16 digits
Options
precision
- Float. Default1.0e-7
. Precision to which root is calculated.
Examples
iex> Math.nth_root(8, 3)
2.0
iex> Math.nth_root(27, 3)
3.0
iex> Math.nth_root(78125, 7)
5.0
iex> Math.nth_root(104, 3)
4.702669375441515
Calculate ω(n)
- the number of distinct prime factors of n
.
See also bigomega/1
- number of total prime factors of n
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001221/1
- Number of distinct primes dividingn
Examples
iex> Math.omega(3)
1
iex> Math.omega(15)
2
iex> Math.omega(25)
1
iex> Math.omega(99960)
5
Count the number of ordered factorizations of n
.
Also called the Hille function, or Kalmár's problem, this counts all possible factorizations (not all necessarily prime)
of n
, regardless of order. So 10
has 3 factorizations, 2x5
, 5x2
, and 10
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a074206/1
- Kalmar's problem: number of ordered factorizations ofn
Examples
iex> Math.ordered_factorization_count(1)
1
iex> Math.ordered_factorization_count(8)
4
iex> Math.ordered_factorization_count(25)
2
iex> Math.ordered_factorization_count(104)
20
iex> Math.ordered_factorization_count(3648)
2496
Count the number of partitions of a set into any number of ordered lists.
Also known as the sum of all sizes of k-subsets of original set of size n
.
This implementation is based on a recurrence relation:
A(n) = (2 * n - 1) * A(n - 1) - (n - 1) * (n - 2) * A(n - 2)
As this is a highly recursive relation, a cache is used for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000262/1
- Number of sets of lists
Examples
iex> Math.ordered_subsets_count(1)
1
iex> Math.ordered_subsets_count(3)
13
iex> Math.ordered_subsets_count(11)
824073141
iex> Math.ordered_subsets_count(30)
197987401295571718915006598239796851
Find the p-adic valuation of n
.
From p-adic order on Wikipedia:
In number theory, for a given prime number p, the p-adic order or p-adic valuation of a non-zero integer n is the highest exponent
ν
such thatp^ν
dividesn
.
The p
value for p_adic_valuation
must be prime. By defintion the p-adic
value of 0
is
always infinity.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001511/1
- The ruler function
Examples
A handful of examples for 3-adic
, 5-adic
, and 7-adic
valuation, though any prime number
can be used as the p
value:
2-adic valutions:
iex> Math.p_adic_valuation(2, 1)
0
iex> Math.p_adic_valuation(2, 24)
3
iex> Math.p_adic_valuation(2, 9728)
9
3-adic valutions:
iex> Math.p_adic_valuation(3, 137)
0
iex> Math.p_adic_valuation(3, 999)
3
7-adic valutions
iex> Math.p_adic_valuation(7, 686)
3
iex> Math.p_adic_valuation(7, 980)
2
Count the maximum number of pieces that can be made from n
cuts of a disk.
Also called the Central Polygonal Numbers, Pizza Numbers, or the Lazy Caterer's Sequence.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000123/1
- Lazy caterer's sequence
Examples
iex> Math.pancake_cut_max(1)
2
iex> Math.pancake_cut_max(3)
7
iex> Math.pancake_cut_max(7)
29
iex> Math.pancake_cut_max(24)
301
Count the number of partitions of n
.
A partition of n
is the set of ways of creating a sum of n
. For example, 4
has a partition
count of 5
, as it can be represented as the following sums:
4
3 + 1
2 + 2
2 + 1 + 1
1 + 1 + 1 + 1
This is a recursive form of the Partition Function, yielding an exact answer, but computationally
intensive for larger numbers. Because this function is exponentially recursive, it uses a value
cache that persists as a named Agent, which is used by any call to partition_count
. On a reasonably
fast computer this results in the following execution times for different values of n
:
n | Seconds |
---|---|
10 | 0.021 |
100 | 0.071 |
1000 | 7.301 |
2500 | 43.616 |
3000 | 61.921 |
5000 | 185.277 |
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000041/1
- Partitions of integern
Examples
iex> Math.partition_count(1)
1
iex> Math.partition_count(10)
42
iex> Math.partition_count(100)
190569292
iex> Math.partition_count(416)
17873792969689876004
Count the number of ways n
can be partitioned into the sum of two squares.
For the purposes of this function, the partition count for 0
and 1
is 1
. For
all cases, 1
counts as a square number.
As examples:
5
has one partition,1^2 + 2^2
25
has two partitions,3^2 + 4^2
and0^2 + 5^2
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000161/1
- Number of partitions ofn
into 2 squaresChunky.Sequence.OEIS.Core.create_sequence_a001481/1
- Numbers that are the sum of two squares
Examples
iex> Math.partitions_into_two_squares(20)
1
iex> Math.partitions_into_two_squares(300)
0
iex> Math.partitions_into_two_squares(986)
2
iex> Math.partitions_into_two_squares(9945)
4
Find the Pell Number for n
.
Pell numbers are an infinite sequence of integers that form the denominators of increasingly
accurate fractional representations of sqrt(2)
. See Pell Number on
Wikipedia or Pell Number on MathWorld.
Calculating the Pell numbers takes a similar recursive form to calculating the Fibonacci sequence:
Pell(n) = 2 * Pell(n - 1) + Pell(n - 2)
This implementation uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000129/1
- Pell numbers
Examples
iex> Math.pell_number(1)
1
iex> Math.pell_number(10)
2378
iex> Math.pell_number(67)
15646814150613670132332869
iex> Math.pell_number(123)
42644625325266431622582204734101084193553730205
Find the n
-th pentagonal number.
See Pentagonal number for a useful visualization of how pentagonal numbers grow.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000326/1
- Pentagonal numbers
Examples
iex> Math.pentagonal_number(0)
0
iex> Math.pentagonal_number(30)
1335
iex> Math.pentagonal_number(300)
134850
iex> Math.pentagonal_number(874)
1145377
Count the number of perfect partitions of n
.
A perfect partition of n
is a partition of n
such that any number from 1 to n
can
be uniquely generated using the values of the partition. Take, for example, the perfect
partition of 4
; {1, 1, 1, 1}
. In this case the base required value (n
copies of 1
)
is the only perfect partition. The partition {2, 1, 1}
isn't part of the perfect partition
because the value 2
could be constructed in two different ways with those values ({2, _, _}
and {_, 1, 1}
).
When assessing a perfect partition, an intermediate value that can be constructed with the same
partition values multiple times is still a perfect partition. For instance, 5
has as one of its
valid perfect partitions {2, 2, 1}
. The value 3
can be constructed twice, as {2, _, 1}
and
{2, 1, _}
, but as both constructions use identical values (2 + 1
), this is still a perfect
partition.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a002033/1
- Number of perfect partitions ofn
Examples
iex> Math.perfect_partition_count(3)
2
iex> Math.perfect_partition_count(23)
20
iex> Math.perfect_partition_count(351)
112
iex> Math.perfect_partition_count(2345)
75
Count the number of planar partitions with sum n
.
Via Plane partition:
in combinatorics, a plane partition is a two-dimensional array of nonnegative integers
π{i,j}
(with positive integer indices i and j) that is nonincreasing in both indices.
The generalized formula for counting the number of plane partitions is
This implementation uses the recurrence relationship:
PL(n) = sum{1..n:k} PL(n - k) * sigma-2(k)
As this is a deeply recursive recurrence, this implementation uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000219/1
- Number of planar partitions ofn
Examples
iex> Math.plane_partition_count(1)
1
iex> Math.plane_partition_count(7)
86
iex> Math.plane_partition_count(13)
2485
iex> Math.plane_partition_count(34)
28175955
Count the number of planted 3-trees of height < n
.
Used in combinatoric calculations of tree rootings admitting trees of certain heights. The number of
planted 3 trees grows very quickly - planted_3_trees_count(20)
is a 50,084 digit number.
This function is recursive, and uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a006894/1
- Number of planted 3-trees of height <n
Examples
iex> Math.planted_3_trees_count(1)
1
iex> Math.planted_3_trees_count(3)
4
iex> Math.planted_3_trees_count(6)
2279
iex> Math.planted_3_trees_count(9)
5695183504492614029263279
Integer exponentiation, x^y
.
This function uses pure integer methods to bypass issues with floating point precision
trucation in large values using the built-in :math
exponentiation functions. For negative
exponents a Fraction will be returned.
For pure integer roots, see nth_root_int/2
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000079/1
- Powers of 2Chunky.Sequence.OEIS.Core.create_sequence_a000225/1
- Mersenne numbersChunky.Sequence.OEIS.Core.create_sequence_a000244/1
- Powers of 3Chunky.Sequence.OEIS.Core.create_sequence_a000290/1
- The squares,n^2
Example
iex> Math.pow(2, 10)
1024
iex> Math.pow(17, 14)
168377826559400929
iex> Math.pow(4, -3)
%Fraction{num: 1, den: 64}
Integer power/exponentiation in Modular Arithmetic.
Examples
iex> Math.pow(5, 3, 13)
8
iex> Math.pow(67930, 32319, 103969)
6582
Count the exponents of the prime factors of n
.
This function counts the exponents on the prime factors of n
, for example the
number 2,025,000
can be factored to: [2, 2, 2, 3, 3, 3, 3, 5, 5, 5, 5, 5]
or 2^3 * 3^4 * 5^5
, hence the exponent of 2
is 3
, the exponent of 3
is
4
, and the exponent of 5
is 5
.
As a simpler example, the prime factors of 49
are [7, 7]
, or 7^2
, so the
result of prime_factor_exponents(49)
would be %{7 => 2}
Examples
iex> Math.prime_factor_exponents(2)
%{2 => 1}
iex> Math.prime_factor_exponents(8)
%{2 => 3}
iex> Math.prime_factor_exponents(2025000)
%{2 => 3, 3 => 4, 5 => 5}
iex> Math.prime_factor_exponents(49)
%{7 => 2}
Decompose an integer to prime factors.
This is not an exhaustive factorization, but a reduction to all prime factors for an integer.
Examples
iex> Math.prime_factors(12)
[1, 2, 2, 3]
iex> Math.prime_factors(101)
[1, 101]
iex> Math.prime_factors(79170)
[1, 2, 3, 5, 7, 13, 29]
iex> Math.prime_factors(233*444*727*456)
[1, 2, 2, 2, 2, 2, 3, 3, 19, 37, 233, 727]
Count the number of primes less than or equal to n
.
Sometimes written pi(n)
or π(n)
, this is the prime counting function.
This impementation uses a summation over fractions of the sigma/1
function. If the
counting function needs to be applied over a sequence of numbers, it is more efficient
to use the OEIS A000720 sequence from Chunky.Sequences.OEIS.Core
, as it unrolls the
continued summation using historic values:
counter = Sequence.create(Sequence.OEIS.Core, :a000720)
Examples
iex> Math.prime_pi(1)
0
iex> Math.prime_pi(38)
12
iex> Math.prime_pi(945)
160
iex> Math.prime_pi(100000)
9592
Find the product of the exponents of the prime factors of n
.
This function takes the prime factors of n
, such as the factors of 8 = {1, 2, 2, 2}
,
groups the factors and to find the exponents, such as 8 = 1^1 * 2^3
, and then
finds the product of the exponents, like 1 * 3
. Here the product of
prime factorization exponents for 8
is 3
.
The numbers generated by this function are related to the OEIS Sequence A005361, and the prodex function.
Examples
iex> Math.product_of_prime_factor_exponents(8)
3
iex> Math.product_of_prime_factor_exponents(100000)
25
Find the radical of an integer n
.
Also called the square-free kernel, or written as rad(n)
, the radical of an integer is
the product of the distinct primes of n
.
Examples
iex> Math.radical(1)
1
iex> Math.radical(504)
42
iex> Math.radical(99960)
3570
Calculate the Ramanujan Tau function for n
.
The Ramanujan Tau function is defined as:
It's use in mathematics is noted by Wikipedia as
an "error term" involved in counting the number of ways of expressing an integer as a sum of 24 squares
When calculating the Nth term of the Ramanujan Tau, this function uses a summation form (developed in GP/Pari by Joerg Arndt), that looks like:
a(n) =
n^4 * sigma(n)
- 24 *
sum(
k = 1,
n - 1,
(
35 * k^4
- 52 * k^3 * n
+ 18 * k^2 * n^2
)
* sigma(k)
* sigma(n - k)
)
Note that the summation in sum(k = 1, n - 1, ...
is linear to the size of n
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000594/1
- Ramanujan's Tau function
Examples
iex> Math.ramanujan_tau(1)
1
iex> Math.ramanujan_tau(15)
1217160
iex> Math.ramanujan_tau(460)
-132549189534720
Find the prime factors of n
as the factors to a power.
While the prime_factors/1
function will return the full prime factorization
of n
as a list of all factors (such as [1, 2, 2, 3]
for factors of 12
), this
function returns a list of tuples, with each tuple containing the prime factor, and
the power of the prime factor, such as [{1, 1}, {2, 2}, {3, 1}]
for the prime
factors of 12
.
Examples
iex> Math.reduced_prime_factors(1)
[{1, 1}]
iex> Math.reduced_prime_factors(16)
[{2, 4}]
iex> Math.reduced_prime_factors(34560)
[{2, 8}, {3, 3}, {5, 1}]
iex> Math.reduced_prime_factors(30223017)
[{3, 3}, {11, 3}, {29, 2}]
Remove all occurances of one or more digits from n
.
Once removed, the the remaining digits of n
are reconstituted into a number. If
no digits are remaining then 0
(or a configurable value) is returned.
Examples
iex> Math.remove_digits!(123, [4, 5])
123
iex> Math.remove_digits!(123, [2])
13
iex> Math.remove_digits!(123, [1, 2, 3])
0
iex> Math.remove_digits!(123, [1, 2, 3], empty: nil)
nil
Calculate the n
th Repunit, or R_n
.
OEIS References:
Examples
iex> Math.repunit(0)
0
iex> Math.repunit(1)
1
iex> Math.repunit(5)
11111
iex> Math.repunit(10)
1111111111
iex> Math.repunit(25)
1111111111111111111111111
Reverse the digits of n
.
If the rotated digits of n
would have leading zeros, they are truncated.
Examples
iex> Math.reverse_number(12345)
54321
iex> Math.reverse_number(12300)
321
iex> Math.reverse_number(11)
11
Caculate the rising factorial n^(m)
.
Also called the Pochhammer function, Pochhammer polynomial, ascending factorial, or upper factorial, this is the polynomial expansion:
Examples
iex> Math.rising_factorial(3, 0)
1
iex> Math.rising_factorial(4, 3)
120
iex> Math.rising_factorial(7, 5)
55440
iex> Math.rising_factorial(11, 13)
7124122778572800
The number of unlabeled, or planted, trees with n
nodes.
Alternative definitions:
- Sometimes called Polya Trees
- Number of ways of arranging n-1 nonoverlapping circles
- Number of connected multigraphs of order n without cycles except for one loop
This function is highly recursive, and in this implementation uses a cache to increase efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000081/1
- Number of unlabeled rooted trees withn
nodes
Examples
iex> Math.rooted_tree_count(2)
1
iex> Math.rooted_tree_count(21)
35221832
iex> Math.rooted_tree_count(53)
10078062032127180323468
iex> Math.rooted_tree_count(150)
9550651408538850116424040916940356193332141892140610711711231180087
Enumerate all of the rotations of n
.
A rotate of n
involves a circular rotation of digits - the first digit moved to the end of the
number, repeated until all possible rotations are enumerated.
Examples
iex> Math.rotations(1234)
[1234, 2341, 3412, 4123]
iex> Math.rotations(232)
[232, 322, 223]
iex> Math.rotations(7)
[7]
iex> Math.rotations(123456)
[123456, 234561, 345612, 456123, 561234, 612345]
iex> Math.rotations(123123)
[123123, 231231, 312312]
iex> Math.rotations(1111)
[1111]
Find the n
th Schröder number.
The Schroder numbers describe the number of lattice paths across a grid from a south east corner to a north east corner, using only north, east, or north-east steps.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a006318/1
- Large Schroder numbers
Examples
iex> Math.schroder_number(0)
1
iex> Math.schroder_number(2)
6
iex> Math.schroder_number(6)
1806
iex> Math.schroder_number(10)
1037718
iex> Math.schroder_number(18)
600318853926
Calculate the sigma-1 (or σ1(n)
), also known as sum-of-divisors of an integer.
This is all of the divisors of n
summed.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000203/1
- Sum of divisors ofn
Chunky.Sequence.OEIS.Sigma
- Sigma sequences
Example
iex> Math.sigma(70)
144
iex> Math.sigma(408)
1080
iex> Math.sigma(100000)
246078
Calculate a sigma function of an integer, for any p
-th powers.
This is a generalized Sigma function of the form σp(n)
, so the Sigma-0 of
a number σ0(n)
would be sigma(n, 0)
, while the Sigma-4 (σ4(n)
) would be sigma(n, 4)
.
For a faster version of σ1(n)
(or the sum-of-divisors) see sigma/1
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000005/1
- Number of divisors ofn
Chunky.Sequence.OEIS.Core.create_sequence_a001157/1
- Sum of squares of divisors ofn
Examples
iex> Math.sigma(12, 2)
210
iex> Math.sigma(19, 4)
130322
iex> Math.sigma(24, 0)
8
Find the n
-th square pyramidal number.
The number of elements in a square stacked pyramid n
levels tall, or n x n
at the base.
Via Pyramidal square number on Wikipedia:
Square pyramidal numbers also solve the problem of counting the number of squares in an n × n grid
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000330/1
- Square pyramidal numbers
Examples
iex> Math.square_pyramidal_number(0)
0
iex> Math.square_pyramidal_number(20)
2870
iex> Math.square_pyramidal_number(147)
1069670
iex> Math.square_pyramidal_number(970)
304694945
Create a Kolakoski Sequence over the default alphabet of [1, 2].
A Kolakoski Sequence is a self-describing, Run Length Encoding over a specific alphabet of integers. The first values of the sequence are:
1,2,2,1,1,2,1,2,2,1,2,...
In the OEIS catalog, this is sequence A000002.
This sequence, unlike most others, does not extend by a single value at a time, rather by a length related to the size of the alphabet.
See also extend_kolakoski_sequence/1
and extend_kolakoski_sequence_to_size/2
for ways to
work with the sequence. The data returned by this function, and the other Kolakoski
functions, carries the calculated sequence, the iteration number, and the alphabet, all of which
are required for generating new values for the sequence.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000002/1
- Kolakoski Sequence
Examples
iex> Math.start_kolakoski_sequence()
{[], 0, {1, 2}}
iex> Math.start_kolakoski_sequence() |> Math.extend_kolakoski_sequence()
{[1], 1, {1, 2}}
iex> Math.start_kolakoski_sequence() |> Math.extend_kolakoski_sequence_to_length(20)
{[1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1], 13, {1, 2}}
Find the n
th term of Stern's diatomic series.
This function calculates the diatomic series via a binomial summation.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a002487/1
- Stern diatomic series, or Stern-Brocot sequence
Examples
iex> Math.stern_diatomic_series(0)
0
iex> Math.stern_diatomic_series(25)
7
iex> Math.stern_diatomic_series(30)
4
iex> Math.stern_diatomic_series(90)
12
iex> Math.stern_diatomic_series(127)
7
Find the Stirling partition number (or Stirling number of the second kind) {n, k}
.
In combinatorics, the Stirling partition number describes the number of ways to partition a set of n
elements
into k
non-empty subsets.
The explicit formula for {n, k}
is:
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a008277/1
- Triangle of Stirling numbers of the second kind
Examples
iex> Math.stirling_partition_number(0, 0)
1
iex> Math.stirling_partition_number(3, 0)
0
iex> Math.stirling_partition_number(5, 2)
15
iex> Math.stirling_partition_number(7, 4)
350
iex> Math.stirling_partition_number(10, 6)
22827
iex> Math.stirling_partition_number(10, 13)
0
The tau (number of divisors) function.
Also written as 𝜏(n)
or sigma(n, 0)
, this is a shortcut to sigma/2
.
Examples
iex> Math.tau(9)
3
iex> Math.tau(34)
4
iex> Math.tau(50)
6
iex> Math.tau(3402)
24
Find the n
-th tetrahedral number.
Tetrahedral numbers can be represented as a sum of triangular numbers:
or a binomial:
or as a rising factorial:
This implementation uses the rising factorial, which reduces to just addition and multiplication operations.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000292/1
- Tetrahedral numbers
Examples
iex> Math.tetrahedral_number(0)
0
iex> Math.tetrahedral_number(34)
7140
iex> Math.tetrahedral_number(47)
18424
iex> Math.tetrahedral_number(9876)
160591999876
Convert a decimal integer into another base.
If the new base can be represented with the decimal digits (i.e.; bases 2 through 10), the
returned value will be an integer. If the base is greater than 10, the return value will be
a list of digits that are in base b
.
Examples
iex> Math.to_base(123, 3)
11120
iex> Math.to_base(123456789, 8)
726746425
iex> Math.to_base(987654321, 2)
111010110111100110100010110001
iex> Math.to_base(2048, 60)
[34, 8]
Count the number of total, or series reduced tree, partitions of n
elements.
Also known as Schröder's Fourth problem. In combinatorics, this is the number of
singleton reduced trees with n
labels, where the leaves are non-empty sets.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000311/1
- Series-reduced rooted trees withn
labeled leaves
Examples
iex> Math.total_partitions(0)
0
iex> Math.total_partitions(3)
4
iex> Math.total_partitions(8)
660032
iex> Math.total_partitions(20)
887094711304119347388416
Euler's totient function for n
.
Also called phi or written as Φ(n)
, the Eulerian totient function counts the positive
integers up to n
that are relatively prime or coprime to n
. The method used for
calculating this function relies on a partially closed form of Euler's product formula
that grows relative to the number of prime factors of n
.
If you need the actual coprimes of n
, and not just the count of coprimes, see coprimes/1
or coprimes/2
.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000010/1
- Euler's totient function,phi(n)
Examples
iex> Math.totient(36)
12
iex> Math.totient(101)
100
iex> Math.totient(99999)
64800
Find the triangle or triangular number of n
.
The triangle number is the number of elements in the triangular arrangement of elements
with n
elements on a side.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000217/1
- Triangular numbers
Examples
iex> Math.triangle_number(0)
0
iex> Math.triangle_number(4)
10
iex> Math.triangle_number(50)
1275
iex> Math.triangle_number(475)
113050
iex> Math.triangle_number(29999)
449985000
Find the triangle row and offset for the n
th item in a triangle.
Given an element or number triangle with a single element at the root, counting rows
from 1, and elements from 0, this function will determine at which row and offset the n
-th
element will occur.
So, given the triangle:
*
* *
* * *
* + * *
* * * * *
* * * * * *
The +
is the 8th item (index 7) in the triangle, and is on row 4, offset 1
from the left
iex> Math.triangle_position_for_element(7) {4, 1}
Examples
iex> Math.triangle_position_for_element(0)
{1, 0}
iex> Math.triangle_position_for_element(11)
{5, 1}
iex> Math.triangle_position_for_element(20)
{6, 5}
iex> Math.triangle_position_for_element(32003)
{253, 125}
Calculate the row in which the n
-th element would be in an element triangle.
Given an element or number triangle with a single element at the root, counting rows
from 1, and elements from 0, this function will determine at which row the n
-th
element will occur.
So, given the triangle:
*
* *
* * *
* + * *
* * * * *
* * * * * *
The +
is the 8th item (index 7) in the triangle, and is on row 4:
iex> Math.triangle_row_for_element(7) 4
Examples
iex> Math.triangle_row_for_element(0)
1
iex> Math.triangle_row_for_element(11)
5
iex> Math.triangle_row_for_element(20)
6
iex> Math.triangle_row_for_element(30130)
245
Count the number of bracelet permutations for n
beads of two colors.
By default the "turning over" of beads is allowed. See options below for configuraitons.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000029/1
- Bracelets with n beads of two colors with turning overChunky.Sequence.OEIS.Core.create_sequence_a000031/1
- Bracelets with n beads of two colors without turning over
Options
allow_turning_over
- Boolean. Defaulttrue
. Allow or disallow "turning over" of beads on bracelet
Examples
iex> Math.two_color_bracelet_count(5)
8
iex> Math.two_color_bracelet_count(12)
224
iex> Math.two_color_bracelet_count(37)
1857545300
iex> Math.two_color_bracelet_count(5, allow_turning_over: false)
8
iex> Math.two_color_bracelet_count(12, allow_turning_over: false)
352
iex> Math.two_color_bracelet_count(37, allow_turning_over: false)
3714566312
Count the number of bracelet permutations for n
beads, with primitive period of n
, with two colors.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a000048/1
- Number of n-bead necklaces with beads of 2 colors and periodn
Options
allow_turning_over
- Boolean. Defaultfalse
. Currently only supportsfalse
.
Examples
iex> Math.two_color_bracelet_with_period_count(1)
1
iex> Math.two_color_bracelet_with_period_count(5)
3
iex> Math.two_color_bracelet_with_period_count(12)
170
iex> Math.two_color_bracelet_with_period_count(38)
3616814565
Calculate the Wedderburn-Etherington number for n
.
In combinatorics, the Wedderburn-Etherington number is used to determine the size of certain sets of Binary Trees. Other uses include (via Wikipedia and OEIS A001190):
- Otter Trees - the number of unordered rooted trees with n leaves in which all nodes including the root have either zero or exactly two children.
- Planted Trees - the number of unordered rooted trees with n nodes in which the root has degree zero or one and all other nodes have at most two children.
- The number of different ways of organizing a single-elimination tournament for n players
- Number of colorations of Kn (complete graph of order n) with n-1 colors such that no triangle is three-colored
Calculation of the Wedderburn-Etherington number is done via a recurrence relationship for odd n
:
and even n
:
Because these relations are highly recursive, this implementation uses a cache for efficiency.
OEIS References:
Chunky.Sequence.OEIS.Core.create_sequence_a001190/1
- Wedderburn-Etherington numbers
Examples
iex> Math.wedderburn_etherington_number(3)
1
iex> Math.wedderburn_etherington_number(5)
3
iex> Math.wedderburn_etherington_number(9)
46
iex> Math.wedderburn_etherington_number(45)
639754054803187
iex> Math.wedderburn_etherington_number(300)
1972666500548256069567265504055115733765719122240464770401890754621349706143463425967160618093669965967626678829167