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
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Python solution