Number theory
The 70 definitions of the number theory library, each with its Epsil spelling, its MathJSON name, its signature and its full description.
Finite arguments
The functions of this library give finite results: there is no meaningful
φ(\infty) or \sqrt{\mathrm{NaN}} to return. The parameter types of their
signatures say so. integer and real denote finite values, so +oo, -oo
and NaN are not members of either. Where a signature declares one of them,
a non-finite argument is a type error, not a symbolic or an infinite result:
nthPrime(+oo)
// ➔ error: Type error: expected `integer`, got `+oo` at `positiveInfinity` in `nthPrime(Infinity)`
integerSqrt(NaN)
// ➔ error: Type error: expected `integer`, got `NaN` at ``NaN`` in `integerSqrt(NaN)`
This is the check that rejects a non-integer, and it reports the same error. The argument has to be a member of the declared type before the function is applied.
integerSqrt(2.5)
// ➔ error: Type error: expected `integer`, got `2.5` at `2.5` in `integerSqrt(2.5)`
Congruences
A congruence a ≡ b (mod n) is written congruent(a, b, n) (see
congruent). It is True when a and b
leave the same remainder modulo n. Like divides, it stays symbolic until
its three operands are integers.
congruent(7, 1, 3)
// ➔ true
congruent(x, 1, 3)
// ➔ Congruent(x, 1, 3)
The remainder itself is a % n, the Mod function. In LaTeX, \bmod is
the operator (a \bmod n is Mod(a, n)), and \pmod{n} is the annotation
of a congruence: a \equiv b \pmod{n} is Congruent(a, b, n).
Reduction of large integers
% and congruent reduce an integer expression in ℤ/mℤ without computing
its value, which can have millions of digits. Powers, sums, products,
negations and factorials are reduced this way:
2^(3^20) % 100
// ➔ 52
congruent(2^(3^20), 52, 100)
// ➔ true
2 * 3^5000000 % 7
// ➔ 4
50! % 101
// ➔ 91
The result keeps the sign convention of %: it has the sign of the divisor.
[2^(3^20) % 7, 2^(3^20) % -7]
// ➔ [1,-6]
Solving congruences
solve of a linear congruence returns the family of its solutions, with
ArbitraryInteger(1) for an arbitrary integer. A congruence with no solution
gives the empty list, and a congruence whose coefficients share a factor with
the modulus is reduced:
solve(congruent(6n, 4, 7), n)
// ➔ [3 + 7ArbitraryInteger(1)]
solve(congruent(2x, 1, 4), x)
// ➔ []
solve(congruent(4x, 2, 6), x)
// ➔ [2 + 3ArbitraryInteger(1)]
A system of congruences in one unknown, joined with &&, is combined into
one family by the Chinese remainder theorem, also when the moduli are not
coprime. A system with no solution gives the empty list.
solve(congruent(x, 2, 3) && congruent(x, 3, 5) && congruent(x, 2, 7), x)
// ➔ [23 + 105ArbitraryInteger(1)]
To compute the solution from a list of residues and a list of moduli, use
chineseRemainder.
Algebraic numbers
minimalPolynomial, isAlgebraicInteger, algebraicNumberNorm,
algebraicNumberTrace and the three numberField… functions read their
argument as a polynomial in one generator, with rational coefficients. The
generator is a radical of a rational (sqrt(2), root(2, 3), 2^(1/5)),
i, the golden ratio or a polynomialRoot; sums, products, integer powers
and quotients of it are read. A radical is taken as the engine takes it
everywhere: root(-2, 3) is the real cube root and root(-2, 6) the
principal sixth root. Several numbered roots of one irreducible polynomial
are read in its splitting field.
Anything else stays unevaluated, with its argument as written: two radicals
of different bases (sqrt(2) + sqrt(3)), a transcendental number, a float,
a field of degree above 12, or a polynomial whose irreducibility the
factorization does not prove.
minimalPolynomial(sqrt(2) + sqrt(3), x)
// ➔ MinimalPolynomial(sqrt(2) + sqrt(3), x)
minimalPolynomial(pi, x)
// ➔ MinimalPolynomial(pi, x)
The field functions answer for the field ℚ(a) that the argument a
generates, which can be a proper subfield of the field of its generator.
Under N, the argument is still read exactly, and only the answer is
approximated.
Definitions
Each definition is listed under its Epsil spelling (the MathJSON name when it has none), with its signature in the engine's type syntax. The Standard Library page is the one-page index of every category.
algebraicNumberNorm
(number) -> number
- argument 1 — The algebraic number, a polynomial in one generator (a radical of a rational,
i, the golden ratio or aPolynomialRoot); another value stays unevaluated. Sample:1 + sqrt(2).
The norm of a over ℚ in the field ℚ(a): (−1)ᵈ times its minimal polynomial’s constant term over its leading one.
algebraicNumberNorm(1 + sqrt(2))
// ➔ -1
algebraicNumberNorm(3 + sqrt(2))
// ➔ 7
The norm of a from ℚ(a) to ℚ: the product of the conjugates of a.
algebraicNumberNorm(1 + sqrt(2))
// ➔ -1
algebraicNumberNorm(1/2 + root(2, 3))
// ➔ 17/8
N(algebraicNumberNorm(1/2 + root(2, 3)))
// ➔ 2.125
MathJSON: AlgebraicNumberNorm
["AlgebraicNumberNorm", a].
algebraicNumberTrace
(number) -> number
- argument 1 — The algebraic number, a polynomial in one generator (a radical of a rational,
i, the golden ratio or aPolynomialRoot); another value stays unevaluated. Sample:1 + sqrt(2).
The trace of a over ℚ in the field ℚ(a): minus its minimal polynomial’s second coefficient over its leading one.
algebraicNumberTrace(1 + sqrt(2))
// ➔ 2
algebraicNumberTrace(1 + sqrt(3) / 2)
// ➔ 2
The trace of a from ℚ(a) to ℚ: the sum of the conjugates of a.
algebraicNumberTrace(1 + sqrt(2))
// ➔ 2
MathJSON: AlgebraicNumberTrace
["AlgebraicNumberTrace", a].
bernoulliB
(broadcastable<integer>) -> rational
- argument 1 — The index, at least 0; a negative one stays unevaluated. Sample:
2.
Return the nth Bernoulli number Bₙ as an exact rational, using the convention B₁ = -1/2. Odd n > 1 give 0.
bernoulliB(2)
// ➔ 1/6
[bernoulliB(0), bernoulliB(1), bernoulliB(3), bernoulliB(12)]
// ➔ [1,-1/2,0,-691/2730]
See also: Bernoulli number.
MathJSON: BernoulliB
["BernoulliB", n].
["BernoulliB", 12]
// ➔ -691/2730
carmichaelLambda
(broadcastable<integer>) -> integer
- argument 1 — The modulus, at least 1; a smaller one stays unevaluated. Sample:
15.
Return the Carmichael function λ(n) (the reduced totient): the smallest positive integer m such that a^m ≡ 1 (mod n) for every a coprime to n. Defined for n ≥ 1.
carmichaelLambda(15)
// ➔ 4
λ(n) divides φ(n), and can be smaller:
[carmichaelLambda(8), totient(8)]
// ➔ [2,4]
carmichaelLambda(1)
// ➔ 1
See also: Carmichael function.
MathJSON: CarmichaelLambda
["CarmichaelLambda", n].
["CarmichaelLambda", 15]
// ➔ 4
catalanNumber
(broadcastable<real>) -> number
- argument 1 — The index, an integer at least 0 or a positive rational; a negative value stays unevaluated. Sample:
5.
Return the nth Catalan number C(n) = (2n)! / ((n+1)! · n!): 1, 1, 2, 5, 14, 42, … Defined for n ≥ 0. A positive rational n is taken through the Gamma function, C(n) = Γ(2n+1) / (Γ(n+1) Γ(n+2)): catalanNumber(1/2) is 8/(3π).
catalanNumber(5)
// ➔ 42
catalanNumber(1/2)
// ➔ 8 / (3pi)
A positive half-integer gives an exact value, and a float gives a float.
Another exact rational stays unevaluated, and N evaluates it numerically
from Γ(2n+1) / (Γ(n+1) Γ(n+2)). A negative non-integer stays unevaluated.
[catalanNumber(3/2), catalanNumber(5/2)]
// ➔ [64 / (15pi),1024 / (105pi)]
catalanNumber(2.5)
// ➔ 3.1042792709733490253
[catalanNumber(1/3), catalanNumber(-1/2)]
// ➔ [CatalanNumber(1/3),CatalanNumber(-1/2)]
N(catalanNumber(1/3))
// ➔ 0.849070020544739550675
See also: Catalan number.
MathJSON: CatalanNumber
["CatalanNumber", n].
["CatalanNumber", 5]
// ➔ 42
chineseRemainder
(residues: collection<any>, moduli: collection<any>, lower: integer?) -> integer
residues— The integer residues, one for each modulus. Sample:[2, 3, 2].moduli— The integer moduli, as many as residues; they need not be coprime, and an inconsistent system stays unevaluated. Sample:[3, 5, 7].lower— The least value of the solution; without it the solution is the smallest non-negative one. Sample:100.
Solve a system of simultaneous congruences: return the smallest non-negative integer x such that x ≡ residues[i] (mod moduli[i]) for every i. With a third argument lower, return the smallest solution x ≥ lower. Undefined if the system is inconsistent or the two lists differ in length.
chineseRemainder([2, 3, 2], [3, 5, 7])
// ➔ 23
chineseRemainder([2, 3, 2], [3, 5, 7], 100)
// ➔ 128
The moduli do not have to be coprime. An inconsistent system leaves the expression unevaluated:
chineseRemainder([1, 3], [4, 6])
// ➔ 9
chineseRemainder([1, 2], [4, 6])
// ➔ ChineseRemainder([1,2], [4,6])
To solve a system of congruences written with congruent, see
Solving congruences.
See also: Chinese remainder theorem.
MathJSON: ChineseRemainder
["ChineseRemainder", residues, moduli],
["ChineseRemainder", residues, moduli, d].
["ChineseRemainder", ["List", 2, 3, 2], ["List", 3, 5, 7]]
// ➔ 23
["ChineseRemainder", ["List", 2, 3, 2], ["List", 3, 5, 7], 100]
// ➔ 128
continuedFraction
(x: broadcastable<real>, count: broadcastable<integer>?) -> list<integer | list<integer>>
x— The finite real number to expand. Sample:sqrt(2).count— The largest number of terms, at least 1; a periodic expansion is then written out term by term. Sample:5.
Return the continued-fraction expansion of x as a list of integer terms [a0, a1, …]. An exact rational is expanded fully. An exact quadratic irrational (sqrt(2)) is expanded to its periodic form [a0, …, [period]] ([1, [2]]), or to the optional count terms. Another exact real (exponentialE) is expanded to count terms (default 20), each computed at a precision that vouches for it. An inexact value is expanded as its best rational approximation at working precision (see rationalize), truncated to count terms (default 20).
continuedFraction(43/19)
// ➔ [2,3,1,4]
continuedFraction(sqrt(2))
// ➔ [1,[2]]
With n, a quadratic irrational is expanded to exactly n terms:
continuedFraction(sqrt(2), 5)
// ➔ [1,2,2,2,2]
continuedFraction(sqrt(7))
// ➔ [2,[1,1,1,4]]
Another exact real, such as e or pi, is expanded to n terms (20 by
default). Each term is checked at double the working precision; when one
cannot be confirmed, the expression stays unevaluated.
continuedFraction(e, 8)
// ➔ [2,1,2,1,1,4,1,1]
continuedFraction(pi, 5)
// ➔ [3,7,15,1,292]
For a float, the round-off of the value can put large terms in the tail of the expansion.
continuedFraction(3.14159)
// ➔ [3,7,15,1,25,1,7,4]
See also: Continued fraction.
MathJSON: ContinuedFraction
["ContinuedFraction", x], ["ContinuedFraction", x, n].
["ContinuedFraction", ["Rational", 43, 19]]
// ➔ [2, 3, 1, 4]
["ContinuedFraction", ["Sqrt", 2]]
// ➔ [1, [2]]
digitCount
(n: broadcastable<integer>, base: broadcastable<integer>?, digit: broadcastable<integer>?, length: broadcastable<integer>?) -> integer | list<integer>
n— The integer whose digits are counted; its sign is ignored. Sample:122.base— The base, at least 2; 10 by default. Sample:10.digit— The digit to count, from 0 tobase - 1; without it the result lists the count of every digit. Sample:2.length— The number of digits counted, at least 0: zeros pad the digits on the left, or their most-significant ones are dropped. Sample:8.
Count digits of n in the given base (default 10); the sign of n is ignored. With a third argument digit, return how many times that digit occurs. Otherwise return a list [count of 1, count of 2, …, count of base-1, count of 0]. With a fourth argument length, the digits are first zero-padded on the left (or cut to their last length), so digitCount(5, 2, 0, 8) is 6.
digitCount(122, 10, 2)
// ➔ 2
digitCount(5, 2, 0, 8)
// ➔ 6
The list has one count for each digit of the base, the count of 0 last.
1223 has one 1, two 2s and one 3; 10 is 1010 in base 2.
digitCount(1223)
// ➔ [1,2,1,0,0,0,0,0,0,0]
digitCount(10, 2)
// ➔ [2,2]
length is at most 1,000,000. A larger length leaves the expression
unevaluated.
MathJSON: DigitCount
["DigitCount", n, base], ["DigitCount", n, base, digit],
["DigitCount", n, base, digit, length].
["DigitCount", 122, 10, 2]
// ➔ 2
["DigitCount", 5, 2, 0, 8]
// ➔ 6
digitSum
(n: broadcastable<integer>, base: broadcastable<integer>?, count: broadcastable<integer>?) -> integer
n— The integer whose digits are summed; its sign is ignored. Sample:1234.base— The base, at least 2; 10 by default. Sample:10.count— The number of digits summed, the first ones, or the last ones when it is negative. Sample:2.
Return the sum of the digits of n in the given base (default 10). The sign of n is ignored. With a third argument count, sum only the first count digits (most-significant first), or the last |count| when count is negative; a count past the digit count sums them all.
digitSum(1234)
// ➔ 10
digitSum(1234, 10, 2)
// ➔ 3
digitSum(255, 16)
// ➔ 30
[digitSum(1234, 10, -2), digitSum(1234, 10, 9)]
// ➔ [7,10]
MathJSON: DigitSum
["DigitSum", n], ["DigitSum", n, base], ["DigitSum", n, base, k].
["DigitSum", 1234]
// ➔ 10
["DigitSum", 1234, 10, 2]
// ➔ 3
dirichletCharacter
(k: broadcastable<integer>, j: broadcastable<integer>, n: broadcastable<integer>) -> number
k— The modulus of the character, at least 1. Sample:5.j— The index of the character, from 1 (the principal character) to φ(k); another one stays unevaluated. Sample:2.n— The integer at which the character is evaluated. Sample:2.
The Dirichlet character χ_j(n) modulo k, the j-th of the φ(k) characters (Wolfram's indexing, j = 1 the principal character). Zero where gcd(n, k) > 1; otherwise a root of unity.
dirichletCharacter(5, 2, 2)
// ➔ i
dirichletCharacter(7, 3, 3)
// ➔ e^(2/3 * i * pi)
j runs from 1, the principal character, to \varphi(k). An index outside
that range stays unevaluated. The function threads over a list:
dirichletCharacter(5, 2, [1, 2, 3, 4, 5])
// ➔ [1,i,-i,-1,0]
[dirichletCharacter(4, 1, [1, 2, 3]), dirichletCharacter(5, 5, 2)]
// ➔ [[1,0,1],DirichletCharacter(5, 5, 2)]
MathJSON: DirichletCharacter
["DirichletCharacter", k, j, n].
["DirichletCharacter", 5, 2, 2]
// ➔ "ImaginaryUnit"
["DirichletCharacter", 5, 2, 5]
// ➔ 0
dirichletL
(k: broadcastable<integer>, j: broadcastable<integer>, s: broadcastable<number>) -> number
k— The modulus of the character, at least 1. Sample:4.j— The index of the character, from 1 (the principal character) to φ(k). Sample:2.s— The point at which the L-function is evaluated. Sample:2.
The Dirichlet L-function L(s, χ) = Σ χ(n)/nˢ (n ≥ 1) of the character χ_j modulo k (dirichletCharacter(k, j, ·)): k^(−s) Σ_{r=1}^{k} χ(r) ζ(s, r/k). Entire for a non-principal character; the principal one is ζ(s) Π_{p|k} (1 − p^(−s)).
dirichletL(1, 1, 2)
// ➔ 1/6 * pi^2
dirichletL(3, 2, -2)
// ➔ -2/9
dirichletL(5, 2, 0)
// ➔ (3/5 + 1/5 * i)
The value at a non-positive integer s is exact. For a real character, the
value at s = 1 is exact, from the class number formula. The principal
character has a pole at s = 1.
[dirichletL(4, 2, 1), dirichletL(3, 2, 1), dirichletL(5, 1, 1)]
// ➔ [1/4 * pi,sqrt(3)/9 * pi,~oo]
A real s of a real character is computed to the engine precision. A
character with complex values is computed in double precision. A modulus
above 1000 stays unevaluated.
N(dirichletL(3, 2, 1.5))
// ➔ 0.703968244868733261668
N(dirichletL(5, 2, 2))
// ➔ (0.9587161227168831 + 0.1455658767850896i)
dirichletL(1009, 2, 2)
// ➔ DirichletL(1009, 2, 2)
MathJSON: DirichletL
["DirichletL", k, j, s].
["DirichletL", 4, 2, 1]
// ➔ ["Multiply", ["Rational", 1, 4], "Pi"]
["N", ["DirichletL", 3, 2, 1.5]]
// ➔ 0.703968244868733261668
divides
(broadcastable<integer>, broadcastable<integer>) -> boolean
- argument 1 — The divisor; 0 divides only 0. Sample:
3. - argument 2 — The integer to test for divisibility. Sample:
12.
divides(a, b) returns True if a divides b (i.e. b is an integer multiple of a), corresponding to the notation a ∣ b. Both operands are integers; a symbolic operand keeps the relation unevaluated.
divides(3, 12)
// ➔ true
Every integer divides 0, and 0 divides only 0. notDivides(a, b) is the
negation, a ∤ b.
[divides(4, 10), notDivides(4, 10)]
// ➔ [false,true]
[divides(5, 0), divides(0, 0), divides(0, 5)]
// ➔ [true,true,false]
A symbolic operand keeps the relation unevaluated. A number that is not an integer is a type error.
divides(a, b)
// ➔ Divides(a, b)
divides(2.5, 5)
// ➔ error: Type error: expected `integer`, got `2.5` at `2.5` in `divides(2.5, 5)`
MathJSON: Divides • LaTeX: \mid
["Divides", a, b], ["NotDivides", a, b]. In LaTeX, a \mid b is
["Divides", a, b], and a \nmid b is ["Not", ["Divides", a, b]].
["Divides", 3, 12]
// ➔ "True"
["Divides", 4, 10]
// ➔ "False"
["NotDivides", 4, 10]
// ➔ "True"
divisorSigma
(k: broadcastable<integer>, n: broadcastable<integer>) -> rational
k— The power of each divisor; 0 counts the divisors, and a negative one sums reciprocal powers. Sample:2.n— The integer whose positive divisors are summed; its sign is ignored, and 0 stays unevaluated. Sample:6.
The divisor function σ_k(n) = Σ_{d | n} dᵏ over the positive divisors of n. σ₀ counts divisors, σ₁ sums them. A negative k sums reciprocal powers, a rational: divisorSigma(-1, 12) is 7/3. The sign of n is ignored, as for divisors and primeOmega; divisorSigma(k, 0) is undefined.
divisorSigma(2, 6)
// ➔ 50
divisorSigma(-1, 12)
// ➔ 7/3
divisorSigma(0, n) is sigma0(n) and divisorSigma(1, n) is
sigma1(n). A negative k gives σ₋ₖ(n) / nᵏ.
[divisorSigma(0, 12), divisorSigma(1, 12), divisorSigma(3, 6)]
// ➔ [6,28,252]
[divisorSigma(1, -12), divisorSigma(1, 0)]
// ➔ [28,DivisorSigma(1, 0)]
divisorSigma(-2, 6)
// ➔ 25/18
See also: Divisor function.
MathJSON: DivisorSigma
["DivisorSigma", k, n].
["DivisorSigma", 2, 6]
// ➔ 50
divisors
(broadcastable<integer>) -> list<integer>
- argument 1 — The integer whose divisors are listed; its sign is ignored, and 0 stays unevaluated. Sample:
12.
Return the sorted list of positive divisors of an integer n. The sign of n is ignored.
divisors(12)
// ➔ [1,2,3,4,6,12]
divisors(0) stays unevaluated: every integer divides 0.
divisors(-12)
// ➔ [1,2,3,4,6,12]
divisors(0)
// ➔ Divisors(0)
See also: Divisor.
MathJSON: Divisors
["Divisors", n].
["Divisors", 12]
// ➔ [1, 2, 3, 4, 6, 12]
eulerE
(n: broadcastable<integer>, x: broadcastable<number>?) -> number
n— The index, at least 0; a negative one stays unevaluated. Sample:4.x— The point at which the Euler polynomial is evaluated, a number or a symbol; without it the result is the Euler number. Sample:1/2.
Return the nth Euler number Eₙ (1, 0, -1, 0, 5, 0, -61, …) for eulerE(n), or the Euler polynomial Eₙ(x) for eulerE(n, x). Odd-index Euler numbers are 0. Eₙ = 2ⁿ·Eₙ(1/2). A negative or non-integer n stays symbolic.
eulerE(4)
// ➔ 5
eulerE(2, x)
// ➔ x^2 - x
eulerE(n) is an exact integer for an integer n ≥ 0.
[eulerE(0), eulerE(1), eulerE(2), eulerE(6)]
// ➔ [1,0,-1,-61]
eulerE(-1)
// ➔ EulerE(-1)
eulerE(n, x) is exact at an exact x and a float at a float x, real or
complex in both cases. At a symbol, it is the expanded polynomial.
[eulerE(2, 1/3), eulerE(2, 0.5)]
// ➔ [-2/9,-0.25]
eulerE(3, 1 + 2i)
// ➔ (-25/4 - 8i)
See also: Euler numbers.
MathJSON: EulerE
["EulerE", n], ["EulerE", n, x].
["EulerE", 4]
// ➔ 5
["EulerE", 2, "x"]
// ➔ x^2 - x
["EulerE", 3, ["Complex", 1, 2]]
// ➔ -25/4 - 8i
eulerPhi
MathJSON EulerPhi · (broadcastable<integer>) -> integer
- argument 1 — The integer whose totient is computed; its sign is ignored, and φ(0) is 0. Sample:
12.
eulerPhi is an alias for totient, which is the preferred name. Euler's totient function φ(n): count of positive integers ≤ n that are coprime to n, for n ≥ 1; φ(0) = 0 and φ(−n) = φ(n).
eulerPhi(12)
// ➔ 4
eulerian
(n: broadcastable<integer>, m: broadcastable<integer>) -> integer
n— The number of elements permuted, at least 0. Sample:4.m— The number of ascents, at least 0; the value is 0 fromm = non (forn ≥ 1). Sample:1.
Eulerian number A(n, m): number of permutations of {1..n} with exactly m ascents.
eulerian(4, 1)
// ➔ 11
eulerian(5, 2)
// ➔ 66
An ascent is a position where the next number is greater than the one before it. Of the 24 permutations of 1, 2, 3, 4, eleven have exactly two ascents.
[eulerian(4, 0), eulerian(4, 1), eulerian(4, 2), eulerian(4, 3)]
// ➔ [1,11,11,1]
Outside the triangle the value is 0: eulerian(n, m) is 0 for m ≥ n > 0.
eulerian(0, 0) is 1. A negative or symbolic operand leaves the expression
unevaluated.
[eulerian(3, 5), eulerian(3, 3), eulerian(0, 0)]
// ➔ [0,0,1]
[eulerian(-1, 0), eulerian(n, 2)]
// ➔ [Eulerian(-1, 0),Eulerian(n, 2)]
See also: Eulerian number.
MathJSON: Eulerian
["Eulerian", n, m].
["Eulerian", 4, 2]
// ➔ 11
["Eulerian", 3, 5]
// ➔ 0
extendedGCD
(broadcastable<integer>, broadcastable<integer>, broadcastable<integer>*) -> tuple
- argument 1 — The first integer; its Bézout coefficient is the second entry of the result. Sample:
12. - argument 2 — The second integer; its Bézout coefficient is the third entry of the result. Sample:
18. - arguments 3, … — A further integer; its Bézout coefficient follows those of the integers before it. Sample:
27.
Return the extended GCD of a and b as a tuple (g, x, y) where g = gcd(a, b) is non-negative and a·x + b·y = g (Bézout coefficients). With more operands, ExtendedGCD(a₁, …, aₙ) is the tuple (g, x₁, …, xₙ) with g = gcd(a₁, …, aₙ) and Σ aᵢ·xᵢ = g.
extendedGCD(12, 18)
// ➔ (6, -1, 1)
extendedGCD(12, 18, 27)
// ➔ (3, 4, -4, 1)
240 · (-9) + 46 · 47 is 2, the GCD of 240 and 46:
extendedGCD(240, 46)
// ➔ (2, -9, 47)
extendedGCD(-12, 18)
// ➔ (6, 1, 1)
See also: Extended Euclidean algorithm.
MathJSON: ExtendedGCD
["ExtendedGCD", a, b, ...rest].
["ExtendedGCD", 12, 18]
// ➔ (6, -1, 1)
["ExtendedGCD", 12, 18, 27]
// ➔ (3, 4, -4, 1)
factorInteger
(broadcastable<rational>) -> list<tuple<integer, integer>>
- argument 1 — The integer or rational to factor; 0 and ±1 give
[(0, 1)],[(1, 1)]and[(-1, 1)]. Sample:360.
Return the prime factorization of an integer n as a list of [prime, exponent] tuples, ordered by ascending prime. For a negative n, a leading [-1, 1] tuple carries the sign. A rational p/q is factored as p and q, the exponents of the primes of q being negative.
factorInteger(360)
// ➔ [(2, 3),(3, 2),(5, 1)]
factorInteger(12/35)
// ➔ [(2, 2),(3, 1),(5, -1),(7, -1)]
360 is 2³ · 3² · 5, so its factorization is the list of the tuples
(2, 3), (3, 2) and (5, 1).
factorInteger(-360)
// ➔ [(-1, 1),(2, 3),(3, 2),(5, 1)]
[factorInteger(1), factorInteger(0)]
// ➔ [[(1, 1)],[(0, 1)]]
See also: Integer factorization.
MathJSON: FactorInteger
["FactorInteger", n].
["FactorInteger", 360]
// ➔ [(2, 3), (3, 2), (5, 1)]
["FactorInteger", ["Rational", 12, 35]]
// ➔ [(2, 2), (3, 1), (5, -1), (7, -1)]
fromContinuedFraction
(collection<any>) -> number
- argument 1 — The integer terms, from the integer part on; a last element that is a list is the period, and an empty list stays unevaluated. Sample:
[1, [2]].
Reconstruct the value of a continued fraction given its list of integer terms [a0, a1, …]. A last element that is a list is the repeating period, [1, [2]]: the quadratic irrational sqrt(2).
fromContinuedFraction([2, 3, 1, 4])
// ➔ 43/19
fromContinuedFraction([1, [2]])
// ➔ sqrt(2)
It is the inverse of continuedFraction:
fromContinuedFraction([3, 7, 15, 1])
// ➔ 355/113
fromContinuedFraction([2, [1, 1, 1, 4]])
// ➔ sqrt(7)
MathJSON: FromContinuedFraction
["FromContinuedFraction", terms].
["FromContinuedFraction", ["List", 2, 3, 1, 4]]
// ➔ 43/19
["FromContinuedFraction", ["List", 1, ["List", 2]]]
// ➔ ["Sqrt", 2]
fromDigits
(digits: collection<any>, base: number?) -> number
digits— The digits, most-significant first; an empty list gives 0. Sample:[1, 2, 3, 4].base— The base, 10 by default; any number or symbol. Sample:2.
Reconstruct a number from its digits (most-significant first) in the given base (default 10). The inverse of integerDigits. Digits outside [0, base) are combined positionally, and the base may be any number or symbol: fromDigits([1, 1, 0], -2) is 2 and fromDigits([a, b, c], x) is a x^2 + b x + c.
fromDigits([1, 2, 3, 4])
// ➔ 1234
fromDigits([1, 1, 0], -2)
// ➔ 2
fromDigits([a, b, c], x)
// ➔ a * x^2 + b * x + c
An integer base of any sign gives an exact integer. Another base gives the
sum of d_k · base^k, which is exact; N evaluates it numerically.
fromDigits([1, 0, 1, 1], 2)
// ➔ 11
fromDigits([1, 12], 10)
// ➔ 22
fromDigits([1, 2, 3], 1/2)
// ➔ 17/4
fromDigits([1, 2, 3], pi)
// ➔ 3 + 2pi + pi^2
N(fromDigits([1, 2, 3], pi))
// ➔ 19.1527897082689450958
MathJSON: FromDigits
["FromDigits", digits], ["FromDigits", digits, base].
["FromDigits", ["List", 1, 2, 3, 4]]
// ➔ 1234
["FromDigits", ["List", 1, 1, 0], -2]
// ➔ 2
["FromDigits", ["List", "a", "b", "c"], "x"]
// ➔ a x^2 + b x + c
integerDigits
(n: broadcastable<integer>, base: broadcastable<integer>?, length: broadcastable<integer>?) -> list<integer>
n— The integer whose digits are listed; its sign is ignored. Sample:255.base— The base, at least 2; 10 by default. Sample:16.length— The length of the result, at least 0: zeros pad it on the left, or its most-significant digits are dropped. Sample:8.
Return the digits of n in the given base (default 10), most-significant first. The sign of n is ignored. With a third argument length, the result is zero-padded on the left (or truncated to its least-significant digits) to that length.
integerDigits(255, 16)
// ➔ [15,15]
integerDigits(-255, 16)
// ➔ [15,15]
integerDigits(5, 2, 8)
// ➔ [0,0,0,0,0,1,0,1]
integerDigits(1234, 10, 2)
// ➔ [3,4]
length is at most 1,000,000. A larger length leaves the expression
unevaluated.
integerDigits(5, 2, 2000000)
// ➔ IntegerDigits(5, 2, 2000000)
MathJSON: IntegerDigits
["IntegerDigits", n], ["IntegerDigits", n, base],
["IntegerDigits", n, base, length].
["IntegerDigits", 255, 16]
// ➔ [15, 15]
integerSqrt
(broadcastable<integer>) -> integer
- argument 1 — The integer whose square root is rounded down; a negative one stays unevaluated. Sample:
17.
Return the integer square root of n, i.e. the largest integer m such that m² ≤ n. Undefined for negative n.
integerSqrt(17)
// ➔ 4
For a negative n, the expression stays unevaluated.
[integerSqrt(26), integerSqrt(123456789)]
// ➔ [5,11111]
integerSqrt(-4)
// ➔ IntegerSqrt(-4)
See also: Integer square root.
MathJSON: IntegerSqrt
["IntegerSqrt", n].
["IntegerSqrt", 17]
// ➔ 4
isAbundant
(broadcastable<integer>) -> boolean
- argument 1 — The integer to test; one below 1 is not abundant. Sample:
12.
True if n is an abundant number (sum of divisors > 2n).
isAbundant(12)
// ➔ true
The sum of the divisors of 12 other than itself, 1 + 2 + 3 + 4 + 6, is 16,
more than 12. 945 is the smallest odd abundant number. For n < 1 the
result is False.
[isAbundant(12), isAbundant(6), isAbundant(945)]
// ➔ [true,false,true]
See also: Abundant number.
MathJSON: IsAbundant
["IsAbundant", n].
["IsAbundant", 12]
// ➔ "True"
isAlgebraicInteger
(number) -> boolean
- argument 1 — The algebraic number, a polynomial in one generator (a radical of a rational,
i, the golden ratio or aPolynomialRoot); another value stays unevaluated. Sample:(1 + sqrt(5)) / 2.
Whether a is an algebraic integer: a root of a monic polynomial over ℤ.
isAlgebraicInteger((1 + sqrt(5)) / 2)
// ➔ true
isAlgebraicInteger(sqrt(2) / 2)
// ➔ false
True when a is a root of a monic polynomial with integer coefficients.
isAlgebraicInteger((1 + sqrt(5)) / 2)
// ➔ true
isAlgebraicInteger((1 + sqrt(-5)) / 2)
// ➔ false
MathJSON: IsAlgebraicInteger
["IsAlgebraicInteger", a].
isCenteredSquare
(broadcastable<integer>) -> boolean
- argument 1 — The integer to test; one below 1 is not a centered square. Sample:
25.
True if n is a centered square number, n = t² + (t + 1)² for an integer t ≥ 0: 1, 5, 13, 25, 41, ...
isCenteredSquare(25)
// ➔ true
The kth centered square number is k² + (k+1)²: a dot in the center and
the square layers of dots around it.
[isCenteredSquare(5), isCenteredSquare(6), isCenteredSquare(25)]
// ➔ [true,false,true]
filter(1..100, isCenteredSquare)
// ➔ [1,5,13,25,41,61,85]
See also: Centered square numbers, OEIS A001844.
MathJSON: IsCenteredSquare
["IsCenteredSquare", n].
["IsCenteredSquare", 5]
// ➔ "True"
isHappy
(broadcastable<integer>) -> boolean
- argument 1 — The integer to test; one below 1 is not happy. Sample:
19.
True if n is a happy number, a number which eventually reaches 1 when the number is replaced by the sum of the square of each digit
isHappy(19)
// ➔ true
isHappy(4)
// ➔ false
19 is happy: 1² + 9² is 82, 8² + 2² is 68, 6² + 8² is 100, and 1² + 0² + 0² is 1.
[n for n in 1..50 if isHappy(n)]
// ➔ [1,7,10,13,19,23,28,31,32,44,49]
See also: Happy number.
MathJSON: IsHappy
["IsHappy", n].
["IsHappy", 19]
// ➔ "True"
isOctahedral
(broadcastable<integer>) -> boolean
- argument 1 — The integer to test; one below 1 is not octahedral. Sample:
19.
True if n is an octahedral number.
isOctahedral(19)
// ➔ true
The kth octahedral number is k(2k² + 1)/3, the number of spheres in an
octahedron: 19 is 3 · 19 / 3.
[n for n in 1..200 if isOctahedral(n)]
// ➔ [1,6,19,44,85,146]
[isOctahedral(19), isOctahedral(20)]
// ➔ [true,false]
See also: Octahedral number.
MathJSON: IsOctahedral
["IsOctahedral", n].
["IsOctahedral", 19]
// ➔ "True"
isPerfect
(broadcastable<integer>) -> boolean
- argument 1 — The integer to test; one below 1 is not perfect. Sample:
28.
Returns "True" if n is a perfect number, a positive integer which equals the sum of all its divisors.
isPerfect(28)
// ➔ true
A perfect number is the sum of its positive divisors other than itself: 28
is 1 + 2 + 4 + 7 + 14. For n < 1 the result is False.
[isPerfect(28), isPerfect(12), isPerfect(-6)]
// ➔ [true,false,false]
[n for n in 1..10000 if isPerfect(n)]
// ➔ [6,28,496,8128]
See also: Perfect number.
MathJSON: IsPerfect
["IsPerfect", n].
["IsPerfect", 28]
// ➔ "True"
isPerfectPower
(broadcastable<integer>) -> boolean
- argument 1 — The integer to test; 0 and ±1 are not perfect powers, and a negative one is a perfect power when it is an odd power. Sample:
64.
Return "True" if n is a perfect power a^b for integers a and b ≥ 2 (a negative n requires an odd exponent). The smallest perfect power is 4.
isPerfectPower(64)
// ➔ true
-8 is (-2)³, but -4 is not a perfect power: its square roots are not real. 1 is not a perfect power.
[isPerfectPower(-8), isPerfectPower(-4), isPerfectPower(1)]
// ➔ [true,false,false]
[n for n in 1..70 if isPerfectPower(n)]
// ➔ [4,8,9,16,25,27,32,36,49,64]
See also: Perfect power.
MathJSON: IsPerfectPower
["IsPerfectPower", n].
["IsPerfectPower", 64]
// ➔ "True"
isSquare
(broadcastable<integer>) -> boolean
- argument 1 — The integer to test; 0 is a square, a negative integer is not. Sample:
49.
True if n is a perfect square.
isSquare(49)
// ➔ true
A perfect square is the square of an integer: 16 is 4². 0 is a perfect square, and a negative number is not.
isSquare([0, 2, 4, 9, -4])
// ➔ [true,false,true,true,false]
isSquare(x)
// ➔ IsSquare(x)
See also: Square number.
MathJSON: IsSquare
["IsSquare", n].
["IsSquare", 16]
// ➔ "True"
isSquareFree
(broadcastable<rational>) -> boolean
- argument 1 — The integer or rational to test; its sign is ignored, and 0 is not square-free. Sample:
30.
Return "True" if n is square-free (not divisible by any perfect square > 1). The sign of n is ignored. A rational is square-free when its numerator and denominator are.
isSquareFree(30)
// ➔ true
isSquareFree(6/35)
// ➔ true
[isSquareFree(2/3), isSquareFree(2/9)]
// ➔ [true,false]
MathJSON: IsSquareFree
["IsSquareFree", n].
["IsSquareFree", 30]
// ➔ "True"
isTriangular
(broadcastable<integer>) -> boolean
- argument 1 — The integer to test; one below 1 is not triangular. Sample:
10.
True if n is a triangular number.
isTriangular(10)
// ➔ true
The kth triangular number is k(k+1)/2, the number of objects in a
triangle with k rows: 15 is 5 · 6 / 2. 0 is not counted.
[n for n in 1..60 if isTriangular(n)]
// ➔ [1,3,6,10,15,21,28,36,45,55]
[isTriangular(15), isTriangular(16), isTriangular(0)]
// ➔ [true,false,false]
Like the other predicates of this library, isTriangular applies to each
element of a list, and stays unevaluated for a symbol.
See also: Triangular number.
MathJSON: IsTriangular
["IsTriangular", n].
["IsTriangular", 15]
// ➔ "True"
jacobiSymbol
(a: broadcastable<integer>, n: broadcastable<integer>) -> integer
a— The integer on top of the symbol. Sample:5.n— The odd positive integer at the bottom of the symbol; another one stays unevaluated. Sample:21.
The Jacobi symbol (a/n) for an odd n > 0. Returns -1, 0, or 1. Undefined when n is even or non-positive.
jacobiSymbol(5, 21)
// ➔ 1
For an even or a negative n, the expression stays unevaluated.
[jacobiSymbol(2, 15), jacobiSymbol(3, 9)]
// ➔ [1,0]
[jacobiSymbol(5, 20), jacobiSymbol(5, -21)]
// ➔ [JacobiSymbol(5, 20),JacobiSymbol(5, -21)]
See also: Jacobi symbol.
MathJSON: JacobiSymbol
["JacobiSymbol", a, n].
["JacobiSymbol", 5, 21]
// ➔ 1
legendreSymbol
(a: broadcastable<integer>, p: broadcastable<integer>) -> integer
a— The integer on top of the symbol; 0 whenpdivides it. Sample:3.p— The odd prime at the bottom of the symbol; another integer stays unevaluated. Sample:7.
The Legendre symbol (a/p) for an odd prime p. Returns -1, 0, or 1. Undefined when p is not an odd prime.
legendreSymbol(3, 7)
// ➔ -1
2 is a square modulo 7 (3² is 9), and 14 is a multiple of 7. When p is not
an odd prime, the expression stays unevaluated.
[legendreSymbol(2, 7), legendreSymbol(14, 7)]
// ➔ [1,0]
[legendreSymbol(3, 9), legendreSymbol(3, 2)]
// ➔ [LegendreSymbol(3, 9),LegendreSymbol(3, 2)]
See also: Legendre symbol.
MathJSON: LegendreSymbol
["LegendreSymbol", a, p].
["LegendreSymbol", 3, 7]
// ➔ -1
lucas
MathJSON Lucas · (broadcastable<integer>) -> integer
- argument 1 — The index of the Lucas number, which may be negative. Sample:
10.
lucas is an alias for lucasL, which is the preferred name. Returns the nth Lucas number.
lucas(10)
// ➔ 123
lucasL
(broadcastable<integer>) -> integer
- argument 1 — The index of the Lucas number, which may be negative. Sample:
10.
Return the nth Lucas number: lucasL(0) is 2, lucasL(1) is 1, and LucasL(n) = LucasL(n-1) + LucasL(n-2). Negative indices follow LucasL(-n) = (-1)^n · LucasL(n).
lucasL(10)
// ➔ 123
lucas is an alias: it becomes LucasL.
[lucasL(0), lucasL(1), lucasL(2), lucasL(3)]
// ➔ [2,1,3,4]
[lucasL(-5), lucasL(-4)]
// ➔ [-11,7]
lucas(n)
// ➔ LucasL(n)
See also: Lucas number.
MathJSON: LucasL
["LucasL", n]. ["Lucas", n] is the same function.
["LucasL", 10]
// ➔ 123
minimalPolynomial
(a: number, variable: any) -> expression
a— The algebraic number, a polynomial in one generator (a radical of a rational,i, the golden ratio or aPolynomialRoot); another value stays unevaluated. Sample:sqrt(2).variable— The variable of the polynomial. Sample:x.
The minimal polynomial of the algebraic number a in x: primitive over ℤ, positive leading coefficient.
minimalPolynomial(sqrt(2), x)
// ➔ x^2 - 2
minimalPolynomial(1 + root(2, 3), x)
// ➔ x^3 - 3x^2 + 3x - 3
minimalPolynomial(a, x) is the minimal polynomial of the algebraic number
a over ℚ, in the variable x: irreducible, with integer coefficients
whose greatest common divisor is 1, and a positive leading coefficient.
minimalPolynomial(1 + root(2, 3), x)
// ➔ x^3 - 3x^2 + 3x - 3
minimalPolynomial(1 / (1 + sqrt(2)), x)
// ➔ x^2 + 2x - 1
minimalPolynomial(polynomialRoot(x^3 - 2, x, 1) + polynomialRoot(x^3 - 2, x, 2), x)
// ➔ x^3 + 2
MathJSON: MinimalPolynomial
["MinimalPolynomial", a, x].
modularInverse
(a: broadcastable<integer>, modulus: broadcastable<integer>) -> integer
a— The integer to invert; it must be coprime to the modulus. Sample:3.modulus— The modulusm, not 0; a negative modulus gives a result in (m, 0]. Sample:7.
Return the modular multiplicative inverse of a modulo m: the integer x with a·x ≡ 1 (mod m). The sign of x follows the sign of m (the same floored-division convention as Mod). Undefined when a and m are not coprime.
modularInverse(3, 7)
// ➔ 5
modularInverse(3, -7)
// ➔ -2
For m > 0, the inverse is in the range [0, m). When a and m are not
coprime, the inverse does not exist and the expression stays unevaluated.
[modularInverse(10, 7), modularInverse(-3, 7)]
// ➔ [5,2]
modularInverse(4, 6)
// ➔ ModularInverse(4, 6)
See also: Modular multiplicative inverse.
MathJSON: ModularInverse
["ModularInverse", a, m].
["ModularInverse", 3, 7]
// ➔ 5
["ModularInverse", 4, 6]
// ➔ ["ModularInverse", 4, 6]
moebiusMu
(broadcastable<integer>) -> integer
- argument 1 — The argument of μ. Its sign is ignored; it stays unevaluated at 0. Sample:
30.
Return the Möbius function μ(n): 0 if n is divisible by a perfect square > 1, otherwise (-1) raised to the number of distinct prime factors. The sign of n is ignored.
moebiusMu(30)
// ➔ -1
12 is divisible by 4, and 30 is 2 · 3 · 5, a product of three distinct primes:
[moebiusMu(1), moebiusMu(12), moebiusMu(30)]
// ➔ [1,0,-1]
See also: Möbius function.
MathJSON: MoebiusMu
["MoebiusMu", n].
["MoebiusMu", 30]
// ➔ -1
multiplicativeOrder
(a: integer, modulus: integer, residues: list<integer>?) -> integer
a— The base whose powers are taken. Sample:2.modulus— The modulus; its sign is ignored, and 0 stays unevaluated. Sample:7.residues— The target residues: the result is the leastk > 0witha^kcongruent to one of them; by default, 1. Sample:[3, 11].
The multiplicative order of a modulo n: the smallest k > 0 such that a^k ≡ 1 (mod n). Undefined unless a and n are coprime. With a list of residues, MultiplicativeOrder(a, n, [r1, r2, …]) is the smallest k > 0 such that a^k ≡ r_i (mod n) for some i (a discrete logarithm), and is undefined when no r_i is a power of a. The sign of n is ignored. Undefined for n = 0.
multiplicativeOrder(2, 7)
// ➔ 3
multiplicativeOrder(5, 7, [3, 11])
// ➔ 2
The sign of n is ignored, because the unit group modulo -n is the unit
group modulo n. When a and n are not coprime, or n is 0, the
expression stays unevaluated.
multiplicativeOrder(3, -7)
// ➔ 6
[multiplicativeOrder(2, 6), multiplicativeOrder(2, 0)]
// ➔ [MultiplicativeOrder(2, 6),MultiplicativeOrder(2, 0)]
With a list of residues, the expression stays unevaluated when no element of
the list is a power of a. The powers of 2 modulo 7 are 2, 4 and 1:
multiplicativeOrder(2, 7, [3, 5])
// ➔ MultiplicativeOrder(2, 7, [3,5])
See also: Multiplicative order.
MathJSON: MultiplicativeOrder
["MultiplicativeOrder", a, n], ["MultiplicativeOrder", a, n, residues].
["MultiplicativeOrder", 2, 7]
// ➔ 3
["MultiplicativeOrder", 3, -7]
// ➔ 6
["MultiplicativeOrder", 5, 7, ["List", 3, 11]]
// ➔ 2
nPartition
(broadcastable<integer>) -> integer
- argument 1 — The integer to partition; a negative one gives 0. Sample:
5.
Number of integer partitions of n, for n ≥ 0; it is 0 for n < 0.
nPartition(5)
// ➔ 7
nPartition(10)
// ➔ 42
The number of ways to write n as a sum of positive integers, without
regard to their order. 5 has 7 partitions: 5, 4+1, 3+2, 3+1+1, 2+2+1,
2+1+1+1 and 1+1+1+1+1.
integerPartitions lists
them.
nPartition(5)
// ➔ 7
nPartition(5) == length(integerPartitions(5))
// ➔ true
[nPartition(100), nPartition(-3)]
// ➔ [190569292,0]
partitionsP, the Wolfram Language name, is an alias: it becomes
NPartition.
partitionsP(n)
// ➔ NPartition(n)
See also: Partition (number theory).
MathJSON: NPartition
["NPartition", n]. ["PartitionsP", n] is the same function.
["NPartition", 5]
// ➔ 7
["NPartition", -3]
// ➔ 0
nextPrime
(n: broadcastable<real>, k: broadcastable<integer>?) -> integer
n— The starting point, which is not counted even when it is prime. Sample:10.k— Which prime afternto return, 1 by default; a negativekcounts the primes beforen, and 0 stays unevaluated. Sample:2.
Return the smallest prime greater than n. With a second argument k, return the kth prime after n (k < 0 returns the |k|th prime before n). A non-integer real or rational n is allowed: nextPrime(7/2) is 5.
nextPrime(10)
// ➔ 11
nextPrime(10, -1)
// ➔ 7
nextPrime(7/2)
// ➔ 5
nextPrime(n, -1) is the largest prime less than n. When there is no such
prime, the expression stays unevaluated.
[nextPrime(10, 3), nextPrime(-5)]
// ➔ [17,2]
nextPrime(2, -1)
// ➔ NextPrime(2, -1)
See also: Prime number.
MathJSON: NextPrime
["NextPrime", n], ["NextPrime", n, k].
["NextPrime", 10]
// ➔ 11
["NextPrime", 10, 3]
// ➔ 17
["NextPrime", 10, -1]
// ➔ 7
notDivides
(broadcastable<integer>, broadcastable<integer>) -> boolean
- argument 1 — The divisor
a; 0 divides only 0. Sample:3. - argument 2 — The integer
bto divide. Sample:10.
notDivides(a, b) returns True if a does not divide b, corresponding to the notation a ∤ b.
notDivides(3, 10)
// ➔ true
MathJSON: NotDivides • LaTeX: \nmid
nthPrime
(broadcastable<integer>) -> integer
- argument 1 — The rank of the prime, from 1; it stays unevaluated below 1. Sample:
10.
Return the nth prime number (1-based): nthPrime(1) is 2, nthPrime(2) is 3, …
nthPrime(10)
// ➔ 29
The function is named nthPrime, not prime, because Prime is the
derivative notation of the Compute Engine (as in f'). primeNumber is an
alias: it becomes NthPrime. For n < 1 the expression stays unevaluated.
[nthPrime(1), nthPrime(2), nthPrime(1000)]
// ➔ [2,3,7919]
[nthPrime(0), primeNumber(n)]
// ➔ [NthPrime(0),NthPrime(n)]
See also: Prime number.
MathJSON: NthPrime
["NthPrime", n]. ["PrimeNumber", n] is the same function.
["NthPrime", 10]
// ➔ 29
numberFieldDiscriminant
(number) -> integer
- argument 1 — The algebraic number that generates the field, a polynomial in one generator (a radical of a rational,
i, the golden ratio or aPolynomialRoot); another value stays unevaluated. Sample:sqrt(2).
The discriminant of the field ℚ(a): of its ring of integers.
numberFieldDiscriminant(sqrt(2))
// ➔ 8
numberFieldDiscriminant(sqrt(5))
// ➔ 5
The discriminant of the ring of integers of ℚ(a). A rational generates ℚ, whose discriminant is 1.
numberFieldDiscriminant(root(19, 3))
// ➔ -1083
numberFieldDiscriminant(polynomialRoot(x^4 + x^3 + x^2 + x + 1, x, 1))
// ➔ 125
numberFieldDiscriminant(polynomialRoot(x^4 - 2, x, 1)^2)
// ➔ 8
MathJSON: NumberFieldDiscriminant
["NumberFieldDiscriminant", a].
numberFieldIntegralBasis
(number) -> list<number>
- argument 1 — The algebraic number that generates the field, a polynomial in one generator (a radical of a rational,
i, the golden ratio or aPolynomialRoot); another value stays unevaluated. Sample:sqrt(2).
A ℤ-basis of the ring of integers of ℚ(a), written on the powers of the generator read (the radical or root a is written in), as Wolfram writes it.
numberFieldIntegralBasis(sqrt(5))
// ➔ [1,1/2 + sqrt(5)/2]
A basis of the ring of integers of ℚ(a) over ℤ, in Mathematica's form, and
written in the generator that a is read in.
numberFieldIntegralBasis(sqrt(5))
// ➔ [1,1/2 + sqrt(5)/2]
numberFieldIntegralBasis(root(19, 3))
// ➔ [1,root(19, 3),1/3 + 1/3 * root(19, 3) + 1/3 * 19^(2/3)]
MathJSON: NumberFieldIntegralBasis
["NumberFieldIntegralBasis", a].
numberFieldSignature
(number) -> list<integer>
- argument 1 — The algebraic number that generates the field, a polynomial in one generator (a radical of a rational,
i, the golden ratio or aPolynomialRoot); another value stays unevaluated. Sample:sqrt(2).
The signature (r₁, r₂) of ℚ(a): its real embeddings and its pairs of complex ones.
numberFieldSignature(root(2, 3))
// ➔ [1,1]
The signature [r1, r2] of ℚ(a): the number of its real embeddings and of
its pairs of complex embeddings.
numberFieldSignature(root(2, 3))
// ➔ [1,1]
numberFieldSignature(polynomialRoot(x^4 + x^3 + x^2 + x + 1, x, 1))
// ➔ [0,2]
MathJSON: NumberFieldSignature
["NumberFieldSignature", a].
partitionsP
MathJSON PartitionsP · (broadcastable<integer>) -> integer
- argument 1 — The integer to partition; a negative one gives 0. Sample:
5.
partitionsP is an alias for nPartition, which is the preferred name. Number of integer partitions of n, for n ≥ 0; it is 0 for n < 0.
partitionsP(5)
// ➔ 7
partitionsQ
(broadcastable<integer>) -> integer
- argument 1 — The integer to partition; a negative one gives 0. Sample:
10.
Number of partitions of n into distinct parts (equivalently, into odd parts), q(n); it is 0 for n < 0. The Wolfram Language partitionsQ.
partitionsQ(10)
// ➔ 10
By Euler's theorem, the partitions into distinct parts and the partitions into odd parts are equally many (OEIS A000009). 5 has 3 of each: 5, 4+1 and 3+2, or 5, 3+1+1 and 1+1+1+1+1.
[partitionsQ(5), partitionsQ(-3)]
// ➔ [3,0]
MathJSON: PartitionsQ
["PartitionsQ", n].
["PartitionsQ", 10]
// ➔ 10
polynomialRoot
(polynomial: any, variable: any, index: integer?) -> number
polynomial— The polynomial, with rational coefficients. Sample:x^3 - x - 1.variable— The variable of the polynomial, bound by the call. Sample:x.index— Which root, from 1 to the degree; without it, the abstract root of an irreducible polynomial. Sample:1.
The root of the polynomial p in the variable x (bound, as in Wolfram's Root[Function[x, p], k]). With an index, polynomialRoot(p, x, k) is the k-th root: real roots first in increasing order, then complex roots by increasing real part, each conjugate pair together with the lower one first, and the pairs that share a real part by increasing |imaginary part|, as Mathematica does. It is a numbered embedding, a particular complex number, the one N() gives, and its other roots are other numbers; a root that cannot be numbered with certainty stays unevaluated. The root of a polynomial of degree 1 or 2 is a rational or a radical; past that it stays polynomialRoot until N(). Without an index, polynomialRoot(p, x) is the abstract root of the irreducible polynomial p, with no complex embedding selected: it has no numeric value, and its other roots are indistinguishable from it.
polynomialRoot(x^2 - 2, x, 2)
// ➔ sqrt(2)
N(polynomialRoot(x^3 - x - 1, x, 1))
// ➔ 1.32471795724474602596
polynomialRoot(x^2 + x + 1, x, 1)
// ➔ 1/2 * (-1 - sqrt(3) * i)
polynomialRoot(p, x, k) is the k-th root of the polynomial p in the
variable x, Mathematica's Root[Function[x, p], k]. The variable is
bound: a value of x does not change the root. The real roots come first, in
increasing order, then the complex roots by increasing real part, each
conjugate pair together with the lower one first, as in Mathematica. Pairs
that share a real part come by increasing size of the imaginary part: the roots
of (x^2 - 2x + 2)(x^2 - 2x + 5) are 1 - i, 1 + i, 1 - 2i, 1 + 2i.
The root of a polynomial of degree 1 or 2 is a rational or a radical. A root
of a polynomial of higher degree keeps its name, and N gives its value. A
root is numbered only when disks around the roots prove that they are
separated and in that order; otherwise, and for an index out of range, the
call stays unevaluated.
polynomialRoot(x^2 + x + 1, x, 1)
// ➔ 1/2 * (-1 - sqrt(3) * i)
polynomialRoot(x^3 - 2, x, 1)
// ➔ PolynomialRoot(x^3 - 2, x, 1)
N(polynomialRoot(x^3 - 2, x, 2))
// ➔ (-0.629960524947436582384 - 1.09112363597172140356i)
polynomialRoot(x^3 - 2, x, 4)
// ➔ PolynomialRoot(x^3 - 2, x, 4)
Without an index, polynomialRoot(p, t) is an abstract root of the
irreducible polynomial p: no complex embedding is chosen, so it has no
numeric value, and the other roots of p cannot be told apart from it. It is
the generator that factor takes as its third argument (see
factor). A power at or above the degree
of p is reduced modulo p. A reducible p is an error.
polynomialRoot(t^3 - t - 1, t)^3
// ➔ PolynomialRoot(t^3 - t - 1, t) + 1
N(polynomialRoot(t^2 - 2, t))
// ➔ PolynomialRoot(t^2 - 2, t)
polynomialRoot(t^2 - 1, t)
// ➔ error: Runtime error: invalid algebraic generator
MathJSON: PolynomialRoot
["PolynomialRoot", p, x, k] and ["PolynomialRoot", p, x].
["PolynomialRoot", ["Add", ["Power", "x", 2], -2], "x", 2]
// ➔ ["Sqrt", 2]
powerMod
(base: list<rational> | rational, exponent: list<rational> | rational, modulus: integer | list<integer>) -> integer | list<integer>
base— The basea; a rationalp/qisptimes the modular inverse ofq. Sample:2.exponent— The exponentb; a negative one uses the inverse of the base, and a rationals/rasks for anr-th root ofa^s. Sample:10.modulus— The modulusm, at least 1; the result is in [0, m). Sample:1000.
Return a^b mod m (modular exponentiation). A negative b uses the modular inverse of a; the result is undefined when that inverse does not exist (i.e. when a and m are not coprime). The result is in the range [0, m). A rational base p/q is p times the modular inverse of q, so powerMod(1/2, 2, 7) is 2. A rational exponent s/r gives the least x with x^r ≡ a^s (mod m), the first entry of powerModList(a, s/r, m). It is undefined when there is none, when m cannot be factored, or when there are too many roots to list and none of them is less than 100000. A list operand threads: powerMod([2, 3], 2, 7) is [4, 2].
powerMod(2, 10, 1000)
// ➔ 24
powerMod(4, 1/2, 7)
// ➔ 2
powerMod(1/2, 2, 7)
// ➔ 2
When the modular inverse that a negative exponent or a rational base needs
does not exist, the expression stays unevaluated. So does a rational
exponent s/r when no x has x^r ≡ a^s (mod m).
powerMod(3, -1, 7)
// ➔ 5
powerMod(2, -1, 4)
// ➔ PowerMod(2, -1, 4)
powerMod(1/2, 2, 4)
// ➔ PowerMod(1/2, 2, 4)
powerMod(3, 1/2, 7)
// ➔ PowerMod(3, 1/2, 7)
A list operand threads:
powerMod([2, 3], 2, 7)
// ➔ [4,2]
The rational exponent follows the Wolfram Language. The function works on big integers. It stays unevaluated, rather than give a wrong answer, when the modulus cannot be factored or a discrete logarithm is out of reach.
See also: Modular exponentiation.
MathJSON: PowerMod
["PowerMod", a, b, m].
["PowerMod", 2, 10, 1000]
// ➔ 24
["PowerMod", ["Rational", 1, 2], 2, 7]
// ➔ 2
["PowerMod", ["List", 2, 3], 2, 7]
// ➔ [4, 2]
["PowerMod", 4, ["Rational", 1, 2], 7]
// ➔ 2
powerModList
(base: list<rational> | rational, exponent: list<rational> | rational, modulus: integer | list<integer>) -> list<integer> | list<list<integer>>
base— The basea; a rationalp/qisptimes the modular inverse ofq. Sample:3.exponent— The exponents/r: the result lists thexwithx^rcongruent toa^s; an integer exponent gives one value. Sample:1/2.modulus— The modulusm, at least 1; the roots are in [0, m). Sample:11.
Return the sorted list of every x in [0, m) with x^r ≡ a^s (mod m), for the exponent s/r. An integer exponent gives the single value a^s mod m, a negative one using the modular inverse of a. A rational base is read as p times the inverse of q. A list operand threads: powerModList([2, 3], 2, 7) is [[4], [2]]. The list is empty when a^s is not an r-th power mod m. Undefined for a modulus m < 1, when the inverse of a does not exist, or when m cannot be factored or there are too many roots to list.
powerModList(3, 1/2, 11)
// ➔ [5,6]
powerModList(1, 1/3, 7)
// ➔ [1,2,4]
3 is not a square modulo 7, so it has no square root there. Modulo 8, 1 has four square roots.
powerModList(3, 1/2, 7)
// ➔ []
powerModList(1, 1/2, 8)
// ➔ [1,3,5,7]
As for powerMod, a rational base p/q is p times the inverse of q
modulo m, and a list operand threads:
powerModList(1/2, 2, 7)
// ➔ [2]
powerModList([2, 3], 2, 7)
// ➔ [[4],[2]]
The expression stays unevaluated when the modulus cannot be factored or the list would have more than 100000 entries.
MathJSON: PowerModList
["PowerModList", a, e, m].
["PowerModList", 3, ["Rational", 1, 2], 11]
// ➔ [5, 6]
["PowerModList", ["List", 2, 3], 2, 7]
// ➔ [[4], [2]]
primeFactors
(broadcastable<integer>) -> list<integer>
- argument 1 — The integer to factor. Its sign is ignored; it stays unevaluated at 0. Sample:
360.
Return the sorted list of distinct prime factors of an integer n. The sign of n is ignored; primeFactors(1) is the empty list.
primeFactors(360)
// ➔ [2,3,5]
MathJSON: PrimeFactors
["PrimeFactors", n].
["PrimeFactors", 360]
// ➔ [2, 3, 5]
primeNu
(broadcastable<integer>) -> integer
- argument 1 — The integer whose distinct prime factors are counted. Its sign is ignored; it stays unevaluated at 0. Sample:
360.
Return ω(n), the number of distinct prime factors of n. The sign of n is ignored; primeNu(1) is 0.
primeNu(360)
// ➔ 3
primeNu counts each prime factor once, primeOmega counts it as many times
as it divides n. 360 is 2³ · 3² · 5:
[primeNu(360), primeOmega(360)]
// ➔ [3,6]
See also: Prime omega function.
MathJSON: PrimeNu
["PrimeNu", n].
["PrimeNu", 360]
// ➔ 3
primeNumber
MathJSON PrimeNumber · (broadcastable<integer>) -> integer
- argument 1 — The rank of the prime, from 1; it stays unevaluated below 1. Sample:
10.
The nth prime number. primeNumber is an alias for nthPrime, which is the preferred name.
primeNumber(10)
// ➔ 29
primeOmega
(broadcastable<integer>) -> integer
- argument 1 — The integer whose prime factors are counted. Its sign is ignored; it stays unevaluated at 0. Sample:
360.
Return Ω(n), the number of prime factors of n counted with multiplicity. The sign of n is ignored; primeOmega(1) is 0.
primeOmega(360)
// ➔ 6
See also: Prime omega function.
MathJSON: PrimeOmega
["PrimeOmega", n].
["PrimeOmega", 360]
// ➔ 6
primePi
(broadcastable<real>) -> integer
- argument 1 — The upper bound, which need not be an integer; below 2 the count is 0. Sample:
10.
Return π(n), the prime-counting function: the number of primes less than or equal to n.
primePi(10)
// ➔ 4
n can be any real number:
[primePi(10.5), primePi(-3)]
// ➔ [4,0]
See also: Prime-counting function.
MathJSON: PrimePi
["PrimePi", n].
["PrimePi", 10]
// ➔ 4
primitiveRoot
(broadcastable<integer>) -> integer
- argument 1 — The modulus. Its sign is ignored; it stays unevaluated at 0. Sample:
7.
The smallest primitive root modulo n (a generator of the multiplicative group of integers mod n), or undefined if none exists (which happens unless n is 1, 2, 4, pᵏ, or 2pᵏ for an odd prime p). The sign of n is ignored, and primitiveRoot(1) is 0. Undefined for n = 0.
primitiveRoot(7)
// ➔ 3
The sign of n is ignored, because the unit group modulo -n is the unit
group modulo n. When there is no primitive root, the expression stays
unevaluated.
[primitiveRoot(-7), primitiveRoot(1)]
// ➔ [3,0]
[primitiveRoot(8), primitiveRoot(0)]
// ➔ [PrimitiveRoot(8),PrimitiveRoot(0)]
See also: Primitive root modulo n.
MathJSON: PrimitiveRoot
["PrimitiveRoot", n].
["PrimitiveRoot", 7]
// ➔ 3
["PrimitiveRoot", -7]
// ➔ 3
primitiveRootList
(broadcastable<integer>) -> list<integer>
- argument 1 — The modulus. Its sign is ignored; 0 gives the empty list. Sample:
7.
The sorted list of all primitive roots modulo n: the generators of the multiplicative group of integers mod n. The list is empty when there is none (unless n is 1, 2, 4, pᵏ, or 2pᵏ for an odd prime p), and for n = 0. primitiveRootList(1) is [0], as primitiveRoot(1) is 0. The sign of n is ignored. Undefined when n cannot be factored or there are too many roots to list.
primitiveRootList(7)
// ➔ [3,5]
primitiveRootList(8)
// ➔ []
primitiveRootList(25)
// ➔ [2,3,8,12,13,17,22,23]
[primitiveRootList(1), primitiveRootList(0), primitiveRootList(-7)]
// ➔ [[0],[],[3,5]]
MathJSON: PrimitiveRootList
["PrimitiveRootList", n].
["PrimitiveRootList", 25]
// ➔ [2, 3, 8, 12, 13, 17, 22, 23]
["PrimitiveRootList", 8]
// ➔ []
radical
(broadcastable<integer>) -> integer
- argument 1 — The integer whose radical is returned. Its sign is ignored; it stays unevaluated at 0. Sample:
360.
Return the radical of n (its square-free kernel): the product of its distinct prime factors. The sign of n is ignored; radical(1) is 1.
radical(360)
// ➔ 30
MathJSON: Radical
["Radical", n].
["Radical", 360]
// ➔ 30
randomPrime
(integer, integer?) random -> integer
- argument 1 — Alone, the upper bound
nof the range; with a second argument, its lower boundm(a lower bound below 2 is read as 2). Sample:100. - argument 2 — The upper bound
nof the range. Sample:200.
Return a random prime. randomPrime(n) draws a prime in [2, n]; randomPrime(m, n) draws a prime in [m, n]. Undefined if the range contains no prime.
randomPrime(100)
A range that holds no prime leaves the expression unevaluated:
randomPrime(24, 28)
// ➔ RandomPrime(24, 28)
randomPrime(10, 20)
MathJSON: RandomPrime
["RandomPrime", n], ["RandomPrime", m, n].
["RandomPrime", 100]
// ➔ 47 (for example)
rationalReconstruction
(a: broadcastable<integer>, modulus: broadcastable<integer>) -> rational
a— The residue to reconstruct. Sample:6.modulus— The modulusm, at least 1; it bounds the numerator and the denominator by the square root of(m - 1)/2. Sample:11.
The rational p/q with p ≡ a·q (mod m) and |p|, q ≤ ⌊√((m − 1)/2)⌋, the unique such fraction in lowest terms when it exists (Wang's algorithm). Undefined for m < 1 or when there is none.
rationalReconstruction(6, 11)
// ➔ 1/2
The function works on big integers. A result whose denominator is 1 is an integer.
rationalReconstruction(123456789, 1000000007)
// ➔ -161/81
rationalReconstruction(2, 11)
// ➔ 2
MathJSON: RationalReconstruction
["RationalReconstruction", a, m].
["RationalReconstruction", 6, 11]
// ➔ 1/2
sigma0
(broadcastable<integer>) -> integer
- argument 1 — The integer whose divisors are taken; it stays unevaluated below 1. Sample:
12.
Number of positive divisors of n.
sigma0(12)
// ➔ 6
The number of positive integers that divide n with no remainder. The
divisors of 6 are 1, 2, 3 and 6, so sigma0(6) is 4. It is
divisorSigma(0, n). For n ≤ 0, sigma0 stays unevaluated, while
divisorSigma ignores the sign of n:
[sigma0(-6), divisorSigma(0, -6)]
// ➔ [Sigma0(-6),4]
See also: Divisor function.
MathJSON: Sigma0
["Sigma0", n].
["Sigma0", 6]
// ➔ 4
sigma1
(broadcastable<integer>) -> integer
- argument 1 — The integer whose divisors are taken; it stays unevaluated below 1. Sample:
12.
Sum of positive divisors of n.
sigma1(12)
// ➔ 28
sigma1(6)
// ➔ 12
The sum of the positive divisors of n. The divisors of 6 are 1, 2, 3 and
6, so sigma1(6) is 12. It is divisorSigma(1, n).
sigma1(6)
// ➔ 12
See also: Divisor function.
MathJSON: Sigma1
["Sigma1", n].
["Sigma1", 6]
// ➔ 12
sigmaMinus1
(broadcastable<integer>) -> rational
- argument 1 — The integer whose divisors are taken; it stays unevaluated below 1. Sample:
12.
Sum of reciprocals of positive divisors of n.
sigmaMinus1(12)
// ➔ 7/3
sigmaMinus1(6)
// ➔ 2
The sum of the reciprocals of the positive divisors of n, an exact
rational. For n = 6 it is 1 + 1/2 + 1/3 + 1/6, which is 2: 6 is a perfect
number, and the sum is 2 for every perfect number. It is
divisorSigma(-1, n).
[sigmaMinus1(6), sigmaMinus1(12)]
// ➔ [2,7/3]
See also: Divisor function.
MathJSON: SigmaMinus1
["SigmaMinus1", n].
["SigmaMinus1", 6]
// ➔ 2
stirling
(n: broadcastable<integer>, m: broadcastable<integer>) -> integer
n— The number of elements, at least 0. Sample:5.m— The number of subsets, at least 0; pastnthe value is 0. Sample:2.
Stirling number of the second kind S(n, m): ways to partition n elements into m non-empty subsets.
stirling(5, 2)
// ➔ 15
There are 15 ways to split a set of 5 objects into 2 groups, none of them empty.
stirling(5, 2)
// ➔ 15
Outside the triangle (m > n) the value is 0, as for binomial, and so is
the value of stirlingS1. A negative or symbolic operand leaves the
expression unevaluated.
[stirling(3, 5), stirlingS1(3, 5)]
// ➔ [0,0]
[stirling(-1, 2), stirling(n, 2)]
// ➔ [Stirling(-1, 2),Stirling(n, 2)]
stirlingS2, the Wolfram Language name, is an alias: it becomes Stirling.
stirlingS2(n, k)
// ➔ Stirling(n, k)
See also: Stirling numbers of the second kind.
MathJSON: Stirling
["Stirling", n, m]. ["StirlingS2", n, m] is the same function.
["Stirling", 5, 2]
// ➔ 15
["StirlingS2", 6, 3]
// ➔ 90
stirlingS1
MathJSON StirlingS1 · (n: broadcastable<integer>, m: broadcastable<integer>) -> integer
n— The number of elements, at least 0. Sample:5.m— The number of cycles, at least 0; pastnthe value is 0. Sample:2.
Signed Stirling number of the first kind s(n, m): the coefficient of x^m in the falling factorial x(x−1)…(x−n+1). Its absolute value counts the permutations of n elements with exactly m disjoint cycles.
stirlingS1(5, 2)
// ➔ -50
stirlingS2
MathJSON StirlingS2 · (n: broadcastable<integer>, m: broadcastable<integer>) -> integer
n— The number of elements, at least 0. Sample:5.m— The number of subsets, at least 0; pastnthe value is 0. Sample:2.
stirlingS2 is an alias for stirling, which is the preferred name. Returns the Stirling number of the second kind S(n, m).
stirlingS2(6, 3)
// ➔ 90
totient
(broadcastable<integer>) -> integer
- argument 1 — The integer
n; φ(0) is 0, and the sign ofnis ignored. Sample:12.
Euler's totient function φ(n): count of positive integers ≤ n that are coprime to n, for n ≥ 1; φ(0) = 0 and φ(−n) = φ(n).
totient(12)
// ➔ 4
totient(36)
// ➔ 12
totient(n) counts the integers from 1 to n that have no common factor
with n other than 1. For n = 9, these are 1, 2, 4, 5, 7 and 8, so
φ(9) is 6.
totient(9)
// ➔ 6
[totient(0), totient(-12)]
// ➔ [0,4]
eulerPhi, the Wolfram Language name, is an alias: it becomes Totient.
eulerPhi(n)
// ➔ Totient(n)
See also: Euler's totient function.
MathJSON: Totient
["Totient", n]. ["EulerPhi", n] is the same function.
["Totient", 9]
// ➔ 6
["Totient", -12]
// ➔ 4
["EulerPhi", 12]
// ➔ 4