2 ms·
For the part that depends on the last two digits of the year I use a method that I've not seen anywhere else. It is algorithmically more work than Conway's meth
by tzs 2y ago
For the part that depends on the last two digits of the year I use a method that I've not seen anywhere else. It is algorithmically more work than Conway's method (Y + Y//4, where Y is the last two digits of the year and // is integer division rounding positive results down), but I think is easier for quick mental computation for most people.
Let Y = 10 T + U, i.e., T is the first digit of the two digit year and U is the second digit.
If T is even, the year part is:
2 T + U if U = 0, 1, 2, or 3
2 T + U + 1 if U = 4, 5, 6, or 7
2 T + U + 2 if U = 8 or 9
If T is odd:
3 + 2 T + U if U = 0 or 1
3 + 2 T + U + 1 if U = 2, 3, 4, or 5
3 + 2 T + U + 2 if U = 6, 7, 8 or 9
Algorithmically I compute it something like this:
Compute 2 T + U
If T is even
Add 1 if T >= 4 and add another 1 if T >= 8
else
Add 3
Add 1 if T >= 2 and add another 1 if T >= 6
Those add 1s come from the way leap years are distributed within decades. The 2 T + U takes into account any leap years when U = 0. The add 1s are to take care of the leap years that happen when U != 0. In even decades those occur at 4 and 8, and in odd decades they occur at 2 and 6.
Example: for 2024, T=2 and U=4. Mentally my train of thought would be:
2 doubles to 4, plus 4 = 8 = 1 mod 7, even decade so add 1 for 4 <= T < 8 giving 2.
For that example there is not really easier than Conway's Y + Y//4, which would be 24 + 6 = 30 = 2 mod 7.
Example: for 2099, T = 9 and U = 9. Mentally that goes something like this:
9 = 2 mod 7 doubles to 4, add 9 = 2 mod 7 giving 6
T was odd so add 3 giving 9 = 2 mod 7
T was odd so the so there were leap years at 2 and 6, which each add 1
That gives us 4
I find that easier that Conway's 99 + 99/4. I can do that, and at decent speed, but I can make mistakes.
With my method you never have to deal with a number above 32, and that's only if you do no reductions mod 7 along the way. If you reduce mod 7 whenever you can you never have to deal with anything above 12.
Note: when reducing mod 7 along the way it is crucial to remember that whether you take the T is even case or the T is odd case depends on the actual value of T. E.g., when I am doing 2099 I reduce both T and U to 2 right away but first note that 9 is odd so will need to take the odd case.