Skip to main content
added 1 character in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

CJam, 18 16 bytes

q~[YAK]f{2#%!}:^

Gives 1 (truthy) for leap years and 0 (falsy) otherwise.

Run all test cases here.

Explanation

q~                 e# Read and eval input.
  [YAK]            e# Push an array containing 2, 10, 20 (the square roots of the
                   e# relevant divisors).
       f{    }     e# Map this block onto that array, also passing in the input year.
         2#        e# Square the divisor.
           %!      e# Modulo followed by logical negation. Gives 1 if the year is divisible
                   e# by the given divisor and 0 otherwise.
                   e# At this point we have one of the following arrays:
                   e#   [0 0 0] - not a leap year
                   e#   [1 0 0] - a leap year
                   e#   [1 1 0] - not a leap year
                   e#   [1 1 1] - a leap year
              :^   e# Reduce XOR onto this array, which gives 1 if there is an odd number
                   e# of 1s and 0 if there's an even number.

CJam, 18 16 bytes

q~[YAK]f{2#%!}:^

Gives 1 (truthy) for leap years and 0 (falsy) otherwise.

Run all test cases here.

Explanation

q~                 e# Read and eval input.
  [YAK]            e# Push an array containing 2, 10, 20 (the square roots of the
                   e# relevant divisors.
       f{    }     e# Map this block onto that array, also passing in the input year.
         2#        e# Square the divisor.
           %!      e# Modulo followed by logical negation. Gives 1 if the year is divisible
                   e# by the given divisor and 0 otherwise.
                   e# At this point we have one of the following arrays:
                   e#   [0 0 0] - not a leap year
                   e#   [1 0 0] - a leap year
                   e#   [1 1 0] - not a leap year
                   e#   [1 1 1] - a leap year
              :^   e# Reduce XOR onto this array, which gives 1 if there is an odd number
                   e# of 1s and 0 if there's an even number.

CJam, 18 16 bytes

q~[YAK]f{2#%!}:^

Gives 1 (truthy) for leap years and 0 (falsy) otherwise.

Run all test cases here.

Explanation

q~                 e# Read and eval input.
  [YAK]            e# Push an array containing 2, 10, 20 (the square roots of the
                   e# relevant divisors).
       f{    }     e# Map this block onto that array, also passing in the input year.
         2#        e# Square the divisor.
           %!      e# Modulo followed by logical negation. Gives 1 if the year is divisible
                   e# by the given divisor and 0 otherwise.
                   e# At this point we have one of the following arrays:
                   e#   [0 0 0] - not a leap year
                   e#   [1 0 0] - a leap year
                   e#   [1 1 0] - not a leap year
                   e#   [1 1 1] - a leap year
              :^   e# Reduce XOR onto this array, which gives 1 if there is an odd number
                   e# of 1s and 0 if there's an even number.
added 988 characters in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

CJam, 1818 16 bytes

q~[YAK]f{2#%!}2b3%:^

Gives 1 (truthy) for leap years and 0 (falsy) otherwise.

Run all test cases here.Run all test cases here.

Explanation

q~                 e# Read and eval input.
  [YAK]            e# Push an array containing 2, 10, 20 (the square roots of the
                   e# relevant divisors.
       f{    }     e# Map this block onto that array, also passing in the input year.
         2#        e# Square the divisor.
           %!      e# Modulo followed by logical negation. Gives 1 if the year is divisible
                   e# by the given divisor and 0 otherwise.
                   e# At this point we have one of the following arrays:
                   e#   [0 0 0] - not a leap year
                   e#   [1 0 0] - a leap year
                   e#   [1 1 0] - not a leap year
                   e#   [1 1 1] - a leap year
              2b:^   e# InterpretReduce thatXOR asonto anthis array, ofwhich base-2gives digits,1 givingif 4there oris 7an forodd leapnumber
                   e# yearsof 1s and 0 or 6 otherwise.
                3% e# Take the result modulo 3, giving 1if orthere's 0,an aseven requirednumber.

Summing the array (i.e. interpreting it as base 1) and taking the result modulo 2 also works.

CJam, 18 bytes

q~[YAK]f{2#%!}2b3%

Gives 1 (truthy) for leap years and 0 (falsy) otherwise.

Run all test cases here.

Explanation

q~                 e# Read and eval input.
  [YAK]            e# Push an array containing 2, 10, 20 (the square roots of the
                   e# relevant divisors.
       f{    }     e# Map this block onto that array, also passing in the input year.
         2#        e# Square the divisor.
           %!      e# Modulo followed by logical negation. Gives 1 if the year is divisible
                   e# by the given divisor and 0 otherwise.
                   e# At this point we have one of the following arrays:
                   e#   [0 0 0] - not a leap year
                   e#   [1 0 0] - a leap year
                   e#   [1 1 0] - not a leap year
                   e#   [1 1 1] - a leap year
              2b   e# Interpret that as an array of base-2 digits, giving 4 or 7 for leap
                   e# years and 0 or 6 otherwise.
                3% e# Take the result modulo 3, giving 1 or 0, as required.

Summing the array (i.e. interpreting it as base 1) and taking the result modulo 2 also works.

CJam, 18 16 bytes

q~[YAK]f{2#%!}:^

Gives 1 (truthy) for leap years and 0 (falsy) otherwise.

Run all test cases here.

Explanation

q~                 e# Read and eval input.
  [YAK]            e# Push an array containing 2, 10, 20 (the square roots of the
                   e# relevant divisors.
       f{    }     e# Map this block onto that array, also passing in the input year.
         2#        e# Square the divisor.
           %!      e# Modulo followed by logical negation. Gives 1 if the year is divisible
                   e# by the given divisor and 0 otherwise.
                   e# At this point we have one of the following arrays:
                   e#   [0 0 0] - not a leap year
                   e#   [1 0 0] - a leap year
                   e#   [1 1 0] - not a leap year
                   e#   [1 1 1] - a leap year
              :^   e# Reduce XOR onto this array, which gives 1 if there is an odd number
                   e# of 1s and 0 if there's an even number.
added 988 characters in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

CJam, 18 bytes

q~[YAK]f{2#%!}2b3%

Gives 1 (truthy) for leap years and 0 (falsy) otherwise.

Run all test cases here. I'll add an explanation in a bit

Explanation

q~                 e# Read and eval input.
  [YAK]            e# Push an array containing 2, 10, 20 (the square roots of the
                   e# relevant divisors.
       f{    }     e# Map this block onto that array, also passing in the input year.
         2#        e# Square the divisor.
           %!      e# Modulo followed by logical negation. Gives 1 if the year is divisible
                   e# by the given divisor and 0 otherwise.
                   e# At this point we have one of the following arrays:
                   e#   [0 0 0] - not a leap year
                   e#   [1 0 0] - a leap year
                   e#   [1 1 0] - not a leap year
                   e#   [1 1 1] - a leap year
              2b   e# Interpret that as an array of base-2 digits, giving 4 or 7 for leap
                   e# years and 0 or 6 otherwise.
                3% e# Take the result modulo 3, giving 1 or 0, as required.

Summing the array (i.e. interpreting it as base 1) and taking the result modulo 2 also works.

CJam, 18 bytes

q~[YAK]f{2#%!}2b3%

Gives 1 (truthy) for leap years and 0 (falsy) otherwise.

Run all test cases here. I'll add an explanation in a bit.

CJam, 18 bytes

q~[YAK]f{2#%!}2b3%

Gives 1 (truthy) for leap years and 0 (falsy) otherwise.

Run all test cases here.

Explanation

q~                 e# Read and eval input.
  [YAK]            e# Push an array containing 2, 10, 20 (the square roots of the
                   e# relevant divisors.
       f{    }     e# Map this block onto that array, also passing in the input year.
         2#        e# Square the divisor.
           %!      e# Modulo followed by logical negation. Gives 1 if the year is divisible
                   e# by the given divisor and 0 otherwise.
                   e# At this point we have one of the following arrays:
                   e#   [0 0 0] - not a leap year
                   e#   [1 0 0] - a leap year
                   e#   [1 1 0] - not a leap year
                   e#   [1 1 1] - a leap year
              2b   e# Interpret that as an array of base-2 digits, giving 4 or 7 for leap
                   e# years and 0 or 6 otherwise.
                3% e# Take the result modulo 3, giving 1 or 0, as required.

Summing the array (i.e. interpreting it as base 1) and taking the result modulo 2 also works.

Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975
Loading