List the partitions of an integer - MATLAB Cody - MATLAB Central

Problem 60751. List the partitions of an integer

Difficulty:Rate
Several Cody problems deal with partitions of an integer, or the ways an integer n can be written as the sum of positive numbers—either all integers up to and including n or a subset of those numbers.
Most of the problems involve counting partitions. Cody Problem 1873 counts the partitions of an integer, while Cody Problem 42918 counts the partitions with a given number of parts. Cody Problem 45429 restricts the parts to primes; Cody Problem 1240 restricts them to U.S. coin denominations; and Cody Problem 54740 restricts them to American football scores.
A couple of problems involves listing partitions. Cody Problem 590 asks us to list one partition of a number, without repeating parts, and Cody Problem 59586 involves listing ways to make a sum in Killer Sudoku.
This problem involves listing all partitions of an integer. For example, 5 = 4+1 = 3+2 = 3+1+1 = 2+2+1 = 2+1+1+1 = 1+1+1+1+1. Notice that rearrangements of a partition are not included.
Write a function to list partitions of an integer. Return the partitions in a cell array and sort them as in the example—i.e., from larger to smaller numbers. For n = 5, the required output is
{[5] [4 1] [3 2] [3 1 1] [2 2 1] [2 1 1 1] [1 1 1 1 1]}

Solution Stats

100.0% Correct | 0.0% Incorrect
Last Solution submitted on Dec 18, 2024

Problem Comments

Solution Comments

Show comments
Join Cody Contest 2025 — Have Fun and Win Prizes!
...
We’re excited to invite you to Cody Contest 2025! 🎉 Pick a team,...

Problem Recent Solvers5

Suggested Problems

More from this Author314

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!