Skip to content

Instantly share code, notes, and snippets.

@mmoskal
Created April 8, 2023 17:16
Show Gist options
  • Save mmoskal/b6d8d2c73ec4fe56df9714d8435a234e to your computer and use it in GitHub Desktop.
Save mmoskal/b6d8d2c73ec4fe56df9714d8435a234e to your computer and use it in GitHub Desktop.
compute time ever lived in human history
import { writeFileSync } from "node:fs"
// data from https://en.wikipedia.org/wiki/Estimates_of_historical_world_population PRB column
// and https://www.prb.org/articles/how-many-people-have-ever-lived-on-earth
const data = `
-190000 0
-50000 2M
-8000 5M
0 300M
1200 450M
1650 500M
1750 795M
1850 1265M
1900 1656M
1950 2499M
2000 6149M
2010 6986M
2022 7963M
-7000 7M
-6000 14M
-5000 27M
-4000 50M
-3000 100M
-1000 100M
-500 150M
-200 227M
500 280M
1000 400M
1100 450M
1200 500M
1300 500M
1400 500M
1500 600M
1600 600M
1700 660M
1800 1000M
1850 1265M
1900 1656M
1925 2000M
`
const rows = []
data.split(/\n/).forEach(l => {
let [yr, pop] = l.split(/\s+/)
yr = parseInt(yr)
pop = parseInt(pop)
if (isNaN(yr)) return
rows.push([yr, pop])
})
rows.sort((a, b) => a[0] - b[0])
let total = 0
for (let i = 1; i < rows.length; ++i) {
const [y0, p0] = rows[i - 1]
const [yr, pop] = rows[i]
const len = yr - y0
const popavg = (pop + p0) / 2
const yrslived = Math.round((len * popavg) / 1000)
total += yrslived
rows[i].push(len, popavg, yrslived, total)
}
for (let i = 1; i < rows.length; ++i) {
rows[i].push(
Math.round((10000 * rows[i][rows[i].length - 1]) / total) / 100
)
}
rows.unshift([
"year",
"population (M)",
"time",
"pop.avg (M)",
"years lived (B)",
"total years lived (B)",
"% years lived",
])
writeFileSync("pop.csv", rows.map(r => r.join(",")).join("\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment