Problem overview
The function sumUntilLimit receives a positive integer limit. Add the numbers 1, 2, 3, and so on, in this order, and stop as soon as the total is at least limit. Return how many numbers you added.
The total may overshoot the limit: the number that makes it reach or pass limit is the last one added, and it counts. With limit = 10, 1 + 2 + 3 + 4 is exactly 10, so the answer is 4. With limit = 11, the total after 4 numbers is 10, still below 11, so 5 is added too, the total becomes 15, and the answer is 5. With limit = 1, the first number already reaches the limit, so the answer is 1.
Approach
You can't know in advance how many numbers it will take, so the repeat is driven by a condition: keep adding while the total is below the limit. Go has no while; a for loop with only a condition does the same thing: for total < limit.
Two variables carry the state. total is the sum so far, and count is how many numbers have been added. Because the numbers go 1, 2, 3 in order, count is also the last number added: after adding 1, 2 and 3, it is 3. So the next number to add is always count + 1, and the loop can use count as both the counter and the number.
For limit = 11 the values after each pass are:
| Pass | count | total |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
After pass 5 the total, 15, is no longer below 11, so the loop ends and the function returns 5.
Step by step
- Create
totalwith:=, starting at 0. Nothing has been added yet. - Create
countwith:=, starting at 0. No numbers have been added. - Loop while
total < limit. The loop stops exactly when the total is at leastlimit, which is when adding must stop. - Add 1 to
countwith++. It is now the next number: 1 on the first pass, 2 on the second. - Add
counttototal. The order matters:countgoes up first, so the number added is the next one in the sequence. - Return
countafter the loop.
The solution in Go
func sumUntilLimit(limit int) int {
total := 0
count := 0
for total < limit {
count++
total += count
}
return count
}
Go details:
- The condition is checked before every pass. That is why the pass that reaches the limit still runs fully: the check happens at the top, and a total that is still below
limitlets the next pass start. - The type is
int. It is 64 bits, so the total, which never goes far pastlimit, can't overflow for limits up to a billion.
Complexity
After k passes the total is about k squared over 2, so the loop needs roughly the square root of 2 * limit passes. For limit = 1000000000 it runs 44721 times. The space is constant.
Edge cases and common mistakes
- Reaching the limit exactly.
limit = 10stops at 4 numbers, because the conditiontotal < limitis false when the total equals the limit. Writingtotal <= limitwould add a fifth number. - Overshooting. For 11, the answer counts the number that carried the total past the limit.
- Swapping the two lines in the loop. If
total += countruns beforecount++, the first number added is 0 and every number is one too small. - Returning
total. The question asks how many numbers were added, not their sum.
Testing
10gives 4.11gives 5.1gives 1.1000000000gives 44721.
