PartitionsQ¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
PartitionsQ[n] gives the number q(n) of partitions of the integer n into distinct parts -- equal, by Euler's theorem, to the number of partitions into odd parts.
Notes
n must be an integer; q(n) = 0 for n \< 0. Threads over lists. For the partitions themselves use IntegerPartitions.Examples (6)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (3)¶
In[1]:= Table[PartitionsQ[k], {k, 0, 20}]
Out[1]= {1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 15, 18, 22, 27, 32, 38, 46, 54, 64}
In[2]:= PartitionsQ[100]
Out[2]= 444793
In[3]:= PartitionsQ[{2, 4, 6}]
Out[3]= {1, 2, 4}
Worked examples (1)¶
Applications (2)¶
Algorithm¶
partitions.c — IntegerPartitions
A faithful, efficient recreation of the Wolfram-Language IntegerPartitions. The whole surface collapses onto a single count-vector enumerator over an ordered set of allowed parts, run with exact GMP rational arithmetic so that integers, big integers, rationals and negative values are all handled by the same code path.
Forms:
IntegerPartitions[n] all partitions of n
IntegerPartitions[n, k] into at most k parts
IntegerPartitions[n, {k}] into exactly k parts
IntegerPartitions[n, {kmin, kmax}] between kmin and kmax parts
IntegerPartitions[n, {kmin, kmax, dk}] kmin, kmin+dk, ... parts
IntegerPartitions[n, kspec, sspec] parts drawn only from sspec
IntegerPartitions[n, kspec, sspec, m] first m (m>0) or last |m| (m<0)
n and the s_i may be rational and/or negative. Results are in reverse lexicographic order; within a partition the parts appear in the order of the reversed sspec (descending for the default Range[n]).
Ownership: this builtin only reads res. On every NULL return (bad arguments, ::undef, symbolic input) the evaluator keeps res unevaluated.
Implementation notes¶
Protected,Listable—PartitionsQ[{2, 4, 6}]→{1, 2, 4}.- Two engines, dispatched by the size of
n(thresholdn = 1000): - Small
n— an exact GMP recurrence derived from the Euler identity∏(1−x^k)·∏(1+x^k) = ∏(1−x^{2k}):q(m) = r(m) + Σ_k (−1)^(k−1) [q(m − g_k) + q(m − g_k')]over the same generalized pentagonal numbersg_k = k(3k∓1)/2asPartitionsP, with an inhomogeneous termr(m) = (−1)^kwhenm/2is the generalized pentagonal numberg_k(else0). O(n^1.5) integer additions; O(n·√n)-bit table. - Large
n— the Hardy–Ramanujan–Rademacher / Hagis exact convergent series evaluated in MPFR (Hagis, Amer. J. Math. 85 (1963) 213–222):q(n) = (π/√(24n+1)) · Σ_{k odd} (1/k)·A_k(n)·I₁(π√(48n+2)/(12k)), summing over oddkonly, with the modified Bessel functionI₁(computed by its everywhere-positive power series, as MPFR has noI₁) and the character sumA_k(n) = Σ_{gcd(h,k)=1} cos(π(s(h,k) − s(2h,k)) − 2πnh/k)built from exact Dedekind sumss(h,k). Working precision≈ π√(n/3)/ln 2bits plus guard bits; memory stays tiny where the recurrence's table would be prohibitive. - Unlike
PartitionsP, the q-series has no clean closed-form Rademacher remainder bound, so the HRR engine's term count is convergence-driven (sum≈√nodd terms; accept only when the sum lies within1/4of an integer and the last term is below1/8, growing terms/precision otherwise). Correctness is pinned by an exhaustive HRR-equals-recurrence cross-check (everynup to 1500 plus a strided sample and large spot values ton = 100000; seetests/test_partitionsq.c). q(0) = 1;q(n) = 0forn < 0. The result auto-promotes to a big integer (q(1000)has 22 digits;q(100000)has 245).- Non-integer, symbolic, or astronomically large (big-integer) arguments are
left unevaluated;
PartitionsQcalled with other than one argument emitsPartitionsQ::argxand is left unevaluated.
Attributes: Listable, Protected.
References¶
See also: IntegerPartitions, PartitionsP
- G. E. Andrews, The Theory of Partitions, Cambridge University Press, 1998 — distinct-parts partitions and Euler's identity.
- Source:
src/info.c - Specification:
docs/spec/builtins/number-theory.md - Tests:
tests/test_partitionsq.c
Notes & additional examples¶
Distinct parts equal odd parts¶
PartitionsQ[n] counts the partitions of n into distinct parts. Euler's celebrated
identity says this equals the number of partitions of n into odd parts — the
generating-function identity ∏ (1 + x^k) = ∏ 1/(1 - x^(2k-1)). Contrast
PartitionsP, which counts all partitions.