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

这就是他的算法

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
中国人口问题 2017年3月11日,国家卫计生委主任李斌、副主任王培安在十二届全国人大五次会议新闻中心举行的记者会上指出,中国的人口问题不缺数量,不光是现在不缺,未来几十年,未来一百年都不会缺人口数量。到2030年峰值时期,中国人口将有14.5亿左右,到2050年还有14亿左右的人口。全面放开二胎以后,国家卫计生委预测2017年全国人口出生数量预测最低值2023万,最高2300万。而国家统计局公布的2017年实际出生人口1723万人,比卫计生委预测最低值少300万人,其中二孩比例占51%【也就是说如果不实施二胎政策,全国只出生850万】 目前关于中国人口问题有乐观和悲观两种对立观点:一种认为我国人口基数大,今后应继续控制人口;另一种则认为,我国人口正在“坍塌”,危及经济发展和民族生存。 1. 请你(们)选择或提出若干人口关键指标,例如14岁以下人口占总人口比例,60岁以上人口占总人口比例,一对夫妇平均生育孩子数量,1980-2017全国小学生数量,全国人口平均年龄(核算每种指标社会正常运行的最低值、最高值及我国若干年后例如2030年,2050年,2100年的数值),建立数学模型,预测和分析我国人口发展态势,给出我国人口2030,2040,2050年的人口总数和结构(14岁以下和60岁以上人口占总人口的比例)。 2. 查阅相关数据,综合考虑目前90后生育观念(有的人认为90后多数一个孩子都不愿意要或不敢要,很多人不愿意结婚或结不起婚)、经济情况和生存压力、孩子就医和上学代价、人口结构(性别比)研究和预测2018-2025年我国每年人口出生情况。 3. 根据你们研究结果,向国家卫生健康委员会提交1份报告,提出你们的人口政策建议。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值