In the following, let Y = the last two digits of the year (e.g., 18 for 2018), and let N be the value we are trying to be compute. We only actually need N mod 7, but I'm going to leave out the reduction mod 7 in the equations to reduce clutter.
The simplest way to compute N is simply:
N = Y + Y//4
I'll use Python3-like arithmetic and pseudocode in this comment, so "//" is integer division (with x for times to avoid accidental italics).To keep the numbers smaller during mental calculation, people have developed alternatives. The one given in that link is this:
N = Y // 12
N += Y % 12
N += (Y % 12) // 4
An interesting alternative, called the "odd+11" method, is given on the Wikipedia article [1]: if odd(Y): Y += 11
Y = Y // 2
if odd(Y): Y += 11
N = -Y
For the last step there, N = -Y, it will usually be easier and clearer to reduce Y mod 7 before doing that N = -Y. Also, given Y, sometimes the simplest way to get -Y mod 7 is to just note what you have to add to Y to get to a multiple of 7. For example, if when you get to that step Y = 20, note that adding 1 to Y gives a multiple of 7, so -20 mod 7 = 1.Anyway, here's my method. It keeps the numbers smaller--if you reduce mod 7 aggressively never more than 12--at the cost of slightly more branches in the logic.
Let the last two digits of the year be T and U, so Y = 10 T + U.
N = 2 x T
if odd(T): N += 3
N += U
if odd(T):
N += (U+2)//4
else:
N += U//4
That if...else is taking into account the number of leap years that have occurred in the current decade (not including the year T0). When doing mental calculation, it is probably easier just to remember that if T is odd, at 1 if U >= 2 and add another 1 if U >= 6, and if T is even same except at 4 and 8.Here are examples, using some years from the link, with parenthetical explanations for some of the numbers:
2018: T=1, U=8. 2x1(T) + 3(T is odd) + 8(U) + 2(U>=2,6) = 1 mod 7.
1929: T=2, U=9. 2x2(T) + 9(U) + 2(U>=4,8) = 1 mod 7.
1999: T=9, U=9. 2x2(T%7) + 3(T is odd) + 2(U%7) + 2(U>=2,6) = 4 mod 7. Note that I reduced U and T mod 7 inline when using them. You can do this as long as when checking odd/even you use the original T, and when adding in the leap year correction you use the original U. E.g., for 99, you could compute it like it was 22, except you have to add the 3 for odd T, and use 9 for the U>=2,6 check.
1982: T=8, U=2. 2x1(T%7) + 2(U) = 4 mod 7.
1969: T=6, U=9. 2x(-1)(T%7) + 2(U%7) + 2(U>=4,8) = 2 mod 7.
Some might find changing the leap year handling to this a little easier instead of just remembering the 2,6 or 4,8 +1 points. Change the if...else to this:
N += U//4
if odd(T) and N%4 >= 2: N += 1
E.g., compute the leap year adjustment with U//4 regardless of whether T is odd or even, and if U is in {2, 3, 6, 7} and T is odd, add one more.[1] https://en.wikipedia.org/wiki/Doomsday_rule#The_%22odd_+_11%...