0

I was looking for a function that returns the partitions of an integer. I found almost what I wanted in this question:

Print all unique integer partitions given an integer as input

This function prints the partitions of an integer. But how can I change it in order to actually store and return all the partitions (maybe in a List)?

I'm aware that Java has the combinatoricslib. Is there something similar in C#? If not, how could I make that function return all possible partitions?

Community
  • 1
  • 1

1 Answers1

0

The link you gave are simply printing the result, but you want the result returned. The best way is likely to add an extra "result collector" parameter the "print" method, and have that method add to the collector instead of printing each partition.

Depending on what you want, your collector would be one of the following:

// Each partition is a string of the numbers, like when printed
List<String> partitions;

// Each partition is a list of the numbers
List<List<Integer>> partitions;
Andreas
  • 154,647
  • 11
  • 152
  • 247