mortality-natality_simulation.sf 675 B

1234567891011121314151617181920212223242526
  1. #!/usr/bin/ruby
  2. # Naive simulation of world population decrease when the mortality rate is greater than the natality rate.
  3. var population = 7.7e9 # world population
  4. var deaths = 61e6 # deaths per year
  5. var births = deaths/5 # births per year
  6. var target = 500_000_000 # target population
  7. var death_rate = deaths/population.float
  8. var birth_rate = births/population.float
  9. var years = 0
  10. while (population > target) {
  11. population -= population*death_rate
  12. population += population*birth_rate
  13. ++years
  14. }
  15. say "It took #{years} years to reach a population of #{target.commify}."
  16. __END__
  17. It took 430 years to reach a population of 500,000,000.