List of possible birth years of living humans

40

2

The Challenge

Output a list of years that starts with the current year and ends 120 years ago. The birth year of every living human would be included in this list.

Details

The list should be in descending order.

Every built-in function to manipulate arrays and/or lists is allowed.

Shortest code in bytes wins.

When run this year the output would be

2016, 2015, ..., 1897, 1896

When run next year the output would be

2017, 2016, ..., 1898, 1897

Etc.

Update

  • Some have asked about the format of the list. As most have guessed, it doesn't matter. Insert any separator between the numbers. Intuitively most inserted a comma or space or both, newline or output an array.
  • Quasi superhumans like Jeanne Calment are an exception to the rule stated in my question.

Christiaan Westerbeek

Posted 2016-08-11T11:25:31.933

Reputation: 863

2Please help me understand how to improve my question. It's my first code golf question. – Christiaan Westerbeek – 2016-08-11T11:30:13.477

7

I recommend the sandbox for getting feedback before posting a challenge (not just for your first challenge - most of us use it for every challenge).

– trichoplax – 2016-08-11T11:31:36.660

Thanks. I didn't know the sandbox existed. Not sure though if I know now should delete my question first. Seems a legit code golf question, but I'll go to the sandbox first. – Christiaan Westerbeek – 2016-08-11T11:33:36.650

3I don't see anything wrong with this challenge. One thing that might be worth specifying is whether the output should always start with 2016, or with the year in which it is run (will it start with 2017 if run next year?). This will affect whether it is a fixed output challenge, or needs to access the current date. – trichoplax – 2016-08-11T11:33:45.810

If you get lots of downvotes, it probably means something is wrong. If you just get one downvote, it doesn't necessarily mean anything. It's worth asking just in case, but sometimes a challenge gets a downvote just at random. – trichoplax – 2016-08-11T11:36:24.573

3I think you should probably either specify a separator to use between years, or explicitly say we can use any separator. – Business Cat – 2016-08-11T14:07:06.710

19Jeanne Louise Calment lived 122 years. – Zenadix – 2016-08-11T15:32:52.077

10Lad, that was way too early of an accept. – Addison Crump – 2016-08-11T17:30:12.610

Does the output format matter? – Tom Carpenter – 2016-08-11T19:10:51.790

And you have an off-by-one besides. You can have a 119 year old born in 1895. – Loren Pechtel – 2016-08-11T23:30:38.393

6Is the challenge: "Print all the numbers from y - 120 to y" or "print all the birth years of living people"? Because if someone born in 1896 is alive today, that doesn't mean that there are also still people from 1898 around. – CompuChip – 2016-08-14T09:58:09.687

1Also, if Jeanne Louise Calment was the oldest recorded person ever, I think it's safe to say that 123 years is the upper limit. Why not go back 123 years? – Mr Lister – 2016-08-14T18:32:31.510

Answers

19

Pyke, 6 bytes

wC7m-

Try it here!

w     - 121
   m- - for i in range(^):
 C7   -  current_year-i

(After w, the codepoint for 153 is present but it isn't printable)

Blue

Posted 2016-08-11T11:25:31.933

Reputation: 26 661

1Wow, that was a quick checkmark. Maybe wait for a Jelly answer? – Adám – 2016-08-11T15:10:08.797

Whilst I agree it was a quick checkmark, I doubt even Jelly can do it in 5 – Blue – 2016-08-11T15:28:31.687

Why do you have a variable for 121? Just because it is a square number? But well done! – Denker – 2016-08-11T16:00:12.170

3The w actually has an unprintable byte afterwards that makes it 121 – Blue – 2016-08-11T16:02:10.327

What encoding does Pyke use? – Leaky Nun – 2016-08-11T16:05:12.153

1@LeakyNun it's configurable, whatever the default for the server it's running on. On pyke.catbus, I set it to Windows-1252 – Blue – 2016-08-11T16:10:24.903

11

Pyth, 11 9 8 bytes

-L.d3C\y

Works by mapping over the range [0...120] and subtracting every number from the current year. The range is built implicitly by using 121 as the map argument. To avoid a separating whitespace between .d3 and 121 we get this number by converting y to it's codepoint.
Thanks to @FryAmTheEggman for coming up with this approach!

Try it here!

Old 9-byte solution:

_>121S.d3

Try it here!

Builds the range [1...<current year>] and only takes the last 121 elements of it.

Denker

Posted 2016-08-11T11:25:31.933

Reputation: 6 639

1You should reverse the list so that it starts with the current year – Christiaan Westerbeek – 2016-08-11T12:27:47.067

1-L... saves a few bytes. – Jakube – 2016-08-11T12:28:47.303

1@ChristiaanWesterbeek Missed that part, fixed. Thanks for spotting! – Denker – 2016-08-11T12:33:12.647

@Jakube Used a different approach now, but thanks for the reminder that this exists, didn't use Pyth a lot in the last time! – Denker – 2016-08-11T12:34:50.180

1-L.d3C\y should work for 8. – FryAmTheEggman – 2016-08-11T12:38:04.330

@FryAmTheEggman Very clever, thanks! :) Gotta do more Pyth, needed way too long to understand how that works... – Denker – 2016-08-11T12:48:57.150

9

R, 34 bytes

(format(Sys.Date(),"%Y"):0)[1:121]

See here on an online interpreter.

Edit Could be reduced to 33 bytes by using substr.

(substr(Sys.Date(),1,4):0)[1:121]

but technically this solution will only work until the 9999-12-31.

plannapus

Posted 2016-08-11T11:25:31.933

Reputation: 8 610

3This is awesome. I did not know it could typecast a string into a number when using the : operator... – Andreï Kostyrka – 2016-08-11T22:27:01.733

8

BASH + coreutils, 35 33 bytes

x=`date +%Y`;seq $x -1 $((x-120))

Joe

Posted 2016-08-11T11:25:31.933

Reputation: 281

2in your case, quotes are unneeded: change : date +"%Y" into date +%Y – Olivier Dulac – 2016-08-11T16:26:25.617

1x=`date +%Y`;seq $x -1 $[x-120] or seq $[x=`date +%Y`] -1 $[x-120] works as well. – Dennis – 2016-08-11T17:48:27.060

10 bytes shorter if you have OSX or are willing to install jot on Linux – Digital Trauma – 2016-08-12T00:11:44.083

8

AngularJS + Lodash, 274 bytes

angular.module('x',[]).controller('x',['$scope',function(x){x.x=_.range(9,new Date().getFullYear()+1).slice(-121).reverse()}])
<script src=//goo.gl/M5LvGe></script><script src=//goo.gl/opljJl></script><select ng-app=x ng-controller=x multiple><option ng-repeat="x in x">{{x}}

Output

enter image description here

YOU

Posted 2016-08-11T11:25:31.933

Reputation: 4 321

7

CJam, 14 12 11 bytes

Saved 2 bytes thanks to Sp3000 and 1 byte thanks to Martin Ender

et0=121,f-p

Try it online!

Explanation

et0=         e# Push the current year
    121,     e# Push the range 0, 1, ..., 120
        f-   e# For each item in the range, subtract it from current year
          p  e# Print the array

Business Cat

Posted 2016-08-11T11:25:31.933

Reputation: 8 927

7

HP50g RPL, 97 bytes

120 'S' STO DATE ->STR 6 9 SUB OBJ-> 'Y' STO {} S WHILE 0 >= REPEAT Y S - + 'S' DECR END REVLIST

Ungolfed:

120
'span' STO     @ Store the span of years to cover.
DATE           @ Get the date as number 11.082016. 
→STR           @ Convert to string "11.082016".
               @ (Number format must allow all these decimal places.)
6 9 SUB        @ "11.082016" Substring for the year.
               @       ^  ^
               @  123456789
OBJ→           @ Convert string to number.
'year' STO     @ Store as the year to start at.
{} span        @ Start empty list to collect the years.
               @ Leave loop counter on the stack.
WHILE 0 ≥      @ Loop until the counter goes negative.
REPEAT
  year span -  @ E.g. on first iteration: 2016 - 120 = 1896.
  +            @ Append the year to the list on the stack.
  'span' DECR  @ Decrement loop counter and leave on stack.
END
REVLIST        @ Put list in reverse chronological order.

Showing a list of 2016 down to 1896:

2016 to 1896

Caleb Paul

Posted 2016-08-11T11:25:31.933

Reputation: 171

Edited answer to get the current year from the calculator. – Caleb Paul – 2016-08-11T19:41:34.067

6

Dyalog APL, 11 10 bytes

120↑⌽⍳⊃⎕ts

120↑ take 120 elements

of the reversed

indices until

the first element of

⎕TS TimeStamp in the format [YYYY, M, D, h, m, s, t]

TryAPL online!


Old version:

(⊃⎕TS)-⍳120

⊃⎕TS first element of [YYYY, M, D, h, m, s, t]

- minus

⍳120 [0, 1, 2, ..., 118, 119]

Requires ⎕IO←0, which is default on many systems.

TryAPL online!

Adám

Posted 2016-08-11T11:25:31.933

Reputation: 37 779

6

05AB1E, 8 7 bytes

Code:

žg120Ý-

Explanation:

žg         # Get the current year.
  120Ý     # Create the list [0, 1, ..., 119, 120].
      -    # Substract, which leaves [year - 0, year - 1, ..., year - 120].

Uses the CP-1252 encoding. Try it online!.

Adnan

Posted 2016-08-11T11:25:31.933

Reputation: 41 965

6

PowerShell, 26 24 bytes

@TimmyD improved version:

0..120|%{(date).Year-$_}

Was:

($d=(date).year)..($d-120)

where date runs Get-Date

TessellatingHeckler

Posted 2016-08-11T11:25:31.933

Reputation: 2 412

1Blargh, should have answered earlier. That's exactly the straightforward version I had ready. Then work interfered :D A fun version would also be: ((date).year..0)[0..120]; alas it's not shorter. – Joey – 2016-08-11T14:35:29.057

1I find it interesting that date is equivalent to Get-Date when date is actually not an alias to Get-Date. I've seen this called the "implied verb" rule and it is rarely ever documented in PowerShell literature. – Bevo – 2016-08-13T01:04:40.177

@Bevo It's useful for CodeGolf, but a bit risky for any other use. See: http://codegolf.stackexchange.com/a/778/571 and comment chain; it's both prone to name clashes and very slow.

– TessellatingHeckler – 2016-08-13T01:11:13.360

6

Vitsy + *sh + JavaScript, 33 26 21 18 bytes

Vitsy doesn't have native time/date retrieval, so I had to use shell and eval for this one.

Thanks to @Caleb for helping me shear off another 3 bytes!

'Y%+ etad',Dca*-HZ

'Y%+ etad'               Push the string 'date +"%Y"' to the stack.
          ,              Execute through shell. 
           Dca*-         Dupe n, push n - 120.
                H        Pop x, y, push range(x, y) to the stack.
                 Z       Output all stack as characters.

You can't try this one online, because it uses both shell AND eval.

Output is as character codes.

Addison Crump

Posted 2016-08-11T11:25:31.933

Reputation: 10 763

You should be able to trim a couple bytes off of this because you don't need the double quotes around the date format string (since there are no spaces in it); date +%Y works just fine. – Caleb – 2016-08-11T16:13:58.477

@Caleb Perfect, thanks! Made the need for a JS eval non-existent as well. – Addison Crump – 2016-08-11T17:28:12.493

5

05AB1E, 9 bytes

121FžgN-=

Try online

P. Knops

Posted 2016-08-11T11:25:31.933

Reputation: 301

2Welcome to the site! :) – James – 2017-04-03T18:57:24.217

5

R, 47 39 bytes

as.double(substr(Sys.Date(),1,4))-0:120

If only someone invented a UTF-8 implementation of R with conveniently abbreviated frequently used system functions...

UPD: shaved off 7 (!) bytes owing to plannapus, who suggested subrtacting a 0:120 vector rather than counting from a to (a-120) and 1 byte grâce à user5957401, who noticed that double would work as well as numeric. Old version:

a=as.numeric(format(Sys.Date(),"%Y"));a:(a-120)

Andreï Kostyrka

Posted 2016-08-11T11:25:31.933

Reputation: 1 389

Fiddling around with and seq(a<-...,a-120) can only worsen the situation. – Andreï Kostyrka – 2016-08-11T12:48:59.960

using the type forcing command as.double instead of as.numeric will save you a byte. Similarly, if instead of formatting a date, you treat the date response as a string to pull from and use substr(Sys.Date(),1,4) you save another byte – user5957401 – 2016-08-11T14:34:20.567

5

MATL, 10 bytes

1&Z'0:120-

Try it online!

1&Z'      % Push first component of "clock" vector, which is year
0:120     % Literal vector [0, 1, 2, ..., 120]
-         % Subtract element-wise. Implicitly display

Luis Mendo

Posted 2016-08-11T11:25:31.933

Reputation: 87 464

5

Mathematica/Wolfram Language, 28 bytes

Date[][[1]]-#&/@Range[0,120]

user6014

Posted 2016-08-11T11:25:31.933

Reputation: 288

Welcome to PPCG! – Addison Crump – 2016-08-11T13:13:44.103

4Welcome to PPCG! You don't need to use a map here since subtraction is threaded over lists automatically. And -Range@121+1 saves a byte over the zero-based list. And #&@@ a byte over [[1]]. That said, unless stated otherwise in the challenge, all submissions have to be functions or full programs, and this code is merely a snippet/expression. The shortest fix would be making it an unnamed function by appending &. – Martin Ender – 2016-08-11T13:38:28.150

@MartinEnder Thanks for the feedback! I should have caught Minus being list able, minor mental lapse, but the other tips are much appreciated! – user6014 – 2016-08-12T18:13:50.433

#&@@Date[]-#&/@0~Range~120& will save you two bytes, but then making it a function requires adding the & on the end (which I think yours needs anyway), so -1 byte in total. – numbermaniac – 2017-07-12T09:05:53.050

4

Python 2, 64 62 54 bytes

import time
n=time.gmtime()[0]
exec'print n;n-=1;'*121

@KarlKastor thanks for 8 bytes!

atlasologist

Posted 2016-08-11T11:25:31.933

Reputation: 2 945

time.gmtime().tm_year is 3 bytes shorter – KarlKastor – 2016-08-11T17:21:09.033

time.gmtime()[0] even shorter – KarlKastor – 2016-08-11T17:27:58.897

@KarlKastor nice! Playing with the time module is like walking into a maze every time for me. So many ways to get lost – atlasologist – 2016-08-11T17:44:26.560

4

php, 73 66 58 42 bytes

<?=implode(', ',range($j=date(Y),$j-120));

Output:

2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1966, 1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956, 1955, 1954, 1953, 1952, 1951, 1950, 1949, 1948, 1947, 1946, 1945, 1944, 1943, 1942, 1941, 1940, 1939, 1938, 1937, 1936, 1935, 1934, 1933, 1932, 1931, 1930, 1929, 1928, 1927, 1926, 1925, 1924, 1923, 1922, 1921, 1920, 1919, 1918, 1917, 1916, 1915, 1914, 1913, 1912, 1911, 1910, 1909, 1908, 1907, 1906, 1905, 1904, 1903, 1902, 1901, 1900, 1899, 1898, 1897, 1896

If we don't need to separate with ,, then 58 57 41 bytes:

<?=implode(' ',range($j=date(Y),$j-120));

Thanks to insertusernamehere for saving 16 bytes

gabe3886

Posted 2016-08-11T11:25:31.933

Reputation: 221

You don't really need to separate with ,, look at many of the other answers. – Addison Crump – 2016-08-11T13:25:08.557

It looks that way, but I'll put in two options so I can do what most are, and also what was requested – gabe3886 – 2016-08-11T13:27:31.080

I've managed to get the comma answer down to my original non-comma answer size. That's as much as I think I can golf it – gabe3886 – 2016-08-11T13:29:45.610

You can remove the <?php tag since we allow running with -r for free

– Business Cat – 2016-08-11T13:38:44.037

Would I then count the quotes needed round it, or just the actual execution code? – gabe3886 – 2016-08-11T14:02:14.657

2Golf off 16 bytes: <?=implode(', ',range($j=date(Y),$j-120)); – insertusernamehere – 2016-08-11T15:14:02.610

I think you can count just the execution code, not anything needed to invoke it (such as the surrounding quotes or shell escapes to get the right string passed to the parser. But I think you do need a print, which is a step backwards from <?=. You might look around at other PHP answers on popular questions and see about the print thing. – Caleb – 2016-08-11T16:22:26.580

37 bytes (using win-1252 encoding) <?=join(~ß,range($j=date(Y),$j-120)); – aross – 2016-08-15T10:32:45.600

4

PostgreSQL, 57 bytes

Who needs a FROM :-). Probably shouldn't work, but it does, try it at sqlfiddle

select date_part('y',current_date)-generate_series(0,120)

MickyT

Posted 2016-08-11T11:25:31.933

Reputation: 11 735

4

Bash on OS X, 23

jot 121 `date +%Y` - -1

This will also work on Linux if you have BSD jot installed (e.g. sudo apt-get install athena-jot.

Digital Trauma

Posted 2016-08-11T11:25:31.933

Reputation: 64 644

3

Python 2, 62 bytes

import time
for n in range(121):print int(time.ctime()[-4:])-n

And at 64 bytes:

for n in range(121):print int(__import__("time").ctime()[-4:])-n

Daniel

Posted 2016-08-11T11:25:31.933

Reputation: 6 425

3

JavaScript, 55 52 50 49 bytes

for(a=d=s=Date().substr(11,4);d-->s-120;)a+=" "+d

+9 bytes to alert the data (not necessary if you run this in a console).

for(a=d=s=Date().substr(11,4);d-->s-120;)a+=" "+d;alert(a)

-4 bytes if the delimiter isn't necessary.

for(a=d=s=Date().substr(11,4);d-->s-120;)a+=d

for (a = d = s = Date().substr(11, 4); d --> s - 120; )
    a += " " + d;
console.log(a);

Yay295

Posted 2016-08-11T11:25:31.933

Reputation: 650

1The []s appear to be unnecessary. – Neil – 2016-08-11T22:48:25.373

@Neil: Thanks. My original solution used an array, but I hadn't thought to remove it. – Yay295 – 2016-08-12T00:26:58.813

1You can save one byte using a for instead of while on this: for(a=d=s=Date().substr(11,4);d-->s-120;)a+=" "+d! – Dom Hastings – 2016-08-12T07:58:16.683

3

Oracle SQL 11.2, 111 bytes

SELECT LISTAGG(TO_CHAR(SYSDATE,'YYYY')-LEVEL+1,', ')WITHIN GROUP(ORDER BY LEVEL)FROM DUAL CONNECT BY LEVEL<122;

Jeto

Posted 2016-08-11T11:25:31.933

Reputation: 1 601

1I would be tempted to say the the listagg isn't required and just the set of ordered rows – MickyT – 2016-08-11T20:20:28.750

3

C, 87 85 76 74 65 bytes

main(int i,char**v){for(i=0;i<121;printf("%d,",atoi(v[1])-i++));}

Ungolfed:

main(int i,char**v){
    for(i = 0; i < 121; printf("%d,", atoi(v[1])-i++));
}

My first code golf - Any pointers would be well received. Would be nice if I could cut out the argc/argv junk, but I'm not that skilled a C programmer. Improvements welcome.

EDIT: The current year is obtained from the commandline - specifically by a group of automated, well trained drinking birds.

EDIT 2: 85 bytes Thanks to Easterly Irk (removed spaces around arg function parameters)

EDIT 3: 76 bytes Thanks to anatolyg for pointing out the obvious (removed verbose argc/argv param names)

EDIT 4: 74 bytes Thanks to Yay295 (char**v, changed year delimiter)

EDIT 5: 65 bytes thanks to Yay295 and matt (re-used the variable i, removed variable x, changed while to for loop, updated printf to include atoi() read and i++)

schil227

Posted 2016-08-11T11:25:31.933

Reputation: 131

1Can't you a space? int argc, char -> int argc,char? – Rɪᴋᴇʀ – 2016-08-11T16:01:20.930

2Replace argc by c (or better replace int argc by just int) and argv by v. Also replace while by for. The latter change requires some experience in C (but may be fun regardless). – anatolyg – 2016-08-11T17:11:08.440

@anatolyg oh, duh (for the argc/argv stuff). Wouldn't the 'for' loop be the same length as the while loop? for(;i>x;) vs while(i>x)? – schil227 – 2016-08-11T17:29:11.030

1char*v[] -> char**v saves 1 byte. You can save another by delimiting your years with a space or a comma instead of a newline. – Yay295 – 2016-08-11T17:57:39.450

1You can make it shorter. 71 bytes.

main(int c,char**v){int i=0;while(i<120)printf("%d,",atoi(v[1])-i++);} – Matt – 2016-08-11T20:57:51.310

1@Matt: Nice, but we can go one further. main(int i,char**v){i=0;while(i<121)printf("%d,",atoi(v[1])-i++);}. Also, we've been off by 1. – Yay295 – 2016-08-12T01:09:07.667

@Yay295 - sweet! good reuse of i. – Matt – 2016-08-12T01:18:09.673

2Even shorter. 65 bytes main(int i,char**v){for(i=0;i<121;printf("%d,",atoi(v[1])-i++));} – Matt – 2016-08-12T02:07:31.730

63 bytes: main(i,v)char**v;{for(i=0;i<121;printf("%d,",atoi(v[1])-i++));} – Johan du Toit – 2017-04-03T14:51:51.287

3

Processing, 51 42 41 bytes

for(int i=0;i<121;)print(year()-i+++" ");

Ungolfed

for(int i = 0; i < 121;)
    print(year() - i++ + " ");

Processing is just a wrapper for Java if you didn't know, and takes most of the boilerplate away, so the obvious solution is also super short compared to a Java version. Also opens a window for drawing graphics, but having that doesn't appear to disqualify me :)

Cody

Posted 2016-08-11T11:25:31.933

Reputation: 447

1for(int i=0;i<121;)print(year()-i+++" "); – Leaky Nun – 2016-08-11T16:27:28.157

I love the "+++", it looks so suspicious. Thanks for the suggestion! – Cody – 2016-08-11T16:38:28.953

3

MATLAB, 18 bytes

Assuming the output format doesn't matter (aside form descending order), the following program prints the last 121 years starting with the current one.

year(date)-(0:120)

It requires MATLAB version r2014a or higher. Earlier versions didn't include the year function.

Tom Carpenter

Posted 2016-08-11T11:25:31.933

Reputation: 3 990

3

JavaScript, 60 53 48 Bytes

f=x=>x>Date().split` `[3]-121&&(alert(x),f(x-1))

I used a recursive solution.

Huntro

Posted 2016-08-11T11:25:31.933

Reputation: 459

1You can replace console.log with alert, and remove the last ;. – NoOneIsHere – 2016-08-12T22:09:18.503

1Save 5 bytes with f=x=>x>Date().split" "[3]-121&&(alert(x),f(x-1)), replacing the quotation marks with backticks. – Shaggy – 2017-04-20T13:24:53.527

2

Japt, 12 6 bytes

Saved 6 bytes thanks to @ETHproductions

#yonKi

Try it online!

Explanation:

#yonKi
#y          // # gets the char-code of y, which is 121
  o         // Create a range from [0...121]
   nKi      // At each item, perform .n(K.i()), which subtracts each item from Ki (Current year)

Oliver

Posted 2016-08-11T11:25:31.933

Reputation: 7 160

2

Perl, 33 bytes

say+(gmtime)[5]-$_+1900for 0..120

Run with -M5.010 or -E :

perl -E 'say+(gmtime)[5]-$_+1900for 0..120'

Dada

Posted 2016-08-11T11:25:31.933

Reputation: 8 279

2

k, 27 bytes

Output as specified

1@", "/:$(`year$.z.d)-!121;

Example:

k)1@", "/:$(`year$.z.d)-!121;
2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1966, 1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956, 1955, 1954, 1953, 1952, 1951, 1950, 1949, 1948, 1947, 1946, 1945, 1944, 1943, 1942, 1941, 1940, 1939, 1938, 1937, 1936, 1935, 1934, 1933, 1932, 1931, 1930, 1929, 1928, 1927, 1926, 1925, 1924, 1923, 1922, 1921, 1920, 1919, 1918, 1917, 1916, 1915, 1914, 1913, 1912, 1911, 1910, 1909, 1908, 1907, 1906, 1905, 1904, 1903, 1902, 1901, 1900, 1899, 1898, 1897, 1896

It can be shortened more by not formatting the output and merely returning the list of integers:

(`year$.z.d)-!121

skeevey

Posted 2016-08-11T11:25:31.933

Reputation: 4 139

2

Fourier, 17 bytes

Non competing

I added the date feature (d) earlier today, so this answer is invalid. Despite this, I decided to add this for reference.

121(5d-io10ai^~i)

Since no output format is specified, each year is separated by a newline:

Try it online

Beta Decay

Posted 2016-08-11T11:25:31.933

Reputation: 21 478

2

jq, 46 characters

(45 characters code + 1 character command line option)

now|strftime("%Y")|tonumber|range(.;.-121;-1)

Sample run:

bash-4.3$ jq -n 'now|strftime("%Y")|tonumber|range(.;.-121;-1)' | head
2016
2015
2014
2013
2012
2011
2010
2009
2008
2007

On-line test

manatwork

Posted 2016-08-11T11:25:31.933

Reputation: 17 865

2

Vim, 32 29 keystrokes

Thanks to @daniero for some help on saving some keystrokes and making the output a little neater.

"=strftime('%Y')<Enter>pqqYp<Ctrl-x>q118@q
  • <Enter> is Enter
  • <Ctrl-x> is Ctrl + X

Explanation:

"                                          # Put into the register {
 =strftime('%Y')                           #   This year in YYYY form
                <Enter>                    # }
                       p                   # Paste the register
                        qq                 # Record macro q {
                          Y                #   Yank (copy) the current line
                           p               #   Paste
                            <Ctrl-x>       #   Decrment number at cursor
                                    q      # }
                                     118@q # Do macro q 118 times

Output format:

Each number is on a separate line like below.

2016
2015
.
.
.
1898
1897

Captain Man

Posted 2016-08-11T11:25:31.933

Reputation: 386

OP clearly asks for a future-proof version, so only the second one answers the question. Don't you need to punch the Enter key (commonly refered to as <CR> in Vim context) after :pu=strftime('%Y')? Also, you can skip some keystrokes by using Y with copies the whole line. In total: :pu=strftime('%Y')<CR>qqYp<Ctrl-x>q118@q - 30 keystrokes – daniero – 2016-08-12T18:35:14.133

Also, you can yank from the = register: "=strftime('%Y')<CR>pqqYp<Ctrl-x>q118@q - 29 keystrokes, and it gets rid of the first empty line – daniero – 2016-08-12T18:42:00.997

If you're allowed to use shell as well then you can save five more keystrokes with the date utility: :.!date +\%Y<CR>qqYp<C-x>q118@q – John Gowers – 2016-08-15T10:04:33.133

2

PHP, 40 35 33 bytes

while($i<121)echo date(Y)-$i++._;

I'm just going to pretend that error reporting is always disabled for code golfing... :)

[Edit 1: Saved 5 bytes via manatwork]

[Edit 2: Saved 2 bytes via Titus]

Alex Howansky

Posted 2016-08-11T11:25:31.933

Reputation: 1 183

Forget good coding habits here. ;) Call that date() 121 times: while($i<121)echo date(Y)-$i++." ";. – manatwork – 2016-08-13T11:33:15.850

@manatwork: save another two bytes by using the underscore (or any letter or 0) as separator: while($i<121)echo date(Y)-$i++,_; (33 bytes) – Titus – 2016-08-13T12:35:00.193

@manatwork Oh cripes how did I miss that? :) – Alex Howansky – 2016-08-13T14:10:30.580

@Titus Nice. I used the "interpret an unquoted constant as a string" thing already once here, and still totally missed this one. – Alex Howansky – 2016-08-13T14:18:02.080

It is always assumed that config is at default settings, so in PHP: E_NOTICE, E_DEPRECATED and E_STRICT are off -> You´re safe. (for different settings you have to either add the command or the string to add to config to your byte count) – Titus – 2016-08-13T15:59:55.693

OK great, the default recommended ini that ships with the source turns E_NOTICE back on. Good to know I can ignore that. – Alex Howansky – 2016-08-13T16:37:06.713

I wrote almost the exact same answer, then noticed yours on page 2 :( for(;$i<121;)echo~ß,date(Y)-$i++; – aross – 2016-08-30T12:03:49.083

On second thought, you can still shave off 3 bytes (using the dodgy delim rule): for(;$i<121;)echo$i++-date(Y); – aross – 2016-08-30T12:06:45.993

2

LaTeX, 129 bytes

Or, if I'm allowed to skip the document class definition & setup, and just count the package import and for loop code: 79 bytes.

\documentclass{book}\usepackage{tikz}\begin{document}\foreach \n in {0,...,120}{\pgfmathint{\year\n}\pgfmathresult}\end{document}

Ungolfed:

\documentclass{book}
\usepackage{tikz}
\begin{document}
\foreach \n in {0,...,120}
{
\pgfmathint{\year-\n}\pgfmathresult}

\end{document} 

Output (w/ free page number :) ):

enter image description here

MH.

Posted 2016-08-11T11:25:31.933

Reputation: 261

1

JavaScript (ES6), 66 54 51 bytes

My first foray into code golf so I'm open to suggestions for improvements.

The following will output an array of the required years.

f=

_=>[...Array(121)].map((x,y)=>Date().split` `[3]-y)

console.log(f());


History

54 bytes

_=>Array(121).fill(Date().split` `[3]).map((x,y)=>x-y)

66 bytes

(y=[Date().split` `[3]],x=121)=>{while(x--)y[x]=(y[0]-x);return y}

Shaggy

Posted 2016-08-11T11:25:31.933

Reputation: 24 623

Well done this was my solution before i read any comments _=>[...Array(121)].map((v,i)=>Date().substr(11,4)-i) – James Harrington – 2017-11-26T20:32:13.417

1

JavaScript (ES6), 65 bytes

[...Array(1+- -Date().substr(11,4)).keys()].slice(-121).reverse()

You're welcome to improve and shorten it...

Thanks to @Yay295 for the fix. I was 1 year off.

Christiaan Westerbeek

Posted 2016-08-11T11:25:31.933

Reputation: 863

[...Array(+Date().substr(11,4)).keys()].slice(-120).reverse() is less! – eithed – 2016-08-11T14:21:23.000

56 Bytes in ES5:

s='';for(i=120;i--;)s+=new Date().getFullYear()-120+', ' – innovati – 2016-08-11T14:40:17.433

@innovati: That prints the same year over and over. If you replace the last '120' with 'i' as I expect you meant, it's still in the wrong order. – Yay295 – 2016-08-11T14:46:31.297

2@ChristiaanWesterbeek,@eithedog: You're both off by 1. Try [...Array(1+- -Date().substr(11,4)).keys()].slice(-121).reverse() instead. – Yay295 – 2016-08-11T15:01:25.187

1

Python 2, 67 bytes

from datetime import*;for i in range(121):print date.today().year-i

Thanks to Sp3000 for removing one byte

TheInitializer

Posted 2016-08-11T11:25:31.933

Reputation: 829

For future reference: In most situations, __import__ isn't really needed, e.g. here you can do from datetime import* and print date.today().year-i instead for -1 byte. – Sp3000 – 2016-08-11T15:01:57.933

1

VBA, 54 bytes

In the immediate pane:

For i=0 To 120:?Year(Date)-i &IIf(i=120,"",", ");:Next

In an actual Sub procedure (the VBE adds whitespace and changes ? to Print, but the code is per language specs without the whitespace and using the ? shorthand nonetheless):

Sub A()
For i = 0 To 120: Print Year(Date) - i & IIf(i = 120, "", ", ");: Next
End Sub

That's 88 characters per Notepad++, with i being an undeclared, implicit Variant local variable.

Both produce the output exactly as specified in the question, comma-separated and with a space between each year:

2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976, 1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1966, 1965, 1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956, 1955, 1954, 1953, 1952, 1951, 1950, 1949, 1948, 1947, 1946, 1945, 1944, 1943, 1942, 1941, 1940, 1939, 1938, 1937, 1936, 1935, 1934, 1933, 1932, 1931, 1930, 1929, 1928, 1927, 1926, 1925, 1924, 1923, 1922, 1921, 1920, 1919, 1918, 1917, 1916, 1915, 1914, 1913, 1912, 1911, 1910, 1909, 1908, 1907, 1906, 1905, 1904, 1903, 1902, 1901, 1900, 1899, 1898, 1897, 1896

If the commas aren't a requirement (as some other answers seem to presume), then the IIf part can be dropped, cutting the immediate pane code down to 33 bytes:

For i=0 To 120:?Year(Date)-i:Next

Mathieu Guindon

Posted 2016-08-11T11:25:31.933

Reputation: 181

1

ListSharp, 71 bytes

NUMB a=<c#DateTime.Now.Yearc#>
[FOREACH NUMB IN a TO a-120 AS y]
SHOW=y

Uses embedded c# code, new feature!!

downrep_nation

Posted 2016-08-11T11:25:31.933

Reputation: 1 152

1

Nim, 60 bytes

import times
for a in 0..120:echo getTime().getGmTime.year-a

Outputs each year on a new line. We use the getTime to get the current UNIX time, then convert it to UTC with getGmTime, get the year minus the counter variable, and echo it.

Copper

Posted 2016-08-11T11:25:31.933

Reputation: 3 684

1

Batch, 67 bytes

@set/ay=%date:~-4%,z=y-120
@for /l %%i in (%y%,-1,%z%)do @echo %%i

Neil

Posted 2016-08-11T11:25:31.933

Reputation: 95 035

1

Haskell 125 bytes

The imports take up a large part of the byte count

import Data.Time.Clock
import Data.Time.Calendar
main=fmap((\(x,_,_)->[x-120..x]).toGregorian.utctDay)getCurrentTime>>=print

HEGX64

Posted 2016-08-11T11:25:31.933

Reputation: 313

1I'm afraid the list is in the wrong order, the spec says current year first. Howevery we can save a few bytes: a) import Data.Time should be enough for all needed functions. b) use the infix version of fmap, i.e. <$>. c) do x<-toGregorain ... ;print[x-120..x] is shorter than the lambda. d) You can extract the year from getZonedTime via read.take 4.show<$>getZonedTime. All in all, including correct order: import Data.Time;do y<-read.take 4.show<$>getZonedTime;print[y,y-1..y-120]. – nimi – 2016-08-12T18:28:32.463

1

SQLite, 82 80 bytes

with b(y)as(select strftime('%Y')union select y-1 from b limit 121)select*from b

SQLFiddle

(For ANSI SQL, replace the strftime() with extract(year from current_date).)

(2 bytes saved thanks to @MickyT)

CL.

Posted 2016-08-11T11:25:31.933

Reputation: 121

You can save yourself a couple with select*from b rather than select y from b – MickyT – 2016-08-21T20:57:04.237

1

C# - DotNet core - 133 bytes

Golfed

class Program{static void Main(){int x=0,y=System.DateTime.Now.Year;while(x<121){System.Console.Write($"{y-x++}"+(x<121?", ":""));}}}

Ungolfed

class Program
{
    static void Main()
    {
        int x=0, y=System.DateTime.Now.Year;

        while(x<121)
        {
            System.Console.Write($"{y-x++}" + (x < 121 ? ", " : ""));
        }
    }
}

Output:

enter image description here

I'm sure this can be improved. I don't particularly like the if statement to display or hide the trailing comma.

Hywel Rees

Posted 2016-08-11T11:25:31.933

Reputation: 161

1

Javascript: 68 82 59 bytes

i=121;while(i--){x[i]=Date().substr(11,4)-i;}console.log(x)

Dylan Meeus

Posted 2016-08-11T11:25:31.933

Reputation: 220

Will this give a proper output when run in 2017? – Leibrug – 2016-08-12T13:21:24.127

yes, now it will, oops :-) – Dylan Meeus – 2016-08-12T14:01:45.363

1

Ruby, 47 40 39 bytes

p [*0..Time.new.year].last(121).reverse

Thanks to @Value Ink for 7 bytes!

Ideone link: https://ideone.com/yRovUl

Leibrug

Posted 2016-08-11T11:25:31.933

Reputation: 121

2Some people are submitting programs where the separator is newline, so you can take out the joining operation and just puts it. Or use p *<the rest> because they're all integers – Value Ink – 2016-08-12T19:31:58.983

Thank you @Value Ink. Since outputting an array is allowed, I used plain p instead p *<the rest> – Leibrug – 2016-08-16T05:28:02.750

1

Coffeescript, 29 bytes

->a=Date()[11..14];[a..a-120]

user3080953

Posted 2016-08-11T11:25:31.933

Reputation: 366

Welcome to Programming Puzzles & Code Golf! – Dennis – 2016-08-12T22:42:14.880

Thanks! I've been lurking for a while, finally had time to start – user3080953 – 2016-08-12T23:07:03.977

1

Python 3, 54

import time
print(*range(time.gmtime()[0],0,-1)[:121])

shooqie

Posted 2016-08-11T11:25:31.933

Reputation: 5 032

1

C#, 83 76 bytes

n=>{for(n=0;n<121;)System.Console.Write(System.DateTime.Now.Year-n+++" ");};

TheLethalCoder

Posted 2016-08-11T11:25:31.933

Reputation: 6 930

0

QBIC, 33 bytes

A=right$(_D,4)┘[!A!,!A!-120,-1|?a

steenbergh

Posted 2016-08-11T11:25:31.933

Reputation: 7 772

0

TI-Basic, 22 19 bytes

max(getDate:seq(I,I,Ans,Ans-120,~1

Timtech

Posted 2016-08-11T11:25:31.933

Reputation: 12 038

getDate:Ans(1 can be max(getDate. – lirtosiast – 2017-07-11T01:43:31.910

0

Groovy, 57 bytes

d=new Date().getYear()+1899;(d​..d-120).each{println it​}

Explanation

d=new Date().getYear()                               //returns how many years have passed since 1900
                      +1899;                         //adding 1900 will give us the current year, but we want the program to start one year before, so we do +1900-1=1899
                            (d..d-120)               //a range from the current year -1 to 120 years before that
                                      .each{print it}// for each element in the range, print it.

Output

2016
2015
2014
...
1898
1897
1896

Because of the 1899 trick, it starts with 2016 instead of 2017

Tested on the Groovy Web Console

staticmethod

Posted 2016-08-11T11:25:31.933

Reputation: 191

0

VBA 49 bytes

a=year(now):for i=a to a-120 step-1:msgbox i:next

Stupid_Intern

Posted 2016-08-11T11:25:31.933

Reputation: 373

0

JavaScript, 75 71 bytes

Saved 4 bytes thanks to Zachary.

e="";for(y=(new Date).getFullYear(),i=y;i>=y-120;i--)e+=i+" ";alert(e)

Prints all years into a single alert.

I'm sure it could be improved.

History

75 bytes

var e="";for(y=(new Date).getFullYear(),i=y;i>=y-120;i--)e+=i+" ";alert(e);

(Every year was in a separate alert)

for(y=(new Date).getFullYear(),i=y;i>=y-120;i--)alert(i);

maracuja-juice

Posted 2016-08-11T11:25:31.933

Reputation: 101

Welcome to PPCG! I'm not sure whether there exists a consensus on this, but I do not think that opening an alert for each element counts as outputting the list. – Laikoni – 2017-07-11T14:29:49.400

Hello. It is now in a single alert. (but with more bytes... :)) – maracuja-juice – 2017-07-11T15:31:53.713

Two things, I don't think you need the var before e, and I also don't think you need a semicolon after alert(e). – Zacharý – 2017-07-11T19:08:56.257

0

Tcl, 76 bytes

set y [clock format [clock seconds] -format %Y]
time {puts $y;incr y -1} 121

Try it online!

sergiol

Posted 2016-08-11T11:25:31.933

Reputation: 3 055

0

VBA, 31 Bytes

Anonymous VBE immediate window function that takes no input and outputs to the VBE immediate window

For i=0To 120:?Year(Now)-i:Next

Taylor Scott

Posted 2016-08-11T11:25:31.933

Reputation: 6 709

0

Java 75 74 bytes

public void possible(){
  for(int i=0;i<120;){
    System.out.println(new Date().getYear()-i++);
  }
}

Golfed :

void p(){for(int i=0;i<120;)System.out.println(new Date().getYear()-i++);}

1 byte off. Thanks to @LeakyNun

Roman Gräf

Posted 2016-08-11T11:25:31.933

Reputation: 2 915

void p(){for(int i=0;i<120;)System.out.println(new Date().getYear()-i++);} – Leaky Nun – 2016-08-11T14:08:19.123

1What version of Java is this? As far as I'm aware, getYear() gives you the year since 1900. That means that the current output would be [116, -3]. You'd have to add +1900 (5 bytes) in order to get the correct output. Also, Date requires an import (java.util.Date), which would add to the byte count too. – MH. – 2016-08-11T19:41:27.493

@MH. is right, this code will not compile without the imports so you need to either new java.util.Date() or add the import. Also, why don't you shorten it further by replacing the method with a lambda expression? – Shaun Wild – 2016-08-15T09:26:41.077

0

Python 2, 113 bytes

import datetime
theYear = datetime.datetime.now()
for i in range(theYear.year, theYear.year-121, -1):
   print i,','

Yousef. Python

Posted 2016-08-11T11:25:31.933

Reputation: 11

1Welcome to Code Golf! You need to list the language you're using, and because this is a [tag:code-golf] challenge, you also need the bytecount. – Value Ink – 2016-08-12T09:24:04.633

This is definitely Python 2. – Mego – 2016-08-12T09:58:41.400

Thanks. How can I calculate the bytes? – Yousef. Python – 2016-08-12T11:02:07.897

-1

Ruby, 39 36 bytes

t=Time.new.year;t.downto(t-120).to_a

Old:

t=Time.new.year;(t-120..t).to_a.reverse

Should be obvious.

Seims

Posted 2016-08-11T11:25:31.933

Reputation: 631

Where's the output? – daniero – 2016-08-11T13:29:22.320

Output is implicit. It's the return value of the last statement. Test it here: https://repl.it/CmDY/0

– Seims – 2016-08-11T13:31:59.373

Output is not implicit: You can print the result, or you can return it from a function. From the codegolf wiki: Both programs and functions may output by writing to STDOUT. Functions may also use their arguments or return value(s) for output.

– daniero – 2016-08-11T13:56:38.353

@daniero Sorry, I explained it badly. My program still holds to that definiton tho. – Seims – 2016-08-11T13:57:51.253

1I'm sorry to be so strict and boring -- of course i know what you mean, but your answer is sloppy, as it doesn't do anything. All other answers here, as far as I can tell, either prints something or returns something. – daniero – 2016-08-11T14:01:41.963

And oh, maybe this bit of the tag wiki is more relevant: Answers may be either full programs or functions (or equivalent). As a full program your answer doesn't do anything. – daniero – 2016-08-11T14:13:16.853

couldn't this be made into an executable program just by putting it in a proc, i.e.

->{Time.new.year;t.downto(t-120).to_a}

which comes out to only 2 more bytes – Alexis Andersen – 2016-08-11T14:46:18.913

@DaneAndersen that code doesn't define t – undergroundmonorail – 2016-08-11T17:50:09.693

1Just do p *t.downto(t-120) :D no proc object needed – Value Ink – 2016-08-11T19:27:32.673