16th April 2011 - 5 minutes read time
I was helping my son out with his maths homework today and we came across a question that asked him to find some combinations of two numbers that added up to a given number (he is only 6 so this was good maths practice). This got me to thinking about how to calculate all possible ways in which two numbers could be added together to make another number.
The only rule had to be in there was that doing things like 1+11=12 and 11+1=12 are basically the same thing and so would be cheating. In which case I realised that it would only be necessary to use half of the numbers when creating the list, so for the number 12 every combination can be found using the numbers 0-6 because after this we start duplicating the sums, just swapping the order.
Here is the function I came up with.