23 Oct 12, 01:57AM
(20 Oct 12, 10:12PM)Roflcopter Wrote: Bonus marks if you can find a simple method to find the actual consecutive numbers.
I guess we didn't explicitly do this:
Say you want to find a sequence of x consecutive numbers that total up to y.
If x is an odd number, y must be divisible by x for there to be a possible solution for x numbers.
e.g. find out which 3 numbers can make 21.
21/3 = 7 -- then you add an equal amount of numbers on both sides of this number until you have a sequence of x numbers and there's your answer.
6...7...8 (6+7+8=21)
If x is even, it's a little different. The quotient of y/x must be exactly between two integers. You can double y temporarily and half the quotient to see if it is between two integers (basically 2y/x must be an odd number)
e.g. find out which 4 numbers can make 14.
14/4 = 3.5 OR 14*2(28) / 4 (7) / 2 (3.5)
The middle numbers of your sequence will be Q - 0.5, Q + 0.5, where Q is the quotient to the above problem (when it is exactly between two integers)
3...4
Add numbers to both sides until you have a sequence of x numbers:
2...3...4...5 (2+3+4+5=14)