DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #197 - Population Growth

Setup

Implement a function to calculate the growth of a population of people. The function should be able to take several parameters, including p0(starting population), percent, aug(inhabitants coming or leaving each year), and p(population to surpass). This function should output n(number of years needed to get a population of p).

Don't forget to convert the percent parameter as a percentage in the body of your function: if the parameter percent is 2 you have to convert it to 0.02.

Example

In our example, 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 need to pass for the town to see its population greater or equal to p = 1200 inhabitants?

At the end of the first year there will be: 
1000 + 1000 * 0.02 + 50 => 1070 inhabitants

At the end of the 2nd year there will be: 
1070 + 1070 * 0.02 + 50 => 1141 inhabitants (number of inhabitants is an integer)

At the end of the 3rd year there will be:
1141 + 1141 * 0.02 + 50 => 1213

It will need 3 entire years.

Tests

nbYear(1500, 5, 100, 5000)
nbYear(1500000, 2.5, 10000, 2000000)
nbYear(1500000, 0.25, 1000, 2000000)


This challenge comes from g964 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (8)

Collapse
 
avalander profile image
Avalander • Edited

Haskell

module PopGrowth (
    nbYear
) where


nbYear :: Integral a => a -> Double -> a -> a -> a
nbYear = nbYear' 1

nbYear' :: Integral a => a -> a -> Double -> a -> a -> a
nbYear' year current percent aug target
    | current >= target = year
    | otherwise         = nbYear' (year + 1) next percent aug target
    where
        perone = percent / 100
        inc    = round (perone * (fromIntegral current))
        next   = current + inc + aug

-- Output
nbYear 1500 5.0 100 5000          -- 15
nbYear 1500000 2.5 10000 2000000  -- 10
nbYear 1500000 0.25 1000 2000000  -- 94
Collapse
 
vidit1999 profile image
Vidit Sarkar

Starting population : p0
Percent : pct
Inhabitants coming or leaving each year : aug
Population to surpass : p

After n years the total population will be :
(p0 + aug/pct) * ( (1 + pct)n ) - aug/pct
where aug is converted from percentage i.e. from 5% to 0.05

So, n >= (log((aug + pct*p) / (aug + pct*p0) ) / log(1+pct))

Here is the C++ solution

int nbYear(long p0,long double pct, long aug, long p){
    pct /= 100;
    return ceil(log((aug+pct*p)/(aug+pct*p0))/log(1+pct));
}

cout << nbYear(1000,2,50,1200) << "\n";
cout << nbYear(1500,5,100,5000) << "\n";
cout << nbYear(1500000,2.5,10000,2000000) << "\n";
cout << nbYear(1500000,0.25,1000,2000000) << "\n";

Output :

3
15
10
94
Collapse
 
aminnairi profile image
Amin • Edited

Elm

I was too lazy, just put floats everywhere.

module Main exposing (populationGrowth)


populationGrowth : Float -> Float -> Float -> Float -> Int
populationGrowth start percent augmentation expectation =
    let
        population : Float
        population =
            start * ( 1 + ( percent / 100 ) ) + augmentation

    in
        if population < expectation then
            1 + populationGrowth population percent augmentation expectation

        else
            1

Tests

module MainTest exposing (suite)

import Expect exposing ( Expectation )
import Test exposing ( Test )
import Main exposing ( populationGrowth )


suite : Test
suite =
    Test.describe "Population Growth"
        [ Test.test "It should return 3 with a starting population of 1000, a percentage of 2% and an increase of 50 to reach 1200 people" <| \_ ->
            Expect.equal 3 <| populationGrowth 1000 2 50 1200
        , Test.test "It should return 15 with a starting population of 1500, a percentage of 5% and an increase of 100 to reach 5000 people" <| \_ ->
            Expect.equal 15 <| populationGrowth 1500 5 100 5000
        , Test.test "It should return 10 with a starting population of 1500000, a percentage of 2.5% and an increase of 10000 to reach 2000000 people" <| \_ ->
            Expect.equal 10 <| populationGrowth 1500000 2.5 10000 2000000
        , Test.test "It should return 94 with a starting population of 1500000, a percentage of 0.25% and an increase of 1000 to reach 2000000 people" <| \_ ->
            Expect.equal 94 <| populationGrowth 1500000 0.25 1000 2000000
        ]
$ docker run --rm --interactive --tty --volume "$(pwd)":/home/elm/app aminnairi/elm-test tests/**/*.elm
TEST RUN PASSED

Duration: 278 ms
Passed:   4
Failed:   0
Collapse
 
lormayna profile image
lormayna

Python solution

n [16]: def nbYear(population, growing_rate, new, target):
    ...:     year = 0
    ...:     while population <= target:
    ...:         population = int(population + population*growing_rate + new)
    ...:         year +=1
    ...:         print("Year: {} - Population: {}".format(year, population))
    ...:     return year
    ...: 
    ...: 
    ...: 
    ...: 

In [17]: nbYear(1000, 0.02, 50, 1200)
Year: 1 - Population: 1070
Year: 2 - Population: 1141
Year: 3 - Population: 1213
Out[17]: 3

In [18]: nbYear(1500000, 2.5, 10000, 2000000)
Year: 1 - Population: 5260000
Out[18]: 1

In [19]: nbYear(1500000, 0.25, 1000, 2000000)
Year: 1 - Population: 1876000
Year: 2 - Population: 2346000
Out[19]: 2

In [20]: nbYear(1500, 5, 100, 5000)
Year: 1 - Population: 9100
Out[20]: 1
Collapse
 
aweleczka profile image
Alexander Weleczka

.NET anyone?

int newPopulation = currentPopulation;
newPopulation += (int) Math.Floor(currentPopulation * percentGrowth);
newPopulation += generalFluctuation;

return newPopulation;    

pretty straight forward calculation, checked in a while-loop with a counter.
Got the whole thing over here github.com/AWeleczka/dev-to_daily-... with application and tests. That's also where my solutions are, don't want to spoil this thread any more ;)

Collapse
 
centanomics profile image
Cent

Javascript


const nbYear = (p0, percent, aug, p) => {
  let year = 0;
  while(p0 < p) {
    p0 = p0 + (p0 * percent) + aug;
    year++;
  }
  return year
}

Codepen

Collapse
 
kerldev profile image
Kyle Jones • Edited

In Python:

import sys


def calculate_population_growth(
        starting_population,
        percentage_change,
        aug,
        population_to_surpass):
    '''
    Calculates the number of years required to surpass a given population size.
    '''
    number_of_years = 1
    percentage_change = percentage_change / 100

    while True:
        starting_population += int(starting_population * percentage_change)
        starting_population += int(aug)

        if starting_population >= population_to_surpass:
            return number_of_years
        number_of_years += 1


if len(sys.argv) > 4:
    print(
        str(calculate_population_growth(
            int(sys.argv[1]),
            float(sys.argv[2]),
            int(sys.argv[3]),
            int(sys.argv[4]))))
Collapse
 
maskedman99 profile image
Rohit Prasad • Edited

Python

p0 = int(input("Enter the starting population: "))
percent = float(input("Enter the percent: "))
aug = int(input("Enter the no.of inhabitants coming each year: "))
p = int(input("Enter the population to surpass: "))

percent = percent/100
years = 0

while p0 <= p:
        p0 = p0 + percent*p0 + aug
        years += 1

print(years)