Implementation of Integer Partition Algorithm in C#
suggest changepublic static class IntegerPartition
{
public static long Count(int number, int largestPart)
{
if (number < 0)
throw new System.ArgumentOutOfRangeException(nameof(number));
if (largestPart < 0)
throw new System.ArgumentOutOfRangeException(nameof(largestPart));
var ways = new long[number + 1];
ways[0] = 1;
for (int part = 1; part <= System.Math.Min(number, largestPart); part++)
for (int total = part; total <= number; total++)
ways[total] = checked(ways[total] + ways[total - part]);
return ways[number];
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents