From e9a72107edfb48572c0c723cd9f69ad09587df4a Mon Sep 17 00:00:00 2001 From: Vasily Guzov Date: Thu, 3 Oct 2024 13:04:36 +0300 Subject: [PATCH] [7kyu][fundamentals] Growth of a Population https://www.codewars.com/kata/563b662a59afc2b5120000c6 n 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. How many years does the town need to see its population greater than or equal to p = 1200 inhabitants? --- lib/nbYear.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lib/nbYear.ts diff --git a/lib/nbYear.ts b/lib/nbYear.ts new file mode 100644 index 0000000..675060b --- /dev/null +++ b/lib/nbYear.ts @@ -0,0 +1,18 @@ +export const nbYear = (p0: number, percent: number, aug: number, p: number): number => { + + let year = 0 + + while (p0 < p) { + p0 += Math.floor(p0 * percent / 100) + aug + year++ + } + + return year +} + +console.log( + nbYear(1500, 5, 100, 5000), 15 +) +console.log( + nbYear(1500000, 2.5, 10000, 2000000), 10 +)