Calculating Population Growth: A JavaScript Approach
Written on
Chapter 1: Introduction to Population Growth
This article addresses the calculation of growth rates, population dynamics, and numerical trends. More specifically, it explores how to predict the population increase of a small town.
The Challenge: Understanding Population Growth
In a small town, the initial population is p0 = 1000 at the start of the year. The population grows by 2% annually, and additionally, 50 new residents move in each year. The question is: how many years will it take for the population to reach or exceed p = 1200 inhabitants?
At the end of the first year, the calculation yields:
1000 + (1000 * 0.02) + 50 = 1070 inhabitants
At the conclusion of the second year:
1070 + (1070 * 0.02) + 50 = 1141 inhabitants
By the end of the third year:
1141 + (1141 * 0.02) + 50 = 1213 inhabitants
Thus, it takes a total of 3 years to achieve the target population.
In a more general sense, given the parameters: p0, percent, aug (the influx or outflow of residents each year), and p (the target population), the function nb_year should calculate the number of full years required for the population to meet or exceed p. The variables aug and p0 must be positive integers, while percent can be zero or a positive float.
Examples:
- nb_year(1500, 5, 100, 5000); // Expected output: 15
- nb_year(1500000, 2.5, 10000, 2000000); // Expected output: 10
My Implementation
I approached this problem using two different methods. The first solution employs a straightforward while loop that continues until the target population is met.
export const nbYear = (
p0: number,
percent: number,
aug: number,
p: number
): number => {
let pop = p0;
let perc = percent / 100;
let year = 0;
while (pop < p) {
pop = pop + pop * perc + aug;
pop = Math.trunc(pop);
++year;
}
return year;
};
The key challenge lies in understanding that populations must be represented as whole numbers. Thus, I use the Math.trunc() method to discard any decimal places that would imply a non-integer number of inhabitants.
A more concise version of the function can be written as follows:
function nbYear(p0, percent, aug, p) {
let year = 0;
while (p0 < p) {
p0 = Math.trunc(p0 + (p0 * percent) / 100 + aug);
++year;
}
return year;
}
Complex Solution Using Recursion
The second solution is somewhat more intricate, utilizing a recursive function that calls itself until the population meets the required condition.
export const nbYear = (
p0: number,
percent: number,
aug: number,
p: number
): number => {
if (p0 >= p) return 0;
return (
1 + nbYear(Math.trunc(p0 + p0 * (percent / 100) + aug), percent, aug, p));
};
For clarity, I can enhance this function by splitting it into two distinct parts:
const calculatePop = (p0, percent, aug, p) =>
Math.trunc(p0 + p0 * (percent / 100) + aug);
function nbYear(p0, percent, aug, p) {
const annualCount = calculatePop(p0, percent, aug, p);
if (annualCount >= p) return 1;
return 1 + nbYear(annualCount, percent, aug, p);
}
Conclusion
The challenge presented today is relatively simple. I opted to tackle it using two different strategies: one straightforward and the other more complex. I hope you found this exploration engaging. Stay tuned for more insights in future articles!
The first video titled "Codewars 7 kyu Growth of a Population JavaScript - YouTube" dives deeper into the problem and provides additional insights into the solution.
The second video, "Growth of a Population - Python CodeWars Problem - YouTube," offers a different perspective, showcasing a solution in Python.