Leap year algorithm
-
Hi, I want to have a fast algorithm to determine if it’s leap year, is this a good one?
bool isLeapYear(int year)
{
bool leapYear = year%4 == 0 && (year %100 != 0 || year%400 == 0;
return leapYear;
}Thanks!
It's the only algorithm I know. It's so simple that asking for another one makes no sense. To optimize for speed you may define it as macro or inline function (better):
#define IS_LEAP_YEAR(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
inline bool isLeapYear(int year) const
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
} -
Hi, I want to have a fast algorithm to determine if it’s leap year, is this a good one?
bool isLeapYear(int year)
{
bool leapYear = year%4 == 0 && (year %100 != 0 || year%400 == 0;
return leapYear;
}Thanks!
-
Hi, I want to have a fast algorithm to determine if it’s leap year, is this a good one?
bool isLeapYear(int year)
{
bool leapYear = year%4 == 0 && (year %100 != 0 || year%400 == 0;
return leapYear;
}Thanks!
-
Hi, I want to have a fast algorithm to determine if it’s leap year, is this a good one?
bool isLeapYear(int year)
{
bool leapYear = year%4 == 0 && (year %100 != 0 || year%400 == 0;
return leapYear;
}Thanks!
It's looks good, syntax errors aside (missing a closing bracket).
Steve