DEV Community

Discussion on: Daily Challenge #197 - Population Growth

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