chunky v0.11.0 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

Integer Arithmetic

Arithmetic functions for pure integer operations.

Factorization and Divisors

Work with divisors and prime factors.

Primes

Analyze, test, and generate prime numbers.

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.

Number Theory

Functions related to Number Theory operations over the integers.

Polynomials

Combinatorics

Functions dealing with Combinatorics, permutation calculations, and related topics.

Graph Theory

Analyze numbers related to graph theory and trees.

Fractals

Integer fractals, and related number sets.

Abstract Algebra

Functions, numbers, and set counting related to Abstract Algebra.

  • abelian_group_count/1 - Number of Abelian groups of order n

Differential Topology

Manifolds, differential geometry, and differential topology functions.

Cryptography

Functions related to cryptographc analysis, factorization in cryptography, and numeric constructions.

Number Generation

Number sequence iteration functions used by the Chunky.Sequence library.

Link to this section Summary

Functions

Count the number of Abelian groups of order n.

Find the Aliquot Sum of n.

Calculate the Bell Number of n, or the number of possible partitions of a set of size n.

Calculate Ω(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.

Find the number of derangements of a set of size 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.

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.

Find the n-th Fubini number, the number of ordered partitions of a set size n.

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.

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.

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 if a number n is highly abundant.

Check if a number n is a highly powerful number.

Odious numbers have an odd number of 1s 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.

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 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 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.

Find the n-th coefficient of the q expansion of the modular J invariant function.

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 lpf(n) or least prime factor.

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.

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.

Caculate the rising factorial n^(m).

The number of unlabeled, or planted, trees with n nodes.

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.

Euler's totient function for n.

Find the triangle or triangular number of n.

Find the triangle row and offset for the nth 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

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:

Binomial coefficient

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.

Catalan Number

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

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

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:

BBP Formula

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
Link to this function

divisors_of_form_mx_plus_b(m, b, n)

View Source

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:

Euler Polynomial

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
Link to this function

extend_kolakoski_sequence(arg)

View Source

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}}
Link to this function

extend_kolakoski_sequence_to_length(k_seq, size)

View Source

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

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:

falling factorial

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

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
Link to this function

greatest_prime_factor(n)

View Source

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
Link to this function

hamming_weight(n, base \\ 2)

View Source

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 dimension n − 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

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
Link to this function

is_arithmetic_number?(n)

View Source

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

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 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
Link to this function

is_highly_powerful_number?(n)

View Source

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

Odious numbers have an odd number of 1s 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
Link to this function

is_of_mx_plus_b?(m, b, n, x \\ 0)

View Source

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

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 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:

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 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
Link to this function

j_invariant_q_coefficient(n)

View Source

Find the n-th coefficient of the q expansion of the modular J invariant function.

The Laurent series of the q-expansion begins:

q-expansion fourier transform

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

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:

Jacobsthal Number

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
Link to this function

labeled_rooted_forests_count(n)

View Source

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
Link to this function

labeled_rooted_trees_count(n)

View Source

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

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 nth 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) in n 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):

Motzkin Number

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.

Link to this function

next_number(property_func, n, step \\ 1)

View Source

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

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
Link to this function

ordered_subsets_count(n)

View Source

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 that p^ν divides n.

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:

nSeconds
100.021
1000.071
10007.301
250043.616
300061.921
5000185.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
Link to this function

plane_partition_count(n)

View Source

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

Plane Partitions

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
Link to this function

prime_factor_exponents(n)

View Source

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
Link to this function

product_of_prime_factor_exponents(n)

View Source

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:

![Ramanujan Tau](https://wikimedia.org/api/rest_v1/media/math/render/svg/846fc5e7ae7e57f2df206054ea0aba4124e6f124)

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

Caculate the rising factorial n^(m).

Also called the Pochhammer function, Pochhammer polynomial, ascending factorial, or upper factorial, this is the polynomial expansion:

Rising Factorial

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

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
Link to this function

square_pyramidal_number(n)

View Source

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
Link to this function

start_kolakoski_sequence(alphabet \\ {1, 2})

View Source

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:

Tetrahedral summation

or a binomial:

Tetrahedral binomial

or as a rising factorial:

Tetrahedral 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

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.

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
Link to this function

triangle_position_for_element(n)

View Source

Find the triangle row and offset for the nth 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}
Link to this function

triangle_row_for_element(n)

View Source

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
Link to this function

wedderburn_etherington_number(n)

View Source

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:

Wedderburn-Etherington for Odd N

and even n:

Wedderburn-Etherington for 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