Skip to content

Commit 763913c

Browse files
committed
doc: update doc and examples for partitions(n::Integer)
1 parent 0bd13b1 commit 763913c

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

src/partitions.jl

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export
88
#nextprod,
99

1010

11-
#integer partitions
11+
# integer partitions
1212

1313
struct IntegerPartitions
1414
n::Int
@@ -24,17 +24,56 @@ function Base.iterate(p::IntegerPartitions, xs = Int[])
2424
end
2525

2626
"""
27-
partitions(n)
27+
partitions(n::Integer)
2828
29-
Generate all integer arrays that sum to `n`. Because the number of partitions can be very
30-
large, this function returns an iterator object. Use `collect(partitions(n))` to get an
31-
array of all partitions. The number of partitions to generate can be efficiently computed
32-
using `length(partitions(n))`.
29+
Generate all integer arrays that sum to `n`.
30+
31+
Because the number of partitions can be very large,
32+
this function returns an iterator object.
33+
Use `collect(partitions(n))` to get an array of all partitions.
34+
35+
The number of partitions to generate can be efficiently computed using
36+
`length(partitions(n))`.
37+
38+
See also:
39+
- [`integer_partitions(n::Integer)`](@ref)
40+
for a non-iterator version that returns all partitions as a array
41+
- [`partitions(n::Integer, m::Integer)`](@ref)
42+
for partitions with exactly `m` parts.
43+
44+
## Examples
45+
```jldoctest
46+
julia> collect(partitions(2))
47+
2-element Vector{Vector{Int64}}:
48+
[2]
49+
[1, 1]
50+
51+
julia> collect(partitions(3))
52+
3-element Vector{Vector{Int64}}:
53+
[3]
54+
[2, 1]
55+
[1, 1, 1]
56+
57+
julia> integer_partitions(3)
58+
3-element Vector{Vector{Int64}}:
59+
[1, 1, 1]
60+
[2, 1]
61+
[3]
62+
63+
julia> first(partitions(10))
64+
1-element Vector{Int64}:
65+
10
66+
67+
julia> length(partitions(10))
68+
42
69+
```
70+
71+
# References
72+
- [Integer partition - Wikipedia](https://en.wikipedia.org/wiki/Integer_partition)
3373
"""
3474
partitions(n::Integer) = IntegerPartitions(n)
3575

3676

37-
3877
function nextpartition(n, as)
3978
isempty(as) && return Int[n]
4079

0 commit comments

Comments
 (0)