42
5
In xkcd 1047, Randall Munroe lists "slightly wrong" approximations of assorted quantities and numbers with varying precision and complexity, such as that the number of liters in a gallon is very close to 3 + π⁄4. In the middle of the comic, he gives an intermission: a way to estimate the world (and United States) population based for a given year.
(Cropped from xkcd: Approximations by Randall Munroe)
Your task is to write a program that implements these formulas to approximate the current world and U.S. populations, replicated as follows.
World population
- Take the last two digits of the current year.
- Subtract the number of leap years (including the current year) since Hurricane Katrina (2005). For these purposes, any year divisible by 4 is considered a leap year.
- Add a decimal point between the two numbers (the same as dividing by 10).
- Add 6. This gives the result in billions of people.
U.S. population
- Take the last two digits of the current year.
- Subtract 10.
- Multiply by 3.
- Add 10.
- Add 3 to the beginning (for this challenge, some numbers will be negative, so add 300 instead). Somehow I didn't notice that just concatenating wouldn't work because the program I used to generate the results just added 300.
- This gives the result in millions of people.
Details
This formula "should stay current for a decade or two," but you must be able to theoretically handle any year 2000–2039 inclusive. For some cases, the leap years since Katrina will have a negative or zero value.
You are free to simplify the formula in any way, as long as all outputs match the ones below.
For the year, use the year according to the computer's clock. It must work next year and any other year this century, so you cannot simply hardcode 2015. For convenience, you might want to include a way to specify the year as a variable or input to test other years.
The output should be the approximated world population (in billions of people), followed by some delimiter (e.g. space or comma), followed by the U.S. population (in millions of people). You may also write a function that returns or prints a string or an array of numbers or strings.
This is code golf, so shortest code in bytes wins. Tiebreaker is earliest post.
Test cases
This is a list of all possible years, followed by the two outputs.
Year World U.S.
2000 6.1 280
2001 6.2 283
2002 6.3 286
2003 6.4 289
2004 6.4 292
2005 6.5 295
2006 6.6 298
2007 6.7 301
2008 6.7 304
2009 6.8 307
2010 6.9 310
2011 7 313
2012 7 316
2013 7.1 319
2014 7.2 322
2015 7.3 325
2016 7.3 328
2017 7.4 331
2018 7.5 334
2019 7.6 337
2020 7.6 340
2021 7.7 343
2022 7.8 346
2023 7.9 349
2024 7.9 352
2025 8 355
2026 8.1 358
2027 8.2 361
2028 8.2 364
2029 8.3 367
2030 8.4 370
2031 8.5 373
2032 8.5 376
2033 8.6 379
2034 8.7 382
2035 8.8 385
2036 8.8 388
2037 8.9 391
2038 9 394
2039 9.1 397
1Do you have to round the numbers? – Blue – 2015-09-12T19:35:05.847
5@muddyfish I'm not sure I understand. If you follow the instructions in the comic exactly, there's technically no division going on, but the world population should be rounded to the nearest tenth. – NinjaBearMonkey – 2015-09-12T19:38:46.187
2I'm a little confused by the United States population one. If you're concatenating a
3
, shouldn't2040
give a population of3100
?40 - 10 = 30
,30 * 3 = 90
,90 + 10 = 100
, which would give"3" + "100" = 3100
– cole – 2015-09-12T20:01:26.700Also, if the language we use cannot obtain the current year, should we hardcode it in, provided that the code without the hardcoded year will work for the range of inputs? – cole – 2015-09-12T20:06:07.373
2@Cole Good point, I'll make it so you only have to support years through 2039. About hardcoding the year, I don't want to allow hardcoding because that will almost always be shorter even languages that do support dates. – NinjaBearMonkey – 2015-09-12T20:09:35.453
@NinjaBearMonkey That's fair, I jumped the gun a bit and made a submission so I'll delete it. My apologies for doing so. – cole – 2015-09-12T20:16:15.063
8@NinjaBearMonkey I suggest that you change the description of "adding 3, thinking concatenation" to a literal "add 300" to cover all of the edge cases that occur when the previous result isn't a nice 2-digit positive number. (For example, year 2000 gives
280
as a result of-20+300=280
and not3 . -20= "3-20"
) – PhiNotPi – 2015-09-13T05:15:00.197Now we can make sure nobody hardcoded the year! – wizzwizz4 – 2016-01-09T10:25:44.497
If anyone's interested, I made an obfuscated solution for this in a cops/robbers challenge ;)
– FlipTack – 2016-11-18T22:48:12.183