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
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.
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
Output :