-3

I want to make a program that calculate the the populations after x years.

where the pop in 2002 is 6.2 billion people and increases 1.3 % each year.

The formula I will use is

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

How do I make 6.2B easier to work with?

delgeezee
  • 103
  • 1
  • 7
  • Why wont 6200000000 work? Or 6.2E9? – Praveen Gollakota Mar 02 '12 at 23:46
  • @Mario - I think perhaps you don't understand the idea of floating point arithmetic. –  Mar 02 '12 at 23:56
  • 1
    6.2e9 is good, but I don't like 6200000000 -- too many zeroes for me to count at a glance. – DSM Mar 02 '12 at 23:56
  • An explanation of the "difficulty" you encounter with 6.2e9 would be welcome, I fail to see what the problem is here. – Roadmaster Mar 03 '12 at 01:43
  • I was a afraid 6200000000 would produce an error, but it seemed to work fine. My difficulty is mainly grasping modular exponentiation. I have been staring at proofs all day and they make little sense to me. – delgeezee Mar 03 '12 at 08:48
  • 1
    Why do you want modular exponentiation for this at all? This is a purely exponential growth model. –  Mar 03 '12 at 13:58

1 Answers1

1

Here is your code. Read and learn well. This is probably a problem that you could have solved with 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
Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60