In 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 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.
More generally given parameters:
p0, percent, aug (inhabitants coming or leaving each year), p (population to surpass)
the function nb_year should return n number of entire years needed to get a population greater or equal to p.
aug is an integer, percent a positive or null number, p0 and p are positive integers (> 0)
Examples:
nb_year(1500, 5, 100, 5000) -> 15
nb_year(1500000, 2.5, 10000, 2000000) -> 10
def nb_year(p0, percent, aug,p):
def nextyearp(now,percent,aug):
next = now*(1+(percent/100))+aug
return next
i = 0
while True:
if i == 0:
now = p0
now = nextyearp(now,percent,aug)
i += 1
if now >= p:
break
else:
continue
return i
def nb_year(population, percent, aug, target):
year = 0
while population < target:
population += population * percent / 100. + aug
year += 1
return year
这个实现就一个字,稳
def nb_year(p0, percent, aug, p, years = 0):
if p0 < p:
return nb_year(p0 + int(p0 * percent / 100) + aug, percent, aug, p, years + 1)
return years
这个实现真是太NB了,递归函数真是博大精深啊。
但是似乎看着好看,但是容易出现溢出
参考
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00137473836826348026db722d9435483fa38c137b7e685000
from math import ceil, log
def nb_year(p0, percent, aug, p):
if not percent: return ceil(1.*(p - p0) / aug)
percent = 1 + percent / 100.
r = aug / (1 - percent)
return ceil(log((p - r) / (p0 - r), percent))
这位哥们没有循环,直接用数学公式给计算出了年限,厉害了。回头我再看看是啥原理
我的程序,其实可以简化的。还是练习的少。
和第一个最佳实践,比较,可以得到一点启示:
1 年后, 总人口 = p0*z + w
2 年后, 总人口 = [p0*z + w]z + w
= p0*z^2 + wz + w
3 年后,总人口 = p0*z^3 + wz^2 + wz + w
n 后年, 总人口 = ( p0*z^n ) + ( wz^(n-1) + ... + wz + w )
可以看到,n年后的总人口,计算公式由两部分组成,
第一部分A是p0*z^n
第二部分B是一个等比数列,公比是z,第一项是w.
利用高中数学里学的,等比数列的前N项和公式,第二部分可以合并为如下
这就是他的算法