40
\$\begingroup\$

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.
\$\endgroup\$
  • 7
    \$\begingroup\$ 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). \$\endgroup\$ – trichoplax Aug 11 '16 at 11:31
  • 3
    \$\begingroup\$ I 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. \$\endgroup\$ – trichoplax Aug 11 '16 at 11:33
  • 19
    \$\begingroup\$ Jeanne Louise Calment lived 122 years. \$\endgroup\$ – Zenadix Aug 11 '16 at 15:32
  • 10
    \$\begingroup\$ Lad, that was way too early of an accept. \$\endgroup\$ – Addison Crump Aug 11 '16 at 17:30
  • 6
    \$\begingroup\$ Is 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. \$\endgroup\$ – CompuChip Aug 14 '16 at 9:58

59 Answers 59

2
\$\begingroup\$

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]

\$\endgroup\$
  • \$\begingroup\$ Forget good coding habits here. ;) Call that date() 121 times: while($i<121)echo date(Y)-$i++." ";. \$\endgroup\$ – manatwork Aug 13 '16 at 11:33
  • \$\begingroup\$ @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) \$\endgroup\$ – Titus Aug 13 '16 at 12:35
  • \$\begingroup\$ @manatwork Oh cripes how did I miss that? :) \$\endgroup\$ – Alex Howansky Aug 13 '16 at 14:10
  • \$\begingroup\$ @Titus Nice. I used the "interpret an unquoted constant as a string" thing already once here, and still totally missed this one. \$\endgroup\$ – Alex Howansky Aug 13 '16 at 14:18
  • \$\begingroup\$ 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) \$\endgroup\$ – Titus Aug 13 '16 at 15:59
2
\$\begingroup\$

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

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

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)
\$\endgroup\$
2
\$\begingroup\$

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

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

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
\$\endgroup\$
1
\$\begingroup\$

Python 2, 67 bytes

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

Thanks to Sp3000 for removing one byte

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Sp3000 Aug 11 '16 at 15:01
1
\$\begingroup\$

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!!

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

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.

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

Batch, 67 bytes

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

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
\$\endgroup\$
  • 1
    \$\begingroup\$ I'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]. \$\endgroup\$ – nimi Aug 12 '16 at 18:28
1
\$\begingroup\$

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.

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

Javascript: 68 82 59 bytes

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

\$\endgroup\$
  • \$\begingroup\$ Will this give a proper output when run in 2017? \$\endgroup\$ – Leibrug Aug 12 '16 at 13:21
  • \$\begingroup\$ yes, now it will, oops :-) \$\endgroup\$ – Dylan Meeus Aug 12 '16 at 14:01
1
\$\begingroup\$

Coffeescript, 29 bytes

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

\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! \$\endgroup\$ – Dennis Aug 12 '16 at 22:42
  • \$\begingroup\$ Thanks! I've been lurking for a while, finally had time to start \$\endgroup\$ – user3080953 Aug 12 '16 at 23:07
1
\$\begingroup\$

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

\$\endgroup\$
  • 2
    \$\begingroup\$ Some 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 \$\endgroup\$ – Value Ink Aug 12 '16 at 19:31
  • \$\begingroup\$ Thank you @Value Ink. Since outputting an array is allowed, I used plain p instead p *<the rest> \$\endgroup\$ – Leibrug Aug 16 '16 at 5:28
1
\$\begingroup\$

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)

\$\endgroup\$
  • \$\begingroup\$ You can save yourself a couple with select*from b rather than select y from b \$\endgroup\$ – MickyT Aug 21 '16 at 20:57
1
\$\begingroup\$

Python 3, 54

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

C#, 83 76 bytes

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

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}
\$\endgroup\$
  • \$\begingroup\$ Well done this was my solution before i read any comments _=>[...Array(121)].map((v,i)=>Date().substr(11,4)-i) \$\endgroup\$ – James Harrington Nov 26 '17 at 20:32
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ [...Array(+Date().substr(11,4)).keys()].slice(-120).reverse() is less! \$\endgroup\$ – eithed Aug 11 '16 at 14:21
  • \$\begingroup\$ 56 Bytes in ES5: s='';for(i=120;i--;)s+=new Date().getFullYear()-120+', ' \$\endgroup\$ – innovati Aug 11 '16 at 14:40
  • \$\begingroup\$ @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. \$\endgroup\$ – Yay295 Aug 11 '16 at 14:46
  • 2
    \$\begingroup\$ @ChristiaanWesterbeek,@eithedog: You're both off by 1. Try [...Array(1+- -Date().substr(11,4)).keys()].slice(-121).reverse() instead. \$\endgroup\$ – Yay295 Aug 11 '16 at 15:01
0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ void p(){for(int i=0;i<120;)System.out.println(new Date().getYear()-i++);} \$\endgroup\$ – Leaky Nun Aug 11 '16 at 14:08
  • 1
    \$\begingroup\$ What 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. \$\endgroup\$ – MH. Aug 11 '16 at 19:41
  • \$\begingroup\$ @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? \$\endgroup\$ – Shaun Wild Aug 15 '16 at 9:26
0
\$\begingroup\$

Python 2, 113 bytes

import datetime
theYear = datetime.datetime.now()
for i in range(theYear.year, theYear.year-121, -1):
   print i,','
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Golf! You need to list the language you're using, and because this is a code-golf challenge, you also need the bytecount. \$\endgroup\$ – Value Ink Aug 12 '16 at 9:24
  • \$\begingroup\$ This is definitely Python 2. \$\endgroup\$ – Mego Aug 12 '16 at 9:58
  • \$\begingroup\$ Thanks. How can I calculate the bytes? \$\endgroup\$ – Yousef. Python Aug 12 '16 at 11:02
0
\$\begingroup\$

QBIC, 33 bytes

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

VBA 49 bytes

a=year(now):for i=a to a-120 step-1:msgbox i:next
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 22 19 bytes

max(getDate:seq(I,I,Ans,Ans-120,~1
\$\endgroup\$
  • \$\begingroup\$ getDate:Ans(1 can be max(getDate. \$\endgroup\$ – lirtosiast Jul 11 '17 at 1:43
0
\$\begingroup\$

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);
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Laikoni Jul 11 '17 at 14:29
  • \$\begingroup\$ Hello. It is now in a single alert. (but with more bytes... :)) \$\endgroup\$ – maracuja-juice Jul 11 '17 at 15:31
  • \$\begingroup\$ 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). \$\endgroup\$ – Zacharý Jul 11 '17 at 19:08
0
\$\begingroup\$

Tcl, 76 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
-1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Where's the output? \$\endgroup\$ – daniero Aug 11 '16 at 13:29
  • \$\begingroup\$ Output is implicit. It's the return value of the last statement. Test it here: repl.it/CmDY/0 \$\endgroup\$ – Seims Aug 11 '16 at 13:31
  • \$\begingroup\$ 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. \$\endgroup\$ – daniero Aug 11 '16 at 13:56
  • 1
    \$\begingroup\$ I'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. \$\endgroup\$ – daniero Aug 11 '16 at 14:01
  • 1
    \$\begingroup\$ Just do p *t.downto(t-120) :D no proc object needed \$\endgroup\$ – Value Ink Aug 11 '16 at 19:27

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.