4 ms·
Wow the way this is presented is super confusing, to the point that your description includes a contradiction. Specifically, of a year is divisible by 400 it's
by sebastialonso 3y ago
Wow the way this is presented is super confusing, to the point that your description includes a contradiction. Specifically, of a year is divisible by 400 it's also divisible by 100, so it's both a leap year and not leap year!
Instead of showing rules as a linear list, I'd suggest it's easier to visualise it as a tree, or simpler still, as a series of nested ifs.
- SamBam 3y agoI mean, that contradiction is also in the first two rules. It's a leap year if it's divisible by 4, but not if it's divisible by 100, so what about 200? Best to describe it as a flowchart * Is the year divisible by 400? YES -> Leap year * NO -> Is the year divisible by 100? YES -> Not leap year * NO -> Is the year divisible by 4? YES -> Leap year * NO -> Not leap year
- stoltzmann 3y agoPedantic but this drives me nuts - this algorithm has to do 3 divisions for almost every year. If you flip it upside down, it's significantly faster. * Is the year divisible by 4? NO -> not leap year * YES -> Is the year divisible by 100? NO -> leap year * YES -> Is the year divisible by 400? NO -> not leap year * YES -> leap year This way you do only 1 division for 75% of years.
- daveguy 3y agoFortunately, it's not a very big N. ;)
- throw0101c 3y ago> Instead of showing rules as a linear list, I'd suggest it's easier to visualise it as a tree, or simpler still, as a series of nested ifs. Feel free to post a comment with ASCII art or (pseudo-)code. :)
- PopAlongKid 3y ago(gets 1978 copy of The C Programming Language off the shelf) if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) [it's a leap year] else [it's not] I remember this because it is where I first learned the extra rules, beyond just every 4th year.