chunky v0.11.4 Chunky.Math View Source
Integer math, number theory, factorization, prime numbers, and numerical analysis 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.
pow/2
- Integer exponentiationfactorial/1
- Factorial (n!
) ofn
rising_factorial/2
- Rising factorial ofn^(m)
falling_factorial/2
- Falling factorial of(n)m
lcm/2
- Least common multiple ofn
andm
lcm/1
- Least common multiple of a list of integersinteger_nth_root?/3
- Determine if an integer has a specificn-th
rootinteger_nth_root/3
- Find then
-th integer root, if it exists
Float Arithmetic
nth_root/3
- Floating pointn-th
root of an integerfloats_equal?/3
- Determine if two floats are equal, within an error bound
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
prime_factors/1
- Factorize an integer into prime factorssigma/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 representationdigit_count/3
- Count digits inn
in any base representationdigit_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 numberreverse_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
Predicates
Assess integers using predicate functions. Every predicate function takes a single integer, and returns a boolean. These predicates span all areas of integer math, from number theory, to factorization and primes, to combinatorics and beyond.
is_abundant?/1
- Test if an integer is abundantis_achilles_number?/1
- Isn
an Achilles Number?is_arithmetic_number?/1
- Test if an integer is an arithmetic numberis_carmichael_number?/1
- Isn
a Carmichael number, a number pseudo-prime to all coprime basesis_circular_prime?/1
- Isn
a circular prime?is_cubefree?/1
- Are any factors ofn
perfect cubes?is_deficient?/1
- Test if an integer is deficientis_double_vampire_number?/1
- Isn
a vampire number whose fangs are also vampire numbers?is_emirp_prime?/1
- Isn
a prime that is a different prime when reversed?is_euler_jacobi_pseudo_prime?/1
- Isn
an Euler-jacobi pseudo prime?is_euler_pseudo_prime?/1
- Isn
an Euler pseudo-prime?is_even?/1
- Is an integer even?is_highly_abundant?/1
- Test if an integer is a highly abundant numberis_highly_powerful_number?/1
- Test if an integer is a highly powerful numberis_left_truncatable_prime?/1
- Isn
a left-truncatable prime?is_left_right_truncatable_prime?/1
- Isn
a left/right truncatable prime?is_multiple_rhonda?/1
- Test ifn
is a Rhonda number in multiple basesis_negative?/1
- Is an integer a negative number?is_odd?/1
- Is an integer odd?is_odious_number?/1
- Does binary expansion ofn
have odd number of1
s?is_palindromic?/1
- Isn
palindromic in base 10?is_palindromic_prime?/1
- Isn
a palindromic prime?is_pandigital?/1
- Isn
a pandigital number in base 10?is_perfect?/1
- Test if an integer is perfectis_perfect_cube?/1
- Ism
a perfect square?is_perfect_power?/1
- Isn
a perfect power?is_perfect_square?/1
- Isn
a perfect square?is_plaindrome?/1
- Doesn
have never decreasing digits in base 10?is_positive?/1
- Is an integer a positive number?is_poulet_number?/1
- Isn
a Poulet number?is_powerful_number?/1
- Test if an integer is a powerful numberis_prime?/1
- Test if an integer is primeis_prime_fast?/1
- Alternative prime test, faster in specific cases ofn
is_prime_power?/1
- Check ifn
is a powerm
of a prime, wherem
>= 1.is_prime_vampire_number?/1
- Isn
a vampire number with prime fangs?is_pseudo_prime?/1
- Isn
a Fermat pseudo prime in base 10?is_pseudo_vampire_number?/1
- Isn
a vampire number with relaxed constraints?is_rhonda_to_base_4?/1
- Determine ifn
is a Rhonda number to base 4is_rhonda_to_base_6?/1
- Determine ifn
is a Rhonda number to base 6is_rhonda_to_base_8?/1
- Determine ifn
is a Rhonda number to base 8is_rhonda_to_base_9?/1
- Determine ifn
is a Rhonda number to base 9is_rhonda_to_base_10?/1
- Determine ifn
is a Rhonda number to base 10is_rhonda_to_base_12?/1
- Determine ifn
is a Rhonda number to base 12is_rhonda_to_base_14?/1
- Determine ifn
is a Rhonda number to base 14is_rhonda_to_base_15?/1
- Determine ifn
is a Rhonda number to base 15is_rhonda_to_base_16?/1
- Determine ifn
is a Rhonda number to base 16is_rhonda_to_base_20?/1
- Determine ifn
is a Rhonda number to base 20is_rhonda_to_base_30?/1
- Determine ifn
is a Rhonda number to base 30is_rhonda_to_base_60?/1
- Determine ifn
is a Rhonda number to base 60is_right_truncatable_prime?/1
- Isn
a right-truncatable prime?is_sphenic_number?/1
- Isn
the product of three distinct primes?is_squarefree?/1
- Are any factors ofn
perfect squares?is_strictly_non_palindromic?/1
- Isn
non-palindromic in bases 2 throughn - 2
?is_two_sided_prime?/1
- Isn
both a left and right truncatable prime?is_vampire_number?/1
- Isn
a vampire number?is_weakly_prime?/1
- Isn
a weakly prime number?is_zero?/1
- Is an integer0
?
All of the predicates can be used to analyze an integer with:
analyze_number/2
- Apply all 1-arity predicates to an integer, and collect resulting labels
Number Theory
Functions related to Number Theory operations over the integers.
aliquot_sum/1
- Find the Aliquot Sum ofn
bigomega/1
- Big Omega function - count of distinct primes, with multiplicitydivisors_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_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 numberproduct_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 pyramidtetrahedral_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 triangel 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
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 circleordered_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
wedderburn_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
nodesrooted_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
is_3_smooth?/1
- Predicate shortcut foris_b_smooth?(n, 3)
is_5_smooth?/1
- Predicate shortcut foris_b_smooth?(n, 5)
is_7_smooth?/1
- Predicate shortcut foris_b_smooth?(n, 7)
is_11_smooth?/1
- Predicate shortcut foris_b_smooth?(n, 11)
is_13_smooth?/1
- Predicate shortcut foris_b_smooth?(n, 13)
is_17_smooth?/1
- Predicate shortcut foris_b_smooth?(n, 17)
is_19_smooth?/1
- Predicate shortcut foris_b_smooth?(n, 19)
is_23_smooth?/1
- Predicate shortcut foris_b_smooth?(n, 23)
Number Generation
Number sequence iteration functions used by the Chunky.Sequence
library.
next_abundant/1
- Find the next abundant number aftern
next_deficient/1
- Find the next deficient number aftern
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
.
Apply all 1-arity predicates to n
, and collect the resulting labels.
Calculate the Bell Number of n
, or the number of possible partitions of a set of size n
.
Calculate Ω(n)
- number of distinct prime factors, with multiplicity.
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.
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
.
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
.
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.
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.
Check if an integer n
is 11-smooth.
Check if an integer n
is 13-smooth.
Check if an integer n
is 17-smooth.
Check if an integer n
is 19-smooth.
Check if an integer n
is 23-smooth.
Check if an integer n
is 3-smooth.
Check if an integer n
is 5-smooth.
Check if an integer n
is 7-smooth.
Determine if an integer is abundant.
Check if a number is an Achilles Number.
Determine if an integer is an arithmetic number.
Determine if an integer n
is b
-smooth, a composite of prime factors less than or equal to b
.
Check if n
is a Carmichael number.
Is n
a circular prime?
Determine if two numbers, a
and b
, are co-prime.
Check if an integer n
has no factors greater than 1
that are perfect cubes.
Determine if an integer is deficient.
Check of n
is a double vampire number.
Is n
an emirp - a prime number that is a different prime number when reversed?
Check if n
is an Euler-Jacobi pseudo-prime to base 10.
Check if n
is an Euler-Jacobi pseudo-prime to base a
.
Check if n
is an Euler pseudo-prime in base 10.
Check if n
is an Euler pseudo-prime in base a
.
Predicate version of is_even/1
Integer guard.
Check if a number n
is highly abundant.
Check if a number n
is a highly powerful number.
Check if n
is a valid number in base b
.
Is n
a left/right truncatable prime?
Is n
a left truncatable prime?
Check if the integer n
is a Rhonda number to more than one base.
Determine if a number is a negative integer.
Predicate version of is_odd?/1
Integer guard.
Odious numbers have an odd number of 1
s in their binary expansion.
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 a palindromic number in base 10.
Check if n
is palindromic in base b
.
Is n
a palindromic prime number?
Check if n
is pandigital in base 10.
Determine if n
is pandigital in base b
.
Determine if an integer is a perfect number.
Check if n
is a perfect cube.
Check if n
is a perfect power.
Check if n
is a perfect square.
Check if n
is a plaindrome in base 10.
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.
Determine if a number is a positive integer.
Is n
a Poulet number?
Check if n
is a power of m
.
Determine if an integer n
is a powerful number.
Determine if a positive integer is prime.
Determine if a positive integer is prime.
Check if n
is a power m
of a prime, where m
>= 1.
Check if n
is a prime vampire number.
Determine if n
is pseudo-prime to base 10.
Determine if n
is a Fermat pseudo-prime to base a
.
Check if n
is a pseudo vampire number.
Check if n
is a Rhonda number to the base b
.
Determine if n
is a Rhonda number to base 10.
Determine if n
is a Rhonda number to base 12.
Determine if n
is a Rhonda number to base 14.
Determine if n
is a Rhonda number to base 15.
Determine if n
is a Rhonda number to base 16.
Determine if n
is a Rhonda number to base 20.
Determine if n
is a Rhonda number to base 30.
Determine if n
is a Rhonda number to base 4.
Determine if n
is a Rhonda number to base 60.
Determine if n
is a Rhonda number to base 6.
Determine if n
is a Rhonda number to base 8.
Determine if n
is a Rhonda number to base 9.
Is n
a right truncatable prime?
Check if n
is any k
-th root of m
, where k > 2
.
Check if n
is a sphenic number, the product of three distinct primes.
Check if an integer n
has no factors greater than 1
that are perfect squares.
Check if n
is strictly non-palindromic.
Is n
a two sided prime, a number that is both a left and right trucatable prime.
Check if n
is a true vampire number.
Is n
a weakly prime number?
Predicate for testing for 0
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.
Find the next abundant number after n
.
Find the next deficient number after n
.
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.
Generalized floating point nth root, from
Calculate ω(n)
- the number of distinct prime factors 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
.
Find the Pell Number for n
.
Find the n
-th pentagonal number.
Count the number of planar partitions with sum 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
.
Remove all occurances of one or more digits from 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
.
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].
The tau (number of divisors) function.
Find the n
-th tetrahedral number.
Convert a decimal integer into another base.
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.
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
.
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
.
Examples
iex> Math.aliquot_sum(1)
0
iex> Math.aliquot_sum(10)
8
iex> Math.aliquot_sum(48)
76
Apply all 1-arity predicates to n
, and collect the resulting labels.
This function uses the names of all of the predicate functions as sources for labels, and collects
the resulting labels from a number being analyzed. This will work with all integers in the range (-∞..0..+∞)
.
Some predicate functions can take a long time to run depending on the size of n
, so the analyze_number/2
function
uses a timeout for each predicate. See the predicate_wait_time
option for more details.
Options
skip_smooth
- Boolean, defaultfalse
. Iftrue
, skip all predicates of formis_#_smooth?/1
predicate_wait_time
- Integer, default100
. Maximum number of milliseconds to wait for an answer from each predicate function
Examples
iex> Math.analyze_number(2048)
[:"11_smooth", :"13_smooth", :"17_smooth", :"19_smooth", :"23_smooth",:"3_smooth", :"5_smooth", :"7_smooth", :deficient, :even, :odious_number,:perfect_power, :positive, :powerful_number, :prime_power]
iex> Math.analyze_number(2048, skip_smooth: true)
[:deficient, :even, :odious_number,:perfect_power, :positive, :powerful_number, :prime_power]
iex> Math.analyze_number(-37)
[:negative, :odd]
iex> Math.analyze_number(0)
[:even, :palindromic, :plaindrome, :zero]
iex> Math.analyze_number(105840, skip_smooth: true)
[:abundant, :arithmetic_number, :even, :odious_number, :positive]
iex> Math.analyze_number(105840, skip_smooth: true, predicate_wait_time: 20_000)
[:abundant, :arithmetic_number, :even, :highly_abundant, :odious_number, :positive]
iex> Math.analyze_number(1000, skip_smooth: true)
[:abundant, :even, :multiple_rhonda, :perfect_cube, :perfect_power, :positive, :powerful_number, :rhonda_to_base_16]
iex> Math.analyze_number(1435)
[:arithmetic_number, :cubefree, :deficient, :odd, :odious_number, :positive, :pseudo_vampire_number, :sphenic_number, :squarefree, :vampire_number]
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.
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 Ω(n)
- number of distinct prime factors, with multiplicity.
See also omega/1
- number of distinct prime factors.
Examples
iex> Math.bigomega(3)
1
iex> Math.bigomega(15)
2
iex> Math.bigomega(25)
2
iex> Math.bigomega(99960)
8
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.
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.
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)
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
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
.
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
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
.
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]
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
.
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))
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.
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.
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.
Examples
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
.
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.
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.
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.
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
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.
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.
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.
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
Check if an integer n
is 11-smooth.
See is_b_smooth?/2
for more details.
Examples
iex> Math.is_11_smooth?(3)
true
iex> Math.is_11_smooth?(40)
true
iex> Math.is_11_smooth?(107)
false
iex> Math.is_11_smooth?(2020)
false
Check if an integer n
is 13-smooth.
See is_b_smooth?/2
for more details.
Examples
iex> Math.is_13_smooth?(3)
true
iex> Math.is_13_smooth?(40)
true
iex> Math.is_13_smooth?(107)
false
iex> Math.is_13_smooth?(2020)
false
Check if an integer n
is 17-smooth.
See is_b_smooth?/2
for more details.
Examples
iex> Math.is_17_smooth?(3)
true
iex> Math.is_17_smooth?(40)
true
iex> Math.is_17_smooth?(107)
false
iex> Math.is_17_smooth?(2020)
false
Check if an integer n
is 19-smooth.
See is_b_smooth?/2
for more details.
Examples
iex> Math.is_19_smooth?(3)
true
iex> Math.is_19_smooth?(40)
true
iex> Math.is_19_smooth?(107)
false
iex> Math.is_19_smooth?(2020)
false
Check if an integer n
is 23-smooth.
See is_b_smooth?/2
for more details.
Examples
iex> Math.is_23_smooth?(3)
true
iex> Math.is_23_smooth?(40)
true
iex> Math.is_23_smooth?(107)
false
iex> Math.is_23_smooth?(2020)
false
Check if an integer n
is 3-smooth.
See is_b_smooth?/2
for more details.
Examples
iex> Math.is_3_smooth?(3)
true
iex> Math.is_3_smooth?(40)
false
iex> Math.is_3_smooth?(107)
false
iex> Math.is_3_smooth?(2020)
false
Check if an integer n
is 5-smooth.
See is_b_smooth?/2
for more details.
Examples
iex> Math.is_5_smooth?(3)
true
iex> Math.is_5_smooth?(40)
true
iex> Math.is_5_smooth?(107)
false
iex> Math.is_5_smooth?(2020)
false
Check if an integer n
is 7-smooth.
See is_b_smooth?/2
for more details.
Examples
iex> Math.is_7_smooth?(3)
true
iex> Math.is_7_smooth?(40)
true
iex> Math.is_7_smooth?(107)
false
iex> Math.is_7_smooth?(2020)
false
Determine if an integer is abundant.
An abundant number is an integer n
, such that the sum of all proper divisors of n
(including itself)
is greater than 2 * n
.
Alternatively, an abundant number is a number that satisfies: 𝜎(n) > 2n
See also; is_deficient?/1
, is_perfect?/1
, is_highly_abundant?/1
, next_abundant/1
.
Examples
iex> Math.is_abundant?(3)
false
iex> Math.is_abundant?(12)
true
iex> Math.is_abundant?(945)
true
Check if a number is an Achilles Number.
Achilles numbers are n
that are powerful (see is_powerful_number?/1
but not perfect powers (see is_perfect_power?/1
).
See Chunky.Sequence.OEIS.Factor
, sequence A052486
.
Examples
iex> Math.is_achilles_number?(70)
false
iex> Math.is_achilles_number?(72)
true
iex> Math.is_achilles_number?(2700)
true
iex> Math.is_achilles_number?(784)
false
Determine if an integer is an arithmetic number.
An arithmetic number n
such that the average of the sum of the proper divisors of n
is
a whole integer. Alternatively, n
that satisfy 𝜎(n) / 𝜏(n) == 0
.
Examples
iex> Math.is_arithmetic_number?(11)
true
iex> Math.is_arithmetic_number?(32)
false
iex> Math.is_arithmetic_number?(12953)
true
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 for checking b-smooth for the primes 3
to 23
:
- [`is_3_smooth?/1`](#is_3_smooth?/1)
- [`is_5_smooth?/1`](#is_5_smooth?/1)
- [`is_7_smooth?/1`](#is_7_smooth?/1)
- [`is_11_smooth?/1`](#is_11_smooth?/1)
- [`is_13_smooth?/1`](#is_13_smooth?/1)
- [`is_17_smooth?/1`](#is_17_smooth?/1)
- [`is_19_smooth?/1`](#is_19_smooth?/1)
- [`is_23_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
Check if n
is a Carmichael number.
A Carmichael number n
is a composite number that satisfies the congruence:
for all b
that are coprime to n
.
Examples
iex> Math.is_carmichael_number?(517)
false
iex> Math.is_carmichael_number?(561)
true
iex> Math.is_carmichael_number?(1105)
true
iex> Math.is_carmichael_number?(1107)
false
iex> Math.is_carmichael_number?(41041)
true
Is n
a circular prime?
A circular prime is a number n
that remains a prime number through all
possible rotations of the digits of n
. For instance, 1193
is a circular
prime because 1193
, 1931
, 9311
, and 3119
are all prime.
Examples
iex> Math.is_circular_prime?(1193)
true
iex> Math.is_circular_prime?(3)
true
iex> Math.is_circular_prime?(193939)
true
iex> Math.is_circular_prime?(135)
false
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
Check if an integer n
has no factors greater than 1
that are perfect cubes.
Examples
iex> Math.is_cubefree?(3)
true
iex> Math.is_cubefree?(64)
false
iex> Math.is_cubefree?(2744)
false
Determine if an integer is deficient.
A deficient number is an integer n
, such that the sum of all proper divisors of n
(including itself)
is less than 2 * n
.
Alternatively, a deficient number is a number that satisfies: 𝜎(n) < 2n
See also; is_abundant?/1
, is_highly_abundant?/1
, is_perfect?/1
, next_deficient/1
.
Examples
iex> Math.is_deficient?(1)
true
iex> Math.is_deficient?(71)
true
iex> Math.is_deficient?(33550336)
false
iex> Math.is_deficient?(60)
false
Check of n
is a double vampire number.
Double vampire numbers meet all the criteria of a regular vampire number (see is_vampire_number?/1
)
with the additional constraint:
- The two factors of
n
,a
andb
, must also be vampire numbers
Examples
iex> Math.is_double_vampire_number?(6880)
false
iex> Math.is_double_vampire_number?(1047527295416280)
true
Is n
an emirp - a prime number that is a different prime number when reversed?
Emirp primes are related to palindromic primes (see is_palindromic_prime?/1
), except that the
reverse of n
must be a different prime number.
Examples
iex> Math.is_emirp_prime?(373)
false
iex> Math.is_emirp_prime?(13)
true
iex> Math.is_emirp_prime?(983)
true
iex> Math.is_emirp_prime?(947351)
true
Check if n
is an Euler-Jacobi pseudo-prime to base 10.
See is_euler_jacobi_pseudo_prime?/2
for more.
Examples
iex> Math.is_euler_jacobi_pseudo_prime?(17)
false
iex> Math.is_euler_jacobi_pseudo_prime?(481)
true
iex> Math.is_euler_jacobi_pseudo_prime?(487)
false
iex> Math.is_euler_jacobi_pseudo_prime?(6601)
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 10.
See is_euler_pseudo_prime?/2
for a definition.
Examples
iex> Math.is_euler_pseudo_prime?(9)
true
iex> Math.is_euler_pseudo_prime?(14)
false
iex> Math.is_euler_pseudo_prime?(33)
true
iex> Math.is_euler_pseudo_prime?(91)
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
Predicate version of is_even/1
Integer guard.
Examples
iex> Math.is_even?(34)
true
iex> Math.is_even?(0)
true
iex> Math.is_even?(3)
false
Check if a number n
is highly abundant.
A number n
is highly abundant when the sum of the proper factors of n
is
greater than the sum of the proper factors of any number m
that is in 0 < m < n
.
Alternatively, for all natural numbers, m < n ; 𝜎(m) < 𝜎(n)
.
See also; is_deficient?/1
, is_perfect?/1
, is_abundant?/1
.
Examples
iex> Math.is_highly_abundant?(1)
true
iex> Math.is_highly_abundant?(5)
false
iex> Math.is_highly_abundant?(30)
true
iex> Math.is_highly_abundant?(2099)
false
iex> Math.is_highly_abundant?(2100)
true
Check if a number n
is a highly powerful number.
Highly powerful numbers are similar in concept to highly abundant numbers. For highly powerful numbers,
the product of the exponents of prime factors of the number n
need to be greater than the same property
for any number m
, such that 0 < m < n
.
If you need a sequence of highly powerful numbers, use the A005934
sequence in Chunky.Sequence.OEIS.Factors
, which
uses an max/compare method for building the sequence, which is vastly more efficient than repeated
calls to is_highly_powerful_number?/1
.
See also is_powerful_number?/1
, and Highly powerful number.
Examples
iex> Math.is_highly_powerful_number?(4)
true
iex> Math.is_highly_powerful_number?(256)
false
iex> Math.is_highly_powerful_number?(62208)
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
Is n
a left/right truncatable prime?
Truncatable primes are prime number that remain prime when successive digits are removed. For
a left/right truncatable prime, the number will start, and remain, prime as the left and right digits of the
number are recursively dropped at the same time, until the number is a single or double digit prime. For instance, 99729779
is a left/right-truncatable prime, because 99729779
, 972977
, 7297
, and 29
are all prime. For left/right
truncation the final number can be a one or two digit prime, depending on if the original number nad an odd or even
number of digits.
For numbers that are both left truncatable and right trucatable, see is_two_sided_prime?/1
.
Examples
iex> Math.is_left_right_truncatable_prime?(433)
true
iex> Math.is_left_right_truncatable_prime?(1193)
true
iex> Math.is_left_right_truncatable_prime?(89)
true
iex> Math.is_left_right_truncatable_prime?(7)
true
iex> Math.is_left_right_truncatable_prime?(99729779)
true
Is n
a left truncatable prime?
Truncatable primes are prime number that remain prime when successive digits are removed. For
a left truncatable prime, the number will start, and remain, prime as the left digit of the
number is recursively dropped, until the number is a single digit prime. For instance, 967
is a left-truncatable prime, because 967
, 67
, and 7
are all prime.
Examples
iex> Math.is_left_truncatable_prime?(967)
true
iex> Math.is_left_truncatable_prime?(9137)
true
iex> Math.is_left_truncatable_prime?(9656934675)
false
iex> Math.is_left_truncatable_prime?(396334245663786197)
true
Check if the integer n
is a Rhonda number to more than one base.
This is a predicate wrapper around the get_rhonda_to/2
function.
Examples
iex> Math.is_multiple_rhonda?(1000)
true
iex> Math.is_multiple_rhonda?(1230)
false
Determine if a number is a negative integer.
Examples
iex> Math.is_negative?(-34)
true
iex> Math.is_negative?(0)
false
iex> Math.is_negative?(34)
false
Predicate version of is_odd?/1
Integer guard.
Examples
iex> Math.is_odd?(33)
true
iex> Math.is_odd?(0)
false
iex> Math.is_odd?(34)
false
Odious numbers have an odd number of 1
s in their binary expansion.
See definition on MathWorld or Wikipedia.
Examples
iex> Math.is_odious_number?(2)
true
iex> Math.is_odious_number?(37)
true
iex> Math.is_odious_number?(144)
false
iex> Math.is_odious_number?(280)
true
iex> Math.is_odious_number?(19897)
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 a palindromic number in base 10.
Palindromic numbers, like palindromic words, are the same value when read forward and reversed.
Examples
iex> Math.is_palindromic?(1234)
false
iex> Math.is_palindromic?(123454321)
true
iex> Math.is_palindromic?(1004006004001)
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
Is n
a palindromic prime number?
Palindromic prime numbers are both palindromes (the same digits/number when the digits are reversed) and prime numbers. By definition palindromic primes are prime when their digits are reversed.
Examples
iex> Math.is_palindromic_prime?(373)
true
iex> Math.is_palindromic_prime?(78247074287)
true
iex> Math.is_palindromic_prime?(55)
false
Check if n
is pandigital in base 10.
See is_pandigital_in_base?/2
for more details.
Examples
iex> Math.is_pandigital?(123456789)
false
iex> Math.is_pandigital?(1023456789)
true
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
Determine if an integer is a perfect number.
A perfect integer is an n
where the sum of the proper divisors of n
is equal to n
. Alternatively,
an n
that satisfies 𝜎(n) == 2n
.
See also; is_abundant?/1
, is_highly_abundant?/1
, is_deficient?/1
.
Examples
iex> Math.is_perfect?(5)
false
iex> Math.is_perfect?(6)
true
iex> Math.is_perfect?(20)
false
iex> Math.is_perfect?(33550336)
true
Check if n
is a perfect cube.
Perfect cubes are n
such that there exists an m
where m * m * m == n
or m^3 == n
.
Examples
iex> Math.is_perfect_cube?(6)
false
iex> Math.is_perfect_cube?(8000)
true
iex> Math.is_perfect_cube?(1879080904)
true
Check if n
is a perfect power.
Perfect powers are n
where positive integers m
and k
exist, such
that m^k == n
.
Examples
iex> Math.is_perfect_power?(4)
true
iex> Math.is_perfect_power?(100)
true
iex> Math.is_perfect_power?(226)
false
Check if n
is a perfect square.
Perfect squares are n
such that there exists an m
where m * m == n
.
Examples
iex> Math.is_perfect_square?(3)
false
iex> Math.is_perfect_square?(25)
true
iex> Math.is_perfect_square?(123456)
false
Check if n
is a plaindrome in base 10.
See is_plaindrome_in_base?/2
for more details.
Examples
iex> Math.is_plaindrome?(22367)
true
iex> Math.is_plaindrome?(2048)
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
Determine if a number is a positive integer.
Examples
iex> Math.is_positive?(4)
true
iex> Math.is_positive?(-3)
false
iex> Math.is_positive?(0)
false
Is n
a Poulet number?
Poulet numbers are Fermat pseudo-primes to base 2, see is_pseudo_prime?/2
.
Examples
iex> Math.is_poulet_number?(107)
false
iex> Math.is_poulet_number?(271)
false
iex> Math.is_poulet_number?(341)
true
iex> Math.is_poulet_number?(1387)
true
iex> Math.is_poulet_number?(10261)
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 an integer n
is a powerful number.
A powerful number is an integer n
such that for all prime factors m
of n
,
m^2
also evenly divides n
. Alternatively, a powerful number n
can be written
as n = a^2 * b^3
for positive integers a
and b
; n
is the product of a square
and a cube.
Examples
iex> Math.is_powerful_number?(8)
true
iex> Math.is_powerful_number?(10)
false
iex> Math.is_powerful_number?(800)
true
iex> Math.is_powerful_number?(970)
false
Determine if a positive integer is prime.
This function uses a Miller-Rabin primality test, with 40 iterations.
Examples
iex> Math.is_prime?(13)
true
iex> Math.is_prime?(233*444*727*456)
false
iex> Math.is_prime?(30762542250301270692051460539586166927291732754961)
true
Determine if a positive integer is prime.
This function uses a cache of the first 100 primes as a first stage sieve and comparison set. In some
cases using this method will result in a speed up over using is_prime?/1
:
- For numbers < 542,
is_prime_fast?/1
is a MapSet membership check - When iterating integers for prime candidates,
is_prime_fast?/1
can show an ~9% speed up overis_prime?/1
In all cases, is_prime_fast?/1
falls back to calling is_prime?
and the Miller-Rabin primality test code
in cases where the prime cache cannot conclusively prove or disprove primality.
Examples
iex> 1299601 |> Math.is_prime_fast?()
true
iex> 1237940039285380274899124225 |> Math.is_prime_fast?()
false
Check if n
is a power m
of a prime, where m
>= 1.
This is functionally a combination of is_perfect_power?/1
and is_prime?/1
, but
interleaves the factorization, leading to a speed up over using the two functions
independently.
Examples
iex> Math.is_prime_power?(2)
true
iex> Math.is_prime_power?(9)
true
iex> Math.is_prime_power?(10)
false
iex> Math.is_prime_power?(144)
false
Check if n
is a prime vampire number.
A prime vampire number needs to mee the same criteria as a standard vampire number (see is_vampire_number?/1
),
with the additional criteria of:
- Both of the factors of
n
,a
andb
, must be prime
Examples
iex> Math.is_prime_vampire_number?(6881)
false
iex> Math.is_prime_vampire_number?(117067)
true
Determine if n
is pseudo-prime to base 10.
This is a Fermat pseudo-primality test. See is_pseudo_prime?/2
for more details.
Examples
iex> Math.is_pseudo_prime?(9)
true
iex> Math.is_pseudo_prime?(33)
true
iex> Math.is_pseudo_prime?(47)
false
iex> Math.is_pseudo_prime?(481)
true
iex> Math.is_pseudo_prime?(559)
false
iex> Math.is_pseudo_prime?(8401)
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.
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 pseudo vampire number.
The pseudo-vampire numbers have more relaxed criteria than the standard
vampire numbers (see is_vampire_number?/1
). Pseudo-vampires change the restrictions on the number of
digits in n
and the factors a
and b
, such that:
n
can have an even or odd number of digits- The factors
a
andb
can be of any length, not strictly of half the length ofn
Examples
iex> Math.is_pseudo_vampire_number?(126)
true
iex> Math.is_pseudo_vampire_number?(128)
false
iex> Math.is_pseudo_vampire_number?(19026)
true
iex> Math.is_pseudo_vampire_number?(1025779)
true
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
Determine if n
is a Rhonda number to base 10.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_10?(35581)
true
iex> Math.is_rhonda_to_base_10?(327)
false
Determine if n
is a Rhonda number to base 12.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_12?(32742)
true
iex> Math.is_rhonda_to_base_12?(327)
false
Determine if n
is a Rhonda number to base 14.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_14?(135196)
true
iex> Math.is_rhonda_to_base_14?(327)
false
Determine if n
is a Rhonda number to base 15.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_15?(15873)
true
iex> Math.is_rhonda_to_base_15?(327)
false
Determine if n
is a Rhonda number to base 16.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_16?(50055)
true
iex> Math.is_rhonda_to_base_16?(327)
false
Determine if n
is a Rhonda number to base 20.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_20?(86591)
true
iex> Math.is_rhonda_to_base_20?(327)
false
Determine if n
is a Rhonda number to base 30.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_30?(22784)
true
iex> Math.is_rhonda_to_base_30?(327)
false
Determine if n
is a Rhonda number to base 4.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_4?(94185)
true
iex> Math.is_rhonda_to_base_4?(327)
false
Determine if n
is a Rhonda number to base 60.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_60?(91224)
true
iex> Math.is_rhonda_to_base_60?(327)
false
Determine if n
is a Rhonda number to base 6.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_6?(15104)
true
iex> Math.is_rhonda_to_base_4?(327)
false
Determine if n
is a Rhonda number to base 8.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_8?(56420)
true
iex> Math.is_rhonda_to_base_8?(327)
false
Determine if n
is a Rhonda number to base 9.
This function is a predicate version of the generalized Rhonda number
predicate. See is_rhonda_to_base?/2
for more information.
Examples
iex> Math.is_rhonda_to_base_9?(47652)
true
iex> Math.is_rhonda_to_base_9?(327)
false
Is n
a right truncatable prime?
Truncatable primes are prime number that remain prime when successive digits are removed. For
a right truncatable prime, the number will start, and remain, prime as the right digit of the
number is recursively dropped, until the number is a single digit prime. For instance, 23399
is a right-truncatable prime, because 23399
, 2339
, 233
, 23
, and 2
are all prime.
Examples
iex> Math.is_right_truncatable_prime?(23399)
true
iex> Math.is_right_truncatable_prime?(37)
true
iex> Math.is_right_truncatable_prime?(59393)
true
iex> Math.is_right_truncatable_prime?(59397)
false
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
Check if n
is a sphenic number, the product of three distinct primes.
Example
iex> Math.is_sphenic_number?(4)
false
iex> Math.is_sphenic_number?(66)
true
iex> Math.is_sphenic_number?(51339)
true
Check if an integer n
has no factors greater than 1
that are perfect squares.
Examples
iex> Math.is_squarefree?(3)
true
iex> Math.is_squarefree?(8)
false
iex> Math.is_squarefree?(99935)
true
Check if n
is strictly non-palindromic.
These numbers are non-palindromic in any numeric base from 2 to n - 2
. For larger
numbers this can be a very expensive check.
Examples
iex> Math.is_strictly_non_palindromic?(6)
true
iex> Math.is_strictly_non_palindromic?(19)
true
iex> Math.is_strictly_non_palindromic?(80)
false
iex> Math.is_strictly_non_palindromic?(317)
true
Is n
a two sided prime, a number that is both a left and right trucatable prime.
Truncatable primes are prime number that remain prime when successive digits are removed. For
a two sided prime, the number will start, and remain, prime under both left and right truncation.
For instance, 3137
is a two-sided prime, because:
3137
is prime313
,31
, and3
are prime (right truncation)137
,37
, and7
are prime (left truncation)
Examples
iex> Math.is_two_sided_prime?(7)
true
iex> Math.is_two_sided_prime?(313)
true
iex> Math.is_two_sided_prime?(3137)
true
iex> Math.is_two_sided_prime?(739397)
true
Check if n
is a true vampire number.
A vampire number is a number n
that fullfils the following criteria:
n
has an even number of digitsn
can be factored into two digitsa
andb
- Both
a
andb
half exactly half as many digits asn
- One or the other of
a
orb
can have trailing zeros, but not both a
andb
contain all of the original digits ofn
, in any order, including duplicated digits inn
Examples
iex> Math.is_vampire_number?(1260)
true
iex> Math.is_vampire_number?(6000)
false
iex> Math.is_vampire_number?(6880)
true
iex> Math.is_vampire_number?(125500)
true
Is n
a weakly prime number?
A prime number n
is called weakly prime if it becomes not prime when any one of its digits
is changed to every single other digit. Testing the number 347
would involve testing all
of the numbers 147
, 247
, 447
, 547
, ..., 348
, and 349
to see if they were prime. The
number 347
is not weakly prime because 947
is prime.
Examples
iex> Math.is_weakly_prime?(294001)
true
iex> Math.is_weakly_prime?(294007)
false
iex> Math.is_weakly_prime?(3085553)
true
Predicate for testing for 0
Examples
iex> Math.is_zero?(0)
true
iex> Math.is_zero?(34)
false
iex> Math.is_zero?(-34)
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.
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.
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.
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)
.
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.
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.
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.
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.
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
):
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
Find the next abundant number after n
.
See is_abundant?/1
.
Examples
iex> Math.next_abundant(1)
12
iex> Math.next_abundant(12)
18
iex> Math.next_abundant(60)
66
iex> Math.next_abundant(264)
270
Find the next deficient number after n
.
See is_deficient?/1
.
Examples
iex> Math.next_deficient(0)
1
iex> Math.next_deficient(5)
7
iex> Math.next_deficient(41)
43
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.is_powerful_number?/1, 49)
64
iex> Math.next_number(&Math.is_abundant?/1, 60)
66
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
.
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 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.
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.
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.
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 |
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
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.
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.
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 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.
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
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.
Example
iex> Math.pow(2, 10)
1024
iex> Math.pow(17, 14)
168377826559400929
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
.
Examples
iex> Math.ramanujan_tau(1)
1
iex> Math.ramanujan_tau(15)
1217160
iex> Math.ramanujan_tau(460)
-132549189534720
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
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.
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]
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.
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
.
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
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.
See also Chunky.Sequence.OEIS.Core
and the A000002
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}}
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.
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]
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
.
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.
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
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.
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