function to generate collections of integers that all sum to the same value?

2 ビュー (過去 30 日間)
mark palmer
mark palmer 2023 年 12 月 21 日
コメント済み: John D'Errico 2023 年 12 月 21 日
Here's one for you number freaks.I am looking for a Matlab function that generates all ordered combinations of integers that add up to a certain value. For instance, in the case of 4,
1 1 1 1
1 1 2, 1 2 1, 2 1 1
1 3, 3 1
2 2
4
In the case of 5,
1 1 1 1 1
1 1 1 2, 1 1 2 1, 1 2 1 1, 2 1 1 1
1 2 2, 2 1 2, 2 2 1
1 1 3, 1 3 1, 3 1 1
1 4, 4 1
2 3, 3 2
5
Etc., all the way up to 8, or even 16 if that is possible.

採用された回答

Bruno Luong
Bruno Luong 2023 年 12 月 21 日
Look in File Exchange with the keywords integer partition. You'ill find many implementations for example this one:
  2 件のコメント
mark palmer
mark palmer 2023 年 12 月 21 日
Thanks, I didn't realize this was a common question.
John D'Errico
John D'Errico 2023 年 12 月 21 日
Be careful, as even relatively small numbers will have an immense number of partitions. By my count, 100 has 190569292 distinct ways to perform the sum, and 1000 has 24061467864032622473692149727991 distinct ways.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by