Instantly share code, notes, and snippets.

@rindeal /leap.c
Last active Oct 29, 2015

Embed
What would you like to do?
IsLeapYear()
/**
* Modified version of http://stackoverflow.com/a/11595914/2566213
* with added 4000th check.
*/
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