Algorithm for finding numbers that sum up to a certain integer

301 Views Asked by At

I am looking to create an algorithm that produces all of the numbers that sum to a number under certain rules.

  1. There should be exactly two different natural numbers that are used for the summation. (For our purpose, 4 is a different number than 2 even though it can be expressed as 2+2)
  2. Every number must appear at least twice.
  3. No other numbers can be used

End of question - The following section is providing examples and clarifications.

So if I was looking for the numbers that satisfy the rules for the number 12, I would have:

2+2+4+4 // Two 2's and two 4's
2+2+2+3+3 // Three 2's and two 3's
5*2+2*1 //Five 2's and two 1's
4*2+4*1 //Four 2's and four 1's
3*2+6*1 // Three 2's and six 1's
2*2+8*1 //Two 2's and eight 1's
2*4+4*1 //Two 4's and four 1's

These will not satisfy the rules:

4+3+3+2 // Fails rules 1 and 2
3+3+6 //Fails rule 2
1+1+2+2+3+3 //Fails rule 1
1+1+2+2 //doesn't add up to 12

What kind of strategies can I adopt, to avoid having to "brute-force" numbers until I find a pattern that fits?

For instance:

  • if the sum of both numbers*2>goal ---> they get automatically disqualified
  • if my goal is even, and I have an odd number of odd components---->they get automatically disqualified

What other things I may want to consider as I create this algorithm?