Yes, if you consider the following as "two conditions":
If a year is not divisible by 4, or divisible by 100 but not by 400, then it's a common year.
Otherwise, it's a leap year.
Implemented in C, for example:
int IsCommon(int year)
{
if (year%4 != 0 || (year%100 == 0 && year%400 != 0))
return 1;
else
return 0;
}
Please note however, that in terms of runtime-performance, there is not much to gain here by "condensing" Wikipedia's definition from "three conditions" into "two conditions". The logical operators (||
and &&
) yield the same branching that a conditional statement such as if/else if
would...
Here is an alternative solution, that relies on the way in which a switch/case
statement is compiled, and performs only a single branching operation. Please note that it's a programmatic solution, not an algorithmic solution. It is suitable at least for C, C++ and Java (excluding minor semantics), but there are probably similar variations of it in other languages as well (for example, a dictionary in Python):
int IsCommon(int year)
{
switch (year%400)
{
case 0:
case 4:
case ...
case 96:
case 104:
case ...
case 196:
case 204:
case ...
case 296:
case 304:
case ...
case 396:
return 0;
}
return 1;
}
If my memory serves correctly, then a switch/case
statement is compiled in the most efficient way when all cases are listed in an incremented and consecutive order, starting from 0. So you can further expand this code, as in the example below:
int IsCommon(int year)
{
switch (year%400)
{
case 0: return 0;
case 1: return 1;
case 2: return 1;
case 3: return 1;
case 4: return 0;
case 5: return 1;
case 6: return 1;
case 7: return 1;
case ...
case 96: return 0;
case 97: return 1;
case 98: return 1;
case 99: return 1;
case 100: return 1;
case 101: return 1;
case 102: return 1;
case 103: return 1;
case 104: return 0;
case 105: return 1;
case 106: return 1;
case 107: return 1;
case ...
case 196: return 0;
case 197: return 1;
case 198: return 1;
case 199: return 1;
case 200: return 1;
case 201: return 1;
case 202: return 1;
case 203: return 1;
case 204: return 0;
case 205: return 1;
case 206: return 1;
case 207: return 1;
case ...
case 296: return 0;
case 297: return 1;
case 298: return 1;
case 299: return 1;
case 300: return 1;
case 301: return 1;
case 302: return 1;
case 303: return 1;
case 304: return 0;
case 305: return 1;
case 306: return 1;
case 307: return 1;
case ...
case 396: return 0;
case 397: return 1;
case 398: return 1;
case 399: return 1;
}
return -1; // Just in order to prevent a compilation error (i.e., dummy)
}