39
\$\begingroup\$

This challenge is quite simple. You will take an input which will be a year from 1801 to 2400, and output if it is a leap year or not.

Your input will have no newlines or trailing spaces:

1954

You will output in any way that you like that clearly tells the user if it is or isn't a leap year (I will accept y or n for yes/no)

You can get a list of leap years here: http://kalender-365.de/leap-years.php I would note that leap years are not ever four years always. 1896 is a leap year, but 1900 is not. The years that follow this "skip" are:

1900
2100
2200
2300

Test cases:

1936 ->  y
1805 ->  n
1900 ->  n
2272 ->  y
2400 ->  y 

EDIT: This is based on a standard Gregorian calendar: http://www.epochconverter.com/date-and-time/daynumbers-by-year.php

\$\endgroup\$
  • 8
    \$\begingroup\$ You should be more clear: A given year is a leap year if and only if it is (divisible by 4)∧((divisible by 100)→(divisible by 400)). \$\endgroup\$ – LegionMammal978 May 26 '15 at 11:34
  • \$\begingroup\$ Your input will have no newlines or trailing spaces. Dang it, that would have saved me 2 bytes... \$\endgroup\$ – Dennis May 26 '15 at 17:10
  • 2
    \$\begingroup\$ You should extend the accepted input range to AD 1601 thru 2400. This covers two 400-year Gregorian cycles (which proleptically start on Monday). \$\endgroup\$ – David R Tribble May 26 '15 at 18:17
  • 2
    \$\begingroup\$ Does falsy if leap year and truthy if not a leap year count as "clearly tells the user if it is or isn't"? \$\endgroup\$ – lirtosiast May 28 '15 at 21:27
  • \$\begingroup\$ @lirtosiast I think so. A lot of user assume so. \$\endgroup\$ – aloisdg Jul 20 '16 at 13:58

67 Answers 67

0
\$\begingroup\$

MathGolf, 9 bytes

4♀`*]÷~≤*

Try it online!

Explanation

4         Push 4
 ♀        Push 100
  `*      Duplicate top two elements of stack and multiply (400)
    ]     Wrap stack in array ([4, 100, 400])
     ÷    Check input for divisibility with all array items
      ~   Dump array onto stack
       ≤  Check if divisibility by 100 is <= than divisibility with 400
        * Multiply with the divisibility bool for 4, works like logical and

The trick is that ensures that if a number is divisible by 100, it must also be divisible by 400, but if it's not divisible by 100, anything goes.

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

Clam, 33 31 bytes

=a*rp|e%a*"400"0&%a*"100"e%a*40

Try it online!

-2 bytes thanks to ASCII-only

Outputs true for leap years. Outputs false for other years

Explanation

=a*rp|e%a*"400"0&%a*"100"e%a*40
=a*r                             read input and store in a*
    p                            Print..
       %a*"400"                    a* % 400
      e                            ==
               0                   0
     |                             OR
                 %a*"100"            a* % 100
                &                    AND
                          %a*4       a* % 4
                         e           ==
                              0      0

Very awkward explanation, but basically what it does is this:

print(year % 400 == 0 || (year % 100 && year % 4 == 0))

Clam follows JS rules for truthy and falsey values, meaning year % 100 is true if it does not equal 0 (and it's shorter than adding an e before it and an 0 after it)

Resulting JS:

myVar = read();
console.log(year % 400 == 0 || (year % 100 && year % 4 == 0));
\$\endgroup\$
  • \$\begingroup\$ ew 8552. use something nicer like 4545 pls \$\endgroup\$ – ASCII-only Jan 8 at 6:18
  • \$\begingroup\$ jk, 31 \$\endgroup\$ – ASCII-only Jan 8 at 6:19
  • \$\begingroup\$ also pls fix your indentation in the explanation :| \$\endgroup\$ – ASCII-only Jan 8 at 6:27
  • \$\begingroup\$ fixed that year % 100 thing \$\endgroup\$ – ASCII-only Jan 8 at 6:42
0
\$\begingroup\$

JavaScript, 19 bytes

y=>!(y%25?y%4:y%16)

Try it online!

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

Swift, 82 bytes

func l(y:Int){y%4==0 && (y%100 != 0 || y%400==0) ?print("Y"):print("N")}
l(y:1936)

Try it online!

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

PHP, 35 bytes

<?=date(L,strtotime(Jan.$argv[1]));

Try it online!

Came across the L option in the PHP date formatter, which returns if the year of the date is a leap year. After that, it was just a matter of trying to get the timestamp of the year in the shortest way (though I'm not too confident this is the shortest)

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

05AB1E, 9 7 bytes

т‰0Kθ4Ö

Try it online or verify all test cases.

Explanation:

т‰         # Divmod the (implicit) input by 100
           #  i.e. 1900 → [19,00]
           #  i.e. 1936 → [19,36]
           #  i.e. 1991 → [19,91]
           #  i.e. 2000 → [20,00]
  0K       # Remove all 0s
           #  i.e. [19,00] → [19]
           #  i.e. [19,36] → [19,36]
           #  i.e. [19,91] → [19,91]
           #  i.e. [20,00] → [20]
    θ      # Pop and get the last item
           #  i.e. [19] → 19
           #  i.e. [19,36] → 36
           #  i.e. [19,91] → 91
           #  i.e. [20] → 20
     4Ö    # Check if it's divisible by 4 (and output the result implicitly)
           #  i.e. 19 → 0 (falsey)
           #  i.e. 36 → 1 (truthy)
           #  i.e. 91 → 0 (falsey)
           #  i.e. 20 → 1 (truthy)
\$\endgroup\$
-2
\$\begingroup\$

Reality, 1 byte

L

input via stdinput


The language was made after the challenge

\$\endgroup\$

Your Answer

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