Need help with unique integer partitions?

  • Context: Undergrad 
  • Thread starter Thread starter rsq_a
  • Start date Start date
  • Tags Tags
    Integer partitions
Click For Summary

Discussion Overview

The discussion revolves around generating unique integer partitions of a given integer N into M parts, with the requirement that each part is strictly positive and unique. Participants explore various methods and approaches to implement this in programming, including recursive and non-recursive strategies.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes the challenge of generating unique partitions and provides examples for N = 10 and M = 2.
  • Another participant points out that the example provided misses the partition 10 = 5 + 5 and questions the meaning of "systematically shifting the sequence."
  • A suggestion is made to introduce a third variable in the function to limit the size of the parts in the partitions.
  • Further elaboration on the need for actual lists of elements in partitions and the requirement that elements can only be repeated once is presented.
  • One participant proposes a method to shift numbers in the partitions systematically to generate new partitions, while another argues that recursion is not necessary for this task.
  • An alternative approach is suggested involving the enumeration of increasing sequences or sequences of positive numbers that satisfy specific summation conditions.
  • A generalization is proposed for a function that generates the next partition from a given array, which may simplify the implementation.
  • A participant mentions using high-level languages like Mathematica to achieve the desired output with a simple function.
  • The original poster shares that they successfully wrote the code after considerable effort in understanding function usage.

Areas of Agreement / Disagreement

Participants express differing opinions on the necessity of recursion versus iterative methods for generating partitions. There is no consensus on the best approach, and multiple strategies are discussed without resolution.

Contextual Notes

Some participants highlight the complexity of generating unique partitions, particularly with larger values of M, and the potential for confusion in defining the requirements for uniqueness and positivity in the parts.

rsq_a
Messages
103
Reaction score
1
This is causing me a bigger headache than I anticipated.

Basically, given an integer N and a number M, I need a list of all the possible integer partitions of N into M parts such that each part is strictly positive and each part is UNIQUE. I don't want repetitions. Just unique ones.

So for example, with N = 10 and M = 2

1 + 9
2 + 8
3 + 7
4 + 6

are the only possibilities.

I tried to write a code to do it, but just ran into huge headaches because of the necessary recursion. With M > 2, you need to worry about systematically shifting the sequence as you go. Can anybody make a suggestion?
 
Last edited:
Mathematics news on Phys.org
You forgot 10 = 5 + 5...


What do you mean by "systematically shifting the sequence"?



Anyways, you might want a third variable. You want a function

partitions(N, M, B)

that returns all partitions of N that have exactly M parts, each of which is smaller than B.
 
Hurkyl said:
You forgot 10 = 5 + 5...What do you mean by "systematically shifting the sequence"?
Anyways, you might want a third variable. You want a function

partitions(N, M, B)

that returns all partitions of N that have exactly M parts, each of which is smaller than B.

It's not just the number of partitions I want. It's the actual list of elements. Also, I forgot to mention above that I need partitions in which element is repeated only once.

So if I wanted to do m = 4, N = 20, I'd need to do:

Shift the 2-last

1 + 2 + 3 + 14
1 + 2 + 4 + 13
1 + 2 + 5 + 12
...

Shift the 2nd number up

1 + 3 + 4 + 12
1 + 3 + 5 + 11
1 + 3 + 6 + 10
...

Shift the 2nd number up again
1 + 4 + 5 + 10
1 + 4 + 6 + 9
1 + 4 + 7 + 8

Shift the 2nd number up again
1 + 5 + 6 + 8

Shift the 1st number up
2 + 3 + 4 + 11
...And so on. Can someone recommend an easier way to do this in terms of implementation into a program? The above requires some really annoying recursive loops.
 
rsq_a said:
It's not just the number of partitions I want. It's the actual list of elements. Also, I forgot to mention above that I need partitions in which element is repeated only once.

So if I wanted to do m = 4, N = 20, I'd need to do:

Shift the 2-last

1 + 2 + 3 + 14
1 + 2 + 4 + 13
1 + 2 + 5 + 12
...

Shift the 2nd number up

1 + 3 + 4 + 12
1 + 3 + 5 + 11
1 + 3 + 6 + 10
...

Shift the 2nd number up again
1 + 4 + 5 + 10
1 + 4 + 6 + 9
1 + 4 + 7 + 8

Shift the 2nd number up again
1 + 5 + 6 + 8

Shift the 1st number up
2 + 3 + 4 + 11
...


And so on. Can someone recommend an easier way to do this in terms of implementation into a program? The above requires some really annoying recursive loops.
What's wrong with exactly what you said? No recursion is necessary; each time you want to move to the next partition, you simply work from the end to figure out which number needs to be increased... and I don't see what's so annoying about a recursive implementation of the same method.
 
Oh, by the way, it might be easier to enumerate equivalent things, such as:
The list of all increasing sequences of M-1 numbers whose sum S is less than N - {the last number}​

Or even
The list of all sequences of M positive numbers such that the sum x1 + 2x2 + 3x3 + ... + MxM = N​

This latter one is formed by taking the deltas in a partition and reversing the order: e.g. the deltas of the partition (2, 7, 16) would be (2, 5, 9), and reversing would be (9, 5, 2). And you can check 9 + 2*5 + 3*2 = 2 + 7 + 16.

Better yet,
The list of all sequences of M nonnegative numbers such that the sum x1 + 2x2 + 3x3 + ... + MxM = N - M(M+1)/2​

This one has subtracted one from each entry, so that they start counting at 0 instead of 1.
 
Generalizing a bit...


Rather than a function that enumerates all partitions, it might be better to write the following function

int next_partition(unsigned int *start, size_t len);

which, assuming that the len-long array start is a partition, will replace it with the next partition, and return 1. (Or return 0 if you're already at the last one)


It might be easier to think through how to write this function properly, and it's probably a lot simpler to use in an actual program.
 
If you don't care about computer cycles then you could use Mathematica (or some other high-level language) and say:

Code:
Input:
f = Function[{n, m},
   Select[Sort /@ IntegerPartitions[n], 
    Length[#] === m && Union@# === # &]
   ];
f[10, 2]

Output:

{{1, 9}, {2, 8}, {3, 7}, {4, 6}}
 
Thanks for the help. I finally wrote the code to do it (~30 lines).

Funny. It took me ages to wrap my head around how to use functions of functions in a computer program. First time for everything, I guess.
 

Similar threads

Replies
7
Views
2K
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
10
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K