The mailbags that pile up
- O(log n) · Easy
- Free
- Python
- JavaScript
- operations
- search
- loops
Problem
Onésimo works the post office window of a town that just showed up on the map. On the first day first mailbags arrived, and every day after that more bags arrive than the day before: if 3 arrived on the first day and 2 more arrive each day, then 5 arrive on the second day, 7 on the third, 9 on the fourth, and so on. Nothing gets handed out yet, so the bags pile up in the storeroom.
Write a function that takes first (from 1 up), more (from 0 up) and limit (from 1 up), and returns the number of the day when the pile reaches the limit or goes past it. The first day is day 1.
With first equal to 3, more equal to 2 and a limit of 100: on day 1 there are 3 bags piled up, on day 2 there are 8, on day 3 there are 15... on day 9 there are 99, which is still not enough, and on day 10 there are 120. You return 10.
If more is 0 the same number of bags arrives every day. And if the first day already reaches the limit, you return 1.
first and more go up to a billion and the limit up to a trillion, that is, a million millions. With few bags a day that is hundreds of billions of days: adding them up day by day never ends.
What does come out of a plain count, without adding day by day, is how many bags are piled up on any given day. Go back to the example: with first equal to 3 and more equal to 2, by day 4 the bags that arrived were 3, 5, 7 and 9, and 24 are piled up. Those 24 are the 3 of the first day counted four times, which is 12, plus the increase that kept adding up: 0, 2, 4 and 6, another 12. And that 0, 2, 4, 6 does not have to be added one by one either. With that count you can split the range of days in half. But watch out: nobody here gave you a top day to split from, and you have to get one before you split anything.
Examples
The one from the example
3, 2, 100 → 10
The limit lands right on a day
3, 2, 99 → 9
The same bags every day
4, 0, 30 → 8
The first day is already enough
50, 10, 50 → 1
They grow fast
1, 3, 10 → 3
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def limit_day(first, more, limit):
passJavaScript
function limitDay(first, more, limit) {
}It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.