Skip to content

Instantly share code, notes, and snippets.

@dungsaga
Last active September 25, 2025 10:04
Show Gist options
  • Select an option

  • Save dungsaga/875d8593f3115e8031d7cb62806b0df6 to your computer and use it in GitHub Desktop.

Select an option

Save dungsaga/875d8593f3115e8031d7cb62806b0df6 to your computer and use it in GitHub Desktop.
Hirofumi Nakai Improved (HNI) - a method for Doomsday calculation

Hirofumi Nakai Improved

The Doomsday rule was devised by John Conway in 1973 to compute the day of week in your head (https://en.wikipedia.org/wiki/Doomsday_rule). Others have made improvements for easier mental calculation.

Currently, the simplest one is published in 2023 in the paper "A Simple Formula for Doomsday" by Hirofumi Nakai (https://thatsmaths.com/2023/06/22/a-simple-formula-for-the-weekday/).

  • split the year into century part and the remaining 2-digit part: Y = 100c + y
  • compute the modulo 4 of these 2 parts: c₄ = c % 4, y₄ = y % 4
  • the Doomsday would be (5(c₄ + y₄ - 1) + 10y) % 7

I made some improvements to make it even easier.

(5(c₄ + y₄ - 1) + 10y) % 7

= (5(c₄ + y₄ - 1) + 10y - 14y - 7(c₄ + y₄ - 1)) % 7

= (-2(c₄ + y₄ - 1) - 4y) % 7

= 2(1 - c₄ - y₄ - 2y) % 7

Since y has 2 digits y = 10a + b, we can quickly compute 2y % 7

2y % 7

= (20a + 2b) % 7

= (20a + 2b - 21a) % 7

= (2b - a) % 7

Replace 2y with this, we have a new formula which I named HNI (Hirofumi Nakai Improved)

2(1 - c₄ - y₄ + a - 2b) % 7

Faster mental math

The above formulas are simple, but not so quick. If you want to calculate faster, you should memorize some values. For more detail, see https://worldmentalcalculation.com/how-to-calculate-calendar-dates/

The calculation of Doomsday contains 2 parts: W = (C + Y) % 7

  • C is based on the century part: C = (2 - 2c₄) % 7
  • Y is based on the year part: Y = 2(a - 2b - y₄) % 7

You can memorize the values for C = m2053(c₄) = (2 - 2c₄) % 7:

c₄ 0 1 2 3
(2-2c₄)%7 2 0 5 3

Don't worry! There are only 4 alternating values for C:

  • C=2 with year 1600s / 2000s / 2400s …
  • C=0 with year 1700s / 2100s / 2500s …
  • C=5 with year 1800s / 2200s / 2600s …
  • C=3 with year 1900s / 2300s / 2700s …

The remaining part you have to calculate is Y = 2(a - 2b - y₄) % 7. These numbers are quite small, so you can do it quickly with some practise.

You can also add or subtract 7/14/21/... to make intermediate value even smaller.

Using the notation b₄ = b % 4, we can make it faster:

  • if a is even, y₄ = b₄ = (b-4)₄ = (b-8)₄ (you can subtract 4 or 8 instead of dividing by 4)
  • if a is odd, y₄ = (b+2)₄ = (b-2)₄ = (b-6)₄ (you can subtract 2 or 6 instead of dividing by 4)

Even faster with memorization

Y = 2(a - 2b - y₄) % 7

= (2a - 4b - 2y₄) % 7

= (2a + 3b - 2y₄) % 7

= (2a + b + 2(b - y₄)) % 7

when a is even, 10a % 4 = 0, thus y₄ = b₄

= (2a + b + 2(b - b₄)) % 7

= (2a + b + 2(4(b/4))) % 7

= (2a + b + b/4) % 7

when a is odd, 10a % 4 = 2, thus y₄ = (b+2)₄

= (2a + b + 2(b - (b+2)₄)) % 7

= (2a + b + 2(b+2 - (b+2)₄) - 4) % 7

= (2a + b + 2(4((b+2)/4)) - 4) % 7

= (2a + b + (b+2)/4 - 4) % 7

= (2a + b + (b+10)/4 - 6) % 7

= (2a + b + (10+b)/4 + 1) % 7

Now, Y requires only simple math with a and b, the 2 digits of y.

You can also memorize values of b/4 and (10+b)/4+1 to save time.

b 0..3 4..7 8,9
b/4 0 1 2
b 0,1 2..5 6..9
10+b 10,11 12..15 16..19
(10+b)/4+1 3 4 5

Instead of memorization, we can simplify the formula

when a is odd (using p as the 9-complement of b p = 9 - b)

= (2a + b + (b+2)/4 - 4) % 7

= (2a + b - 2 + (b+2-8)/4) % 7

= (2a + b - 9 + (b-9+3)/4) % 7

= (2a - (9-b) + (-p+3)/4) % 7

= (2a - p - p/4) % 7

This is similar to the formula when a is even (with -p in the place of 'b')

Another formula for Y

Y = (y + y/4) % 7

= (10a + b + (10a+b)/4) % 7

= (3a + b + 2a + (2a+b)/4) % 7

= (b - 2a + (b+2a)/4) % 7

= (b - a + (b-2a)/4) % 7

if you don't mind some multiplication, we can make it shorter:

= ((5b - 6a)/4) % 7

= ((5(b-a) - a)/4) % 7

Yet another formula for Y

Y = (y + y/4) % 7

= (y₄ + 4(y/4) + (y/4)) % 7

= (y₄ - 2(y/4)) % 7

= (y₄ - 4(y/4)/2) % 7

= (y₄ - (y-y₄)/2) % 7

= (y₄ - (10u+v)/2) % 7

= (y₄ - 5u - v/2) % 7

= (y₄ + 2u - v/2) % 7

Here we assumed that that y-y₄ = 10u + v (v is even because (y-y₄) % 4 = 0)

Further reading

Thanks to Miroslav (@Borg19l71) for sharing prior art research. There are many other formulas. You can see more at https://qr.ae/pCX9gy

Memorization for all parts of date (day, month, year, century) https://davecturner.github.io/2021/12/27/doomsday-rule.html

@dungsaga
Copy link
Author

dungsaga commented Sep 25, 2025

The following is the answer of Miroslav Janković on 2025-08-28
(copied from https://qr.ae/pCX9gy):

date= year-m-d :

year=xccab=10000x+100cc+10a+b

x=don’t care ( Gregorian calendar repeats every 10000 years)

———————————————————————————————————————

Weekday W=C+Y+d+(M+L) for monthly positive methods like

Weekday W=C+Y+d-(M+L) for monthly negative methods ( AKA Doomsday methods )

Weekday W=W%7=W±7n ( modulo 7 )

1=1st WORKDAY=Monday 2=2nd WORKDAY=Tuesday etc. 6=Saturday 7=0=Sunday

———————————————————————————————————————

century number = year/100=xcc or (year%10000)/100=cc

century number xcc or cc is not equal to classic century.

————————————————————————————————————————————-

number/code C for century number:

C= A–2*(cc%4) ( for Gregorian calendar dates ) ( A is anchor value for given method ) or

C=A+cc/4–2cc ( Zeller’s Congruence method for Gregorian calendar dates ) and

C = (26+A)–cc ( for Julian calendar dates ) (number 26 can be replaced with 26-7z=26,19,12,5 )

Usually mnemonics are given from century number divisible by 4 e.g. century number 0..3

Gregorian century table for Standard method C=6420 (A=6) Julian C=6543210 6543210 65(43210)

Gregorian century table for Doomsday method C=2053 (A=2) Julian C=2106543 2106543 21(06543)

————————————————————————————————————————————-

number/code Y=f(y) for year in century y=10a+b=00..99:

( a=year tens )

( b=year ones )

( many methods )

————————————————————————————————————————

Y=f(y)=

1) (5y)/4

2) y+y/4 ( As in Zeller’s Congruence method )

3) (y%7)+y/4

4) q+f(rest) where q=y/12 rest=y-12q=y%12

5) (y%4)-rest/2=(y%4)+2a-b/2 where rest=y-(y%4)=4n=10a+b

6) odd+11 method=-(y+11 until divisible by 4)/2:

6a) if (y) odd +11

6b) divide by 2

6c) if odd +11

6d) negate (by modulo 7)=count upward to number divisible by 7=7n

7) 2a+b+G where G=(b+14(a%2))/4=group number where

G=0,1,2 for years g=0..3, 4..7, 8..9 for even decades

G=3,4,5 for years g=10..11, 12..15, 16..19 for odd decades

where g=(y%20) year in a group of 20

8) 2a[+3 if a odd]+b+b/4[+1 if a odd and b=2,3,6,7]

9) 2a

+(b+b/4) if a even

-(c+c/4) if a odd where c=9-b ( 9’s complement of b )

10) b-a+floor((b-2a)/4)

11) floor((5b-6a)/4)

12) f(base)+(y-base)+leap_years_from_base_plus_1_upward_to_y_inclusive

where base years can be selected so f(base)=0:

base=00,06,17,23,28,34,45,51,56,62,73,79,84,90

13) mod28 and minus method:

13a) mod28 <=> ( -30+2 or -60+4 or -90+6 )

13b)

if 4.. => +1

if 8.. => -5

if 12.. => -11

if 16.. => -10

if 20.. => -16

if 24.. => -22

if 28.. => -28

if 32.. => -27

14) odd-3 method:

14a) if odd -3

14b) divide by 2

14c) if oddness changed from start -3

14d) negate (by modulo 7)=count upward to number divisible by 7=7n

15) -2(2y+y%4) or -2(2(y%7)+y%4) or 3y-2(y%4) or 3(y%7)-2(y%4) or -2(2b-a+y%4) or 2a+3b-2(y%4)

16) Zeller’s congruence method:

Y+C=(y+y/4)+(A+cc/4–2cc) ( Gregorian calendar dates ) or

Y+C=A+year+year/4+year/400–year/100 ( Gregorian calendar dates ) and

Y+C=(y+y/4)+(26+A-cc) ( Julian calendar dates ) or

Y+C=5+A+year+year/4 ( Julian calendar dates )

17) Y+C=A+4(year%100)-(year%400)-2(year%4) ( origin of method 15) )

———————————————————————————

number/code M for month m = number of odd days before that month ( standard monthly positive method )

number of odd days for months are 303 232 332 323 ( subtract 28 from month length in days )

( counting only for non-leap year )

So for Jan=0 ( because 0 days )

for Feb=Jan+odd_days_in_Jan=0+3=3

for Mar=Feb+odd_days_in_Feb=3+0=3

for Apr=Mar+odd_days_in_Mar=3+3=6

for May=Apr+odd_days_in_Apr=6+2=8=8–7=1 ( by modulo 7 )

for Jun=May+odd_days_in_May=1+3=4

for Jul=Jun+odd_days_in_Jun=4+2=6

for Aug=Jul+odd_days_in_Jul=6+3=9=9–7=2 ( by modulo 7 )

for Sep=Aug+odd_days_in_Aug=2+3=5

for Oct=Sep+odd_days_in_Sep=5+2=7=7–7=0( by modulo 7 )

for Nov=Oct+odd_days_in_Oct=0+3=3

for Dec=Nov+odd_days_in_Nov=3+2=5

so month table M=033 614 625 035 ( standard method )=M0

——————————————————————————————————————

Doomsday method has values M=3-M0-L ( negative monthly method )

(Leap year correction included in the number for month M)

——————————————————————————

L=leap year correction=-1 for Jan/Feb of leap year else 0

——————————————————————————

proof: 3-M0-L= 3–0-(0/-1), 3–3-(0/-1), 3–3 3–6,3–1,3–4, 3–6,3–2,3–5, 3–0,3–3,3–5

=3/4, 0/1, 0, -3,2,-1, -3,1,-2, 3,0,-2

=3/4, 28/29, 0/14, 4,9,6, 11,8,5, 10,7,12

which is the same because differences are ±7n

——————————————————————————————————————-

example 2053-June-30th : ( Doomsday method ) :

20xx—->2–2*(20%4)=2–2*(20–5*4)=2–2*(20–20)=2–2*0=2–0=2

June 6/6 => 6

Day in month=30=30–7n=30–28=2

————————————————-

year in century=53:

1) (5*53)/4=265/4=66

2) 53+53/4=53+13=66

3) 53%7+53/4=(53–49)+13=4+13=17

4) 48/12+(53–48)+(53–48)/4=4+5+5/4=9+1=10

5) =1-(53–1)/2=1–52/2=1–26=-25

6) -(53+11)/2=-64/2=-32 or

6a) 53+11=64

6b) 64/2=32

6d) from 32 to 35=3

7) 2*5+3+(G=4 for g=13)=17

8) 2*5+3+3+3/4+1=17

9) 2*5-(6+6/4)=10–7=3 because 9–3=6

10) 3–5+floor((3–2*5)/4)=-2+floor(-7/4)=-2–8/4=-2–2=-4

11) floor((5*3–6*5)/4)=floor((15–30)/4)=floor(-15/4)=-16/4=-4

12) (53–51)+1=3

13a) 53–30+2=25

13b) 25–22=3

14a) 53-3=50

14b) 50/2=25

14d) -25

15) -2(2(y%7)+y%4)=-2(2(53–49)+(53–52))=-2(2*4+1)=-2(8+1)=-2*9=-18

or -2(2b-a+y%4)=-2(2*3–5+(53–52))=-2(1+1)=-2*2=-4

or 2a+3b-2(y%4)=2*5+3*3-2(53-52)=10+9-2*1=19-2=17

(All methods give same result because differences are ±7n)

16) Y+C=(53+53/4)+(A+20/4–2*20)=66+A+5–40=A+31 or

Y+C=A+2053+2053/4+2053/400–2053/100=A+2053+513+5–20=A+2551

( difference is 2551–31=2520=7*360 )

17) Y+C=A+4*53-53-2*(53-52)=A+3*53-2*1=A+159-2=A+157

———————————————————————————————

So 2053.June 30th:

W=2+3+2–6=1=Monday (Doomsday method)

W=6+3+2+4=15=15–14=1=Monday (Standard method)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment