Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rindeal
Last active July 2, 2020 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rindeal/8ed6fdb24650c9d17416 to your computer and use it in GitHub Desktop.
Save rindeal/8ed6fdb24650c9d17416 to your computer and use it in GitHub Desktop.
IsLeapYear()
#include <stdbool.h>
#include <assert.h>
/**
* Modified version of https://stackoverflow.com/a/11595914/2566213
* with 4000th check added.
*/
bool
IsLeapYear(const int y)
{
return (
((y & 3) == 0) &&
(
(y % 25 != 0 ) ||
(
((y & 15) == 0) &&
(y % 4000 != 0)
)
)
);
}
void
AssertIsLeapYear()
{
assert(!IsLeapYear(1901));
assert(!IsLeapYear(1903));
assert(!IsLeapYear(1905));
assert(IsLeapYear(1904));
assert(IsLeapYear(1908));
assert(IsLeapYear(1996));
assert(IsLeapYear(2004));
assert(!IsLeapYear(1700));
assert(!IsLeapYear(1800));
assert(!IsLeapYear(1900));
assert(!IsLeapYear(2100));
assert(IsLeapYear(1600));
assert(IsLeapYear(2000));
assert(IsLeapYear(2400));
assert(IsLeapYear(3600));
assert(IsLeapYear(4400));
assert(!IsLeapYear(4000));
assert(!IsLeapYear(8000));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment