Skip to content

Commit c0f5dbc

Browse files
daviddelaatandrioni
authored andcommitted
Add partitions function
1 parent bd88b64 commit c0f5dbc

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Catalan.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Catalan
2+
23
using Polynomial
34

45
export bell,
@@ -15,7 +16,8 @@ export bell,
1516
multinomial,
1617
primorial,
1718
stirlings1,
18-
subfactorial
19+
subfactorial,
20+
partitions
1921

2022
include("youngdiagrams.jl")
2123

@@ -149,4 +151,22 @@ function stirlings1(n::Integer, k::Integer)
149151
p[n - k + 1]
150152
end
151153

154+
# Lists the partitions of the number n, the order is consistent with GAP
155+
function partitions(n)
156+
if n == 1
157+
return Vector{Int}[[1]]
158+
end
159+
160+
list = Vector{Int}[]
161+
162+
for p in partitions(n-1)
163+
push!(list, [p, 1])
164+
if length(p) == 1 || p[end] < p[end-1]
165+
push!(list, [p[1:end-1], p[end]+1])
166+
end
167+
end
168+
169+
list
170+
end
171+
152172
end

0 commit comments

Comments
 (0)