Skip to content

Instantly share code, notes, and snippets.

@taku0
Created June 11, 2014 15:15
Show Gist options
  • Select an option

  • Save taku0/01820236c08a807ec423 to your computer and use it in GitHub Desktop.

Select an option

Save taku0/01820236c08a807ec423 to your computer and use it in GitHub Desktop.
def part(n, k)
if k == 1
[[n]]
else
0.upto(n).flat_map do |i|
part(n - i, k - 1).map do |partition|
partition.unshift(i)
end
end
end
end
puts(part(7, 4).map do |partition| partition.join(", ") end.join("\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment