42
\$\begingroup\$

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.

World and U.S. population formula, described below
(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

  1. Take the last two digits of the current year.
  2. 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.
  3. Add a decimal point between the two numbers (the same as dividing by 10).
  4. Add 6. This gives the result in billions of people.

U.S. population

  1. Take the last two digits of the current year.
  2. Subtract 10.
  3. Multiply by 3.
  4. Add 10.
  5. 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.
  6. 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
\$\endgroup\$
  • 1
    \$\begingroup\$ Do you have to round the numbers? \$\endgroup\$ – Blue Sep 12 '15 at 19:35
  • 5
    \$\begingroup\$ @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. \$\endgroup\$ – NinjaBearMonkey Sep 12 '15 at 19:38
  • 2
    \$\begingroup\$ I'm a little confused by the United States population one. If you're concatenating a 3, shouldn't 2040 give a population of 3100? 40 - 10 = 30, 30 * 3 = 90, 90 + 10 = 100, which would give "3" + "100" = 3100 \$\endgroup\$ – cole Sep 12 '15 at 20:01
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – NinjaBearMonkey Sep 12 '15 at 20:09
  • 8
    \$\begingroup\$ @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 not 3 . -20= "3-20") \$\endgroup\$ – PhiNotPi Sep 13 '15 at 5:15

16 Answers 16

22
\$\begingroup\$

Pyth, 21 20 bytes

-1 byte by Dennis

c/J-*3.d3C\ᙹ4T+33J

These have the same byte count but are ASCII-only:

c/J%*3.d3 523 4T+33J
c/-J%*3.d3*44T33 4TJ

I don't know Pyth, so still probably possibly golfable. Using the same algorithm:

TI-BASIC, 23 bytes

max(3getDate-5753
{.1int(Ans/4),Ans+33

getDate returns a list of three floats {YYYY,MM,DD} in some order depending on the date format setting (TI-84s don't have a true int datatype); the max( will be the year. Multiplying and subtracting inside the max( saves a close-paren.

\$\endgroup\$
16
\$\begingroup\$

Javascript (ES6), 55 54 48 bytes

-~((n=Date().substr(13,2)*3+280)/4-9.1)/10+' '+n

Works in Firefox 33; theoretically supports all years from 2000 to 2099. If programs that dump the result onto the console are not allowed, use this 51-byte function:

(n=Date().substr(13,2)*3+280)=>-~(n/4-9.1)/10+' '+n

Full program, 55 bytes:

n=Date().substr(13,2)*3+280,alert(-~(n/4-9.1)/10+' '+n)

Getting the year was quite expensive, but after using the deprecated getYear() instead of getFullYear(), all of the numbers in the equation became smaller, saving a lot of bytes. EDIT: Thanks to an eeevil trick, I skipped new and getYear() altogether. >:D

Suggestions welcome!

\$\endgroup\$
10
\$\begingroup\$

Pyth, 30 bytes

.R+*.075J%.d3C\d6.105 1+*3J280

My first Pyth program!

Thanks @Jakube for some hints (I would never have thought of those!)

\$\endgroup\$
  • 3
    \$\begingroup\$ Haven't read the question, but here are a few optimizations I immediately saw. Write everything on one line. Choose a different ordering of numbers and variables (+*3Z280 instead of +*Z3 280 for instance). C\d instead of 100 (saves spaces). Use J instead of Z (saves the =). Inline the assignment. Link \$\endgroup\$ – Jakube Sep 12 '15 at 20:27
10
\$\begingroup\$

Python 2, 80 bytes

from datetime import*
y=date.today().year%40
print int(61.55+.75*y)/10.,y*3+280

Now rounds!

\$\endgroup\$
  • \$\begingroup\$ You have to round, as OP clarified a minute ago. ;-) \$\endgroup\$ – mınxomaτ Sep 12 '15 at 19:52
  • 3
    \$\begingroup\$ year%100 is the same as year%40. \$\endgroup\$ – lirtosiast Sep 12 '15 at 22:31
6
\$\begingroup\$

CJam, 28 bytes

et0=100%__4/(-Ad/6+S@3*280+

Try it online

To try years other than the current one, replace the et0= at the start with the literal value of the year.

\$\endgroup\$
  • 2
    \$\begingroup\$ Since 2000 is divisible by 40, and you only need 2000-2039, you can use 40% to save one byte. \$\endgroup\$ – Andrea Biondo Sep 13 '15 at 8:34
5
\$\begingroup\$

Python 3, 134 bytes

Works well but seems a bit long

from datetime import*
y=str(date.today().year)
z=int(y[2:])
m=str(60+(z-int((int(y)-2005)/4)))
print("%s.%s"%(m[0],m[1]),310+(z-10)*3)
\$\endgroup\$
  • \$\begingroup\$ To shorten this, use from time import*, y=strftime('%Y'). Or copy the other python answer :P \$\endgroup\$ – FlipTack Nov 19 '16 at 14:31
5
\$\begingroup\$

AutoIt - 60 58 56 bytes

$_=@YEAR-2e3
ClipPut(($_-Int($_/4-1))/10+6&' 3'&3*$_-20)

The rounding eats bytes (not anymore). I have now tweaked both formulas. Some sample outputs:

7.3 325 // 2015
7.3 328
7.4 331
7.5 334 // 2018
8.4 370 // 2030

They all seem to be accurate.

A tip: Order of execution saves bytes on parentheses. For example: (a-4)/4 = a/4-1 :-).

\$\endgroup\$
4
\$\begingroup\$

PowerShell, 53 45 Bytes

$n=date -f yy;.1*[int](61.45+.75*$n);280+3*$n

Uses a similar rounding trick as muddyfish's Python 2 answer to simplify the world population calculation, since PowerShell implicitly Round()s when you cast from a [double] to an [int], rather than truncating.

For the output, we assume that "followed by some delimiter (e.g. space or comma)" can also mean "newline", so we execute one result and then the second. PowerShell implicitly writes out results, so we don't need to explicitly call any printing commands.

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 50 bytes

n=Today["YearShort"];{.1*(61+n-Floor[n/4]),280+3n}

Note that this is dependent on having a Wolfram Engine with Version number 10+ (released 2014) due to dependence on DateObjects.

R, 64 bytes

n=as.numeric(format(Sys.time(),"%y"))
c(.1*(61+n-n%/%4),280+3*n)

Direct port of Mathematica code, think I had a shorter solution but dependent on packages whereas this works with base R.

\$\endgroup\$
  • 1
    \$\begingroup\$ (1/10) -> .1? \$\endgroup\$ – lirtosiast Sep 13 '15 at 17:32
  • 1
    \$\begingroup\$ I think you also don't need parentheses around the .1. \$\endgroup\$ – lirtosiast Sep 13 '15 at 17:43
3
\$\begingroup\$

Java, 180 177 166 152 143 bytes

Thanks Thomas for helping a noob out :)

class D{public static void main(String[]a){int f=java.time.LocalDate.now().getYear();System.out.printf("%.1f %d\n",(3.*f-5755)/40,3*f-5720);}}

Ungolfed version:

class D {
  public static void main(String[]a) {
    int f = java.time.LocalDate.now().getYear();
    System.out.printf("%.1f %d\n",(3.*f-5755)/40,3*f-5720);
  }
}

Requires Java 8.

\$\endgroup\$
  • \$\begingroup\$ import java.time.*? 3.0->3.? You also don't need to print the year with the output. \$\endgroup\$ – lirtosiast Sep 20 '15 at 4:15
  • \$\begingroup\$ Oh, I didn't realize you didn't need the year printed out... :P \$\endgroup\$ – a spaghetto Sep 20 '15 at 14:35
3
\$\begingroup\$

JavaScript (ES6) 52

A function returning the output as an array.

(y=(new Date).getYear())=>[(y+~(y/4)-13)/10,y*3-20]

Just for testing purpose, the function accepts an input equals to current year - 1900 (e.g. 105 for 2015)

That's in the line of the ETHproductions' answer (the math is the math) but avoding the evil trick it is more portable in different locales. And as a function it's 1 byte shorter.

Test snippet:

f=(y=(new Date).getYear())=>[(y+~(y/4)-13)/10,y*3-20]

o=[];
for(a=2000;a<2050;a++)o.push(`<td>${a}</td><td>${f(a-1900)}</td>`);
document.write(`<table><tr>${o.join`</tr><tr>`}</tr></table>`)
td { text-align:right; font-family:monospace }

\$\endgroup\$
2
\$\begingroup\$

Ruby, 57 bytes

y=Time.now.year%40;puts "#{6+(y-(y-5)/4)*0.1} #{3*y+280}"

Unfortunately, Time.now.year really costs some characters.

\$\endgroup\$
2
\$\begingroup\$

Desmos, 140 Bytes

I'm counting a newline to be a signal for a new equation. Folders on the link are just for organization.

Golfed, 140 Bytes

Click add slider when prompted.

a=6+.1b-.1c
d=280+3b
c=\sum_{n=2005}^q\left\{0=\operatorname{mod}\left(n,4\right),0\right\}
b=100\left|\operatorname{floor}\left(.01q\right)-.01q\right|

Ungolfed, 261 Bytes

p_{world}=6+\frac{l_{astdig}-l_{eap}}{10}
p_{us}=310+3\left(l_{astdig}-10\right)
y_{ear}=2039
l_{eap}=\sum _{n=2005}^{y_{ear}}\left\{0=\operatorname{mod}\left(n,4\right),0\right\}
l_{astdig}=100\left|\operatorname{floor}\left(\frac{y_{ear}}{100}\right)-\frac{y_{ear}}{100}\right|
\$\endgroup\$
2
\$\begingroup\$

Glava, 77 bytes

i|f=java.time.LocalDate.now().getYear();F("%.1f %d\n",(3.*f-5755)/40,3*f-5720

A translation of my Java answer.

\$\endgroup\$
1
\$\begingroup\$

PHP, 45 bytes

The code:

echo(int)(($x=3*date(y)+247)/4)*.1," ",$x+33;

Because y (the argument of date()) is an undefined constant, PHP triggers a notice (that can be muted) and converts it to a string (as we need); this PHP courtesy allows saving 2 bytes.

In order to suppress the notice, the program needs to be run using the error_reporting=0 runtime directive, like this:

$ php -d error_reporting=0 -r 'echo(int)(($x=3*date(y)+247)/4)*.1," ",$x+33;'
7.3 325

For testing

By replacing the call to date(y) with $argv[1] (the first command line argument), the program length increases with 1 byte but it can get the year from the command line.

The expected argument is the year minus 2000; it also works for negative values (years before 2000) or values greater than 40 (after year 2040).

$ php -r 'echo(int)(($x=3*$argv[1]+247)/4)*.1," ",$x+33;' 00
6.1 280

$ php -r 'echo(int)(($x=3*$argv[1]+247)/4)*.1," ",$x+33;' 01
6.2 283

$ php -r 'echo(int)(($x=3*$argv[1]+247)/4)*.1," ",$x+33;' 02
6.3 286

$ php -r 'echo(int)(($x=3*$argv[1]+247)/4)*.1," ",$x+33;' 03
6.4 289

$ php -r 'echo(int)(($x=3*$argv[1]+247)/4)*.1," ",$x+33;' 04
6.4 292

$ php -r 'echo(int)(($x=3*$argv[1]+247)/4)*.1," ",$x+33;' 15
7.3 325

$ php -r 'echo(int)(($x=3*$argv[1]+247)/4)*.1," ",$x+33;' 39
9.1 397
\$\endgroup\$
1
\$\begingroup\$

APL, 25 23 29 bytes

{(10÷⍨⌊⍵÷4),⍵+33}⊃¯5753+3×⎕TS

TryAPL

Yes, it's 29 bytes.

\$\endgroup\$
  • \$\begingroup\$ Save two chars: (÷∘40,+∘33)¯5753+3×⊃⎕TS \$\endgroup\$ – Adám Sep 30 '15 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.