如何使用模块化表达式/使用大整数

时间:2012-03-02 23:43:27

标签: python math int modular exponentiation

我想制作一个计算x年后人口的计划。

2002年流行音乐人数为62亿人,每年增长1.3%。

我将使用的公式是

population = ((1.013)**x) * 6.2B

如何让6.2B更易于使用?

1 个答案:

答案 0 :(得分:1)

这是你的代码。阅读和学习。这可能是您可以通过Google解决的问题。

import math

def calculate_population(years_since_2002): #the original calculation
    population_2002 = 6.2*10**9
    final_population = int(((1.013)**years_since_2002)*population_2002)
    return final_population

def pretty_print(num,trunc=0):
    multiplier = int(math.log10(num)) #finds the power of 10
    remainder = float(num)/(10**multiplier) #finds the float after
    str_remainder = str(remainder)
    if trunc != 0:
        str_remainder = remainder[:trunc+1] #truncates to trunc digits total
    return str_remainder+'e'+str(multiplier) #can also be print