[py练习] 人口增长的问题

题目描述

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    

最佳实践1

def nb_year(population, percent, aug, target):
    year = 0
    while population < target:
        population += population * percent / 100. + aug
        year += 1
    return year

这个实现就一个字,稳

最佳实践2

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

最佳实践3

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))

这位哥们没有循环,直接用数学公式给计算出了年限,厉害了。回头我再看看是啥原理

总结

我的程序,其实可以简化的。还是练习的少。
和第一个最佳实践,比较,可以得到一点启示:

  • while的条件句的难点在于,如何让第一次loop和后面的loop都符合公式的写法,如果做到了,就可以简化很多代码,我的代码里,就是不知道怎么写一个都符合的公式。所以条件句放弃了书写,只写了个True.这样就还得写break句子。
  • 有些简单的功能,不需要都独立成函数。足够复杂,且能多次复用,再实现函数。

对于最佳实践3的分析

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项和公式,第二部分可以合并为如下

sumB=w(1zn)1z s u m B = w ( 1 − z n ) 1 − z

所以n年后的人口总和就是
pn=w(1zn)1z+p0zn p n = w ( 1 − z n ) 1 − z + p 0 ∗ z n

把pn换成目标A,再转换下
Aw1zw1zzn+p0zn A ≥ w 1 − z − w 1 − z ∗ z n + p 0 ∗ z n

继续
Aw1z(p0w1z)zn A − w 1 − z ≥ ( p 0 − w 1 − z ) ∗ z n

继续
znAw1zp0w1z z n ≤ A − w 1 − z p 0 − w 1 − z

以z为底数,两边取log,因为z大于1,取完对数后,比较符号不转向,再设置w/1-z 为r,则
nlogz(Arp0r) n ≤ log z ⁡ ( A − r p 0 − r )

这就是他的算法

你可能感兴趣的:(python刷题)