Problem
- In a small town the population is ‘p0 = 1000’ at the beginning of a year.
- The population regularly increases by ‘2 percent’ per year and moreover ‘50’ new inhabitants per year come to live in the town.
- 인구는 매년 ‘2 퍼센트’씩 증가하고, 매년 새로운 주민이 ‘50명’씩 살게 된다.
- How many years does the town need to see its population greater or equal to ‘p = 1200’ inhabitants?
- 인구가 ‘p = 1200’보다 크거나 같으려면 몇 년이 지나야 하는가?
Solution 01
function nbYear(p0, percent, aug, p) {
let years = 0;
while (p0 < p) {
p0 += p0 * percent/100 + aug;
years++;
}
return year;
}
nbYear(1000, 2, 50, 1200); // 3
nbYear(1500, 5, 100, 5000); // 15
Solution 02
function nbYear(p0, percent, aug, p) {
if (p0 >= p) {
return 0;
} else {
return 1 + nbYear(p0 + (p0 * percent) / 100 + aug, percent, aug, p);
}
}
nbYear(1000, 2, 50, 1200); // 3
nbYear(1500, 5, 100, 5000); // 15