Need help with unique integer partitions?

In summary, the conversation discussed the need for a list of all possible integer partitions of a given number N into a given number M of parts, with each part being unique and strictly positive. The process of systematically shifting the sequence as the number of partitions increases was mentioned. Suggestions were made to use a third variable or to enumerate equivalent things to simplify the implementation into a program. The use of a function to enumerate the partitions was also recommended. The conversation concluded with the successful writing of the code to achieve the desired result.
  • #1
rsq_a
107
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 
  • #7
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}}
 
  • #8
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.
 

1. What is the definition of "partitions of an integer"?

Partitions of an integer refers to the different ways in which a positive integer can be expressed as a sum of smaller positive integers.

2. How many partitions does a given integer have?

The number of partitions of an integer n can be calculated using the partition function, denoted as p(n). There is no known formula for calculating p(n), but it can be found using various mathematical techniques.

3. Are there any patterns in the partitions of an integer?

Yes, there are several patterns and properties that have been discovered in the study of partitions of integers. For example, the number of partitions of an integer can be related to the number of divisors of that integer.

4. What is the significance of studying partitions of integers?

Partitions of integers have applications in various fields such as number theory, combinatorics, and computer science. They also have connections to other mathematical concepts, such as the Fibonacci sequence and the binomial coefficients.

5. Can all integers be partitioned in a unique way?

No, not all integers have a unique partition. Some integers, such as prime numbers, can only be partitioned in one way (themselves). However, there are infinitely many integers that have unique partitions.

Similar threads

Replies
5
Views
2K
  • General Math
Replies
2
Views
1K
  • General Math
Replies
1
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
12
Views
965
Replies
6
Views
1K
  • Math Proof Training and Practice
Replies
10
Views
2K
Replies
7
Views
1K
  • Linear and Abstract Algebra
Replies
6
Views
1K
  • General Math
Replies
4
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top