The growing colony
- O(log n) · Easy
- Free
- Python
- JavaScript
- division
- loops
Problem
Nobody on the Aurora colony, on a moon of Jupiter, counts as half a person. Every year new settlers arrive: pct % of the population at that moment, rounded down. And since some ship always shows up, if that comes out to 0, 1 settler arrives that year.
Write a function that takes start, pct and goal (whole numbers, with start and goal 1 or more and pct from 1 to 100) and returns how many years pass until the population is goal or more.
With 100, 10 and 150: 100 + 10 = 110, 110 + 11 = 121, 121 + 12 = 133 (10 % of 121 is 12.1, which becomes 12), 133 + 13 = 146 and 146 + 14 = 160: 5 years. With 10 people and 3 %, the count is 0.3, which becomes 0, so 1 arrives. If start is already goal or more, it is 0 years.
Examples
The example
100, 10, 150 → 5
Doubles every year
1, 100, 1024 → 10
Lands exactly on the goal
30, 10, 60 → 8
Rounding down adds up
200, 7, 1000 → 25
At least one always arrives
10, 3, 15 → 5
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def years_to_goal(start, pct, goal):
passJavaScript
function yearsToGoal(start, pct, goal) {
}It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.