Skip to content

Bugfix: let integer_partitions(0) == [Int[]] #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/partitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ function integer_partitions(n::Integer)
if n < 0
throw(DomainError(n, "n must be nonnegative"))
elseif n == 0
return Vector{Int}[]
return Vector{Int}[[]]
elseif n == 1
return Vector{Int}[[1]]
end
Expand Down
12 changes: 11 additions & 1 deletion test/partitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
end

@testset "integer partitions" begin
@test_broken integer_partitions(0) == [[]]
@test integer_partitions(0) == [Int[]]
@test integer_partitions(1) == [[1]]
@test integer_partitions(2) == [[1, 1], [2]]
@test integer_partitions(3) == [[1, 1, 1], [2, 1], [3]]
Expand All @@ -113,6 +113,16 @@
# integer_partitions <--> partitions(::Integer)
@test Set(integer_partitions(5)) == Set(partitions(5))

# Partition function p(n): https://oeis.org/A000041
@test length(integer_partitions(0)) == 1
@test length(integer_partitions(1)) == 1
@test length(integer_partitions(2)) == 2
@test length(integer_partitions(3)) == 3
@test length(integer_partitions(4)) == 5
@test length(integer_partitions(5)) == 7
@test length(integer_partitions(40)) == 37338
@test length(integer_partitions(49)) == 173525

@test_throws DomainError integer_partitions(-1)
end

Expand Down
Loading