by sebastialonso · 2y ago · 0 comments
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.
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
* 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.
Feel free to post a comment with ASCII art or (pseudo-)code. :)
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.