d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Any Help With Simple Python? > Taking Intro To Programming
Add Reply New Topic New Poll
Member
Posts: 30,905
Joined: Feb 18 2007
Gold: 0.00
Sep 3 2015 05:53pm
Quote
The U.S. Census provides information about the current U.S. population as
well as approximate rates of change. Three rates of change are provided:
a. There is a birth every 7 seconds
b. There is a death every 13 seconds
c. There is a new immigrant every 35 seconds
Using those three rates of change, and a current U.S. population of
307,357,870, write a program to calculate the U.S. population in exactly one
year (365 days). Your algorithm should output the result of your calculations.


I was wondering how to write something for adding 1 or subtracting 1 for when the time hits those amounts.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 3 2015 06:33pm
in pseudocode:

// rate of population change in seconds
BIRTH_RATE = 7;
DEATH_RATE = 13;
IMMIGRANT = 35;

basePopulation = 307357870;
duration = 365 days * 24 hours / 1 day * 60 minutes / 1 hour * 60 seconds / 1 minute; // time elapsed in seconds

changeInPopulation = duration / BIRTH_RATE + duration / IMMIGRANT - duration / DEATH_RATE;
totalPopulation = basePopulation + changeInPopulation;
Member
Posts: 30,905
Joined: Feb 18 2007
Gold: 0.00
Sep 3 2015 06:41pm
Quote (carteblanche @ Sep 3 2015 05:33pm)
in pseudocode:

// rate of population change in seconds
BIRTH_RATE = 7;
DEATH_RATE = 13;
IMMIGRANT = 35;

basePopulation = 307357870;
duration = 365 days * 24 hours / 1 day * 60 minutes / 1 hour * 60 seconds / 1 minute; // time elapsed in seconds

changeInPopulation = duration / BIRTH_RATE + duration / IMMIGRANT - duration / DEATH_RATE;
totalPopulation = basePopulation + changeInPopulation;


Awesome, that makes a lot of sense now. Thank you.
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Sep 4 2015 07:53am
Quote (carteblanche @ Sep 3 2015 06:33pm)
in pseudocode:

// rate of population change in seconds
BIRTH_RATE = 7;
DEATH_RATE = 13;
IMMIGRANT = 35;

basePopulation = 307357870;
duration = 365 days * 24 hours / 1 day * 60 minutes / 1 hour * 60 seconds / 1 minute; // time elapsed in seconds

changeInPopulation = duration / BIRTH_RATE + duration / IMMIGRANT - duration / DEATH_RATE;
totalPopulation = basePopulation + changeInPopulation;


To be hoenst, that is probably valid Python, even with the ;
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll