Trying to fix this problem, reverse add

So here's what we know. 1) Numbers need to be unique, so you can't just string together all 1's to give you 3, like 1+1+1. 2) Numbers need to be in increasing order and can't add up past the input.

Given these two conditions all you need to do, which might not be the most efficient way to go about this is to do the following:

Example input 5.

Start with

1, is 1 larger than the input? No, ok add next number

2, is 1+2 larger than the input? No, ok add next number.

3, is 1+2+3 larger than the input? Yes, ok so now we know 1 isn't in the answer, so scratch off 1 and start with 2 from the very beginning.

2, is 2 larger than the input? No. go to next number.

3, is 2+3 larger than the input? No but it is equal to the input! Therefore stop the loop and exit.

2+3 is now your answer.

/r/learnpython Thread