Hacker News new | ask | show | jobs
by SamBam 846 days ago
I 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

1 comments

Pedantic 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.

Fortunately, it's not a very big N. ;)