Astronomy Answers: Julian Day Number

Astronomy Answers
Julian Day Number


[Astronomy Answers] [Dictionary] [AnswerBook] [Universe Family Tree] [Science] [Starry Sky] [Planet Positions] [Calculate] [Colophon]

1. Derivation of the General Algorithms ... 1.1. Notation ... 1.2. Large Intermediate Results ... 1.3. The Simple Calendar ... 1.3.1. From Month and Day Number Within the Month to Running Day Number ... 1.3.2. Month Length ... 1.3.3. Shifting the Pattern of Months ... 1.3.4. From Running Day Number to Month and Day Number Within the Month ... 1.4. With Whole Numbers Only ... 1.5. Very Unequal Months ... 1.6. Many Kinds of Unequal Months ... 1.7. Very Unequal Month Lengths ... 1.8. Month Lengths Without Internal Patterns ... 1.9. Combinations of straight lines ... 1.9.1. Flat combination ... 1.9.2. Stepped combination ... 1.10. Simultaneous Cycles ... 1.10.1. From Running Day Number to Date ... 1.10.2. From Date to Running Day Number ... 1.10.3. More than Two Periods ... 1.10.4. To One Solution ... 1.10.4.1. The last one at or before ... 1.10.4.2. The first at or after ... 1.10.4.3. The last one before ... 1.10.4.4. The first one after ... 1.11. Summary ... 2. Derivation for Specific Calendars ... 2.1. A Lunar Calendar With Many Fixed Month Lengths ... 2.1.1. From Lunar Calendar to CJDN ... 2.1.2. From CJDN to Lunar Calendar

\(\def\|{&}\DeclareMathOperator{\D}{\bigtriangleup\!} \DeclareMathOperator{\d}{\text{d}\!}\)

\( \DeclareMathOperator{\trunc}{trunc} \DeclareMathOperator{\Div}{div} \DeclareMathOperator{\gcd}{gcd} \DeclareMathOperator{\lcm}{lcm} \DeclareMathOperator{\bdom}{dom} \def\dfloor#1{\left\lfloor #1 \right\rfloor} \def\dceil#1{\left\lceil #1 \right\rceil} \def\dparen#1{\left( #1 \right)} \def\dmod#1#2{\left\lfloor #1 \right\rceil_{#2}} \def\ddom#1#2{\left\lceil #1 \right\rfloor_{#2}} \def\dabs#1{\left| #1 \right|} \def\dfloorratio#1#2{\dfloor{\dfrac{#1}{#2}}} \def\dceilratio#1#2{\dceil{\dfrac{#1}{#2}}} \def\mod1ratio#1#2{\left\lfloor \dfrac{#1}{#2} \right\rceil_1} \def\dom1ratio#1#2{\left\lceil \dfrac{#1}{#2} \right\rfloor_1} \def\eqvide#1{\qquad\text{(vide \eqref{#1})}} \def\eqavide#1{\|\text{(vide \eqref{#1})}} \)

1. Derivation of the General Algorithms

We can derive many calendar formulas by finding straight lines that, with rounding, yield the correct results.

1.1. Notation

We use special notation for some things.

A few useful relationships (for \( p \gt 0 \)):

  1. \begin{equation} \dceil{\dceil{x}} = \dfloor{\dceil{x}} = \dceil{x}\end{equation}
  2. \begin{equation} \dfloor{\dfloor{x}} = \dceil{\dfloor{x}} = \dfloor{x} \end{equation}
  3. \begin{equation} \dmod{\dmod{x}{p}}{p} = \ddom{\dmod{x}{p}}{p} = \dmod{x}{p} \label{eq:modmod} \end{equation}
  4. \begin{equation} \ddom{\ddom{x}{p}}{p} = \dmod{\ddom{x}{p}}{p} = \ddom{x}{p} \label{eq:domdom} \end{equation}
  5. \begin{equation} \dmod{x ± y}{p} = \dmod{\dmod{x}{p} ± \dmod{y}{p}}{p} \label{eq:dmod2} \end{equation}
  6. \begin{equation} \ddom{x ± y}{p} = \ddom{\ddom{x}{p} ± \ddom{y}{p}}{p} \label{eq:ddom2} \end{equation}
  7. \begin{equation} \dceil{x} − \dfloor{x} = C(x⊥1) \label{eq:ceilfloor} \end{equation}
  8. \begin{equation} \dmod{x}{p} + \ddom{x}{p} = pC(x⊥p) \end{equation}
  9. \begin{equation} \dfloor{−x} = −\dceil{x} = −\dfloor{x} − C(x⊥1) \label{eq:minusfloor} \end{equation}
  10. \begin{equation} \dceil{−x} = −\dfloor{x} = −\dceil{x} + C(x⊥1) \label{eq:minusceil} \end{equation}
  11. \begin{equation} \dmod{−x}{p} = \ddom{x}{p} = −\dmod{x}{p} + pC(x⊥p) \label{eq:minusmod} \end{equation}
  12. \begin{equation} \ddom{−x}{p} = \dmod{x}{p} = −\ddom{x}{p} + pC(x⊥p) \label{eq:minusdom} \end{equation}
  13. \begin{equation} \dfloor{x + y} = \dfloor{x} + \dfloor{y} + C\dparen{\dmod{x}{1} + \dmod{y}{1} ≥ 1} \label{eq:splitfloor} \end{equation}
  14. \begin{equation} \dfloor{x − y} = \dfloor{x} − \dfloor{y} − C\dparen{\dmod{x}{1} − \dmod{y}{1} \lt 0} \label{eq:floorm} \end{equation}
  15. \begin{equation} \dmod{x + y}{p} = \dmod{x}{p} + \dmod{y}{p} − pC\dparen{\dmod{x}{p} + \dmod{y}{p} ≥ p} \label{eq:modp} \end{equation}
  16. \begin{equation} \dmod{x − y}{p} = \dmod{x}{p} − \dmod{y}{p} + pC\dparen{\dmod{x}{p} − \dmod{y}{p} \lt 0} \end{equation}
  17. \begin{equation} \dceil{x + y} = \dceil{x} + \dceil{y} − C\dparen{\dmod{x}{1} + \dmod{y}{1} ≥ 1} \end{equation}
  18. \begin{equation} \dceil{x − y} = \dceil{x} − \dceil{y} + C\dparen{\dmod{x}{1} − \dmod{y}{1} \lt 0} \end{equation}
  19. \begin{equation} \ddom{x + y}{p} = \ddom{x}{p} + \ddom{y}{p} − pC\dparen{\ddom{x}{p} + \ddom{y}{p} ≥ p} \label{eq:domp} \end{equation}
  20. \begin{equation} \ddom{x − y}{p} = \ddom{x}{p} − \ddom{y}{p} + pC\dparen{\ddom{x}{p} − \ddom{y}{p} \lt 0} \label{eq:domm} \end{equation}
  21. \begin{equation} n\dmod{x}{1} − \dmod{nx}{1} = \dfloor{n\dmod{x}{1}} \qquad{(n ∥ 1)} \label{eq:dmoddiff1} \end{equation}

  22. \begin{equation} x − y − 1 \lt \dfloor{x} − \dfloor{y} \lt x − y + 1 \label{eq:floordiffrange} \end{equation}
  23. \begin{equation} \dceil{x − y} − 1 \le \dfloor{x} − \dfloor{y} \le \dfloor{x − y} + 1 \label{eq:floordiffrange2} \end{equation}
  24. \begin{equation} x − y − 1 \lt \dceil{x} − \dceil{y} \lt x − y + 1 \label{eq:ceildiffrange} \end{equation}
  25. \begin{equation} \dceil{x − y} − 1 \le \dceil{x} − \dceil{y} \le \dfloor{x − y} + 1 \label{eq:ceildiffrange2} \end{equation}
  26. \begin{equation} \dceilratio{x}{y} = \dfloorratio{x − 1}{y} + 1 \qquad (x, y ∥ 1) \label{eq:ceil2floor} \end{equation}
  27. \begin{equation} \ddom{x}{y} = y − 1 − \dmod{x − 1}{y} \qquad (x, y ∥ 1) \label{eq:ddom2dmod} \end{equation}
  28. \begin{equation} \dfloor{\dmod{x}{y}} = \dmod{\dfloor{x}}{y} \qquad (y ∥ 1) \label{eq:modfloor} \end{equation}

And their proofs:

  1. \( \dceil{\dceil{x}} = \dfloor{\dceil{x}} = \dceil{x}\) because \( \dceil{x} \) is a whole number.
  2. \( \dfloor{\dfloor{x}} = \dceil{\dfloor{x}} = \dfloor{x} \) because \( \dfloor{x} \) is a whole number.
  3. \( \dmod{\dmod{x}{p}}{p} = \ddom{\dmod{x}{p}}{p} = \dmod{x}{p} \) because \( 0 \le \dmod{x}{p} \lt p \).
  4. \( \ddom{\ddom{x}{p}}{p} = \dmod{\ddom{x}{p}}{p} = \ddom{x}{p} \) because \( 0 \le \ddom{x}{p} \lt p \).
  5. \( \dmod{x ± y}{p} = \dmod{\dmod{x}{p} ± \dmod{y}{p}}{p} \) because

    \begin{align} \dmod{x ± y}{p} \| = p\dmod{\dfrac{x ± y}{p}}{1} \notag \eqavide{eq:mod1top} \\ \| = p\dmod{\dfrac{p\dfloorratio{x}{p} + \dmod{x}{p} ± \dparen{p\dfloorratio{y}{p} + \dmod{y}{p}}}{p}}{1} \eqavide{eq:mod} \notag \\ \| = p\dmod{\dfloorratio{x}{p} ± \dfloorratio{y}{p} + \dfrac{\dmod{x}{p} ± \dmod{y}{p}}{p}}{1} \notag \\ \| = p\dmod{\dfrac{\dmod{x}{p} ± \dmod{y}{p}}{p}}{1} \eqavide{eq:modn} \notag \\ \| = \dmod{\dmod{x}{p} ± \dmod{y}{p}}{p} \eqavide{eq:mod1top} \end{align}

  6. \( \ddom{x ± y}{p} = \ddom{\ddom{x}{p} ± \ddom{y}{p}}{p} \) because

    \begin{align} \ddom{x ± y}{p} \| = p\ddom{\dfrac{x ± y}{p}}{1} \notag \eqavide{eq:dom1top} \\ \| = p\ddom{\dfrac{p\dceilratio{x}{p} − \ddom{x}{p} ± \dparen{p\dceilratio{y}{p} − \ddom{y}{p}}}{p}}{1} \eqavide{eq:dom} \notag \\ \| = p\ddom{\dceilratio{x}{p} ± \dceilratio{y}{p} − \dparen{\dfrac{\ddom{x}{p} ± \ddom{y}{p}}{p}}}{1} \notag \\ \| = p\ddom{−\dfrac{\ddom{x}{p} ± \ddom{y}{p}}{p}}{1} \eqavide{eq:domn} \label{eq:xpyp} \end{align}

    so also

    \begin{align} \ddom{\ddom{x}{p} ± \ddom{y}{p}}{p} \| = p\ddom{−\dfrac{\ddom{\ddom{x}{p}}{p} ± \ddom{\ddom{y}{p}}{p}}{p}}{1} \eqavide{eq:xpyp} \notag \\ \| = p\ddom{−\dfrac{\ddom{x}{p} ± \ddom{y}{p}}{p}}{1} \eqavide{eq:domdom} \notag \\ \| = \ddom{x ± y}{p} \eqavide{eq:xpyp} \end{align}

  7. \( \dceil{x} − \dfloor{x} = C(x⊥1) \) because when \( x \) is a whole number (so \( x∥1 \)) then \( \dceil{x} \) and \( \dfloor{x} \) are equal to \( x \), and otherwise the next higher whole nuber (\( \dceil{x} \)) is one greater than the next lower whole number (\( \dfloor{x} \)).

  8. \( \dmod{x}{p} + \ddom{x}{p} = pC(x⊥p) \) because

    \begin{align} \dmod{x}{p} + \ddom{x}{p} \| = x − p\dfloorratio{x}{p} + p\dceilratio{x}{p} − x \| \text{(vide \eqref{eq:mod}, \eqref{eq:dom})} \notag \\ \| = p \dparen{\dceilratio{x}{p} − \dfloorratio{x}{p}} \notag \\ \| = p C\dparen{\dfrac{x}{p} ⊥ 1} \notag \eqavide{eq:ceilfloor} \\ \| = p C\dparen{x ⊥ p} \end{align}

  9. For \( \dfloor{−x} = −\dceil{x} = −\dfloor{x} − C(x⊥1) \) the second \( = \) follows from Equation \eqref{eq:ceilfloor}.

    We prove the first \( = \) as follows.

    \begin{align} \dfloor{−x} \| = \dfloor{−\dfloor{x} − \dmod{x}{1}} \eqavide{eq:mod} \notag \\ \| = −\dfloor{x} + \dfloor{−\dmod{x}{1}} \\ \dceil{x} \| = \dceil{\dfloor{x} + \ddom{x}{1}} \eqavide{eq:dom} \notag \\ \| = \dfloor{x} + \dceil{\ddom{x}{1}} \\ \dfloor{−x} + \dceil{x} \| = \dfloor{−\dmod{x}{1}} + \dceil{\ddom{x}{1}} \end{align}

    If \( x \) is a whole number then \( \dmod{x}{1} = \ddom{x}{1} = 0 \) so \( \dfloor{−x} + \dceil{x} = 0 \).

    And if \( x \) is not a whole number then

    \begin{equation} 0 \lt \dmod{x}{1} \lt 1 ⇒ \dceil{\dmod{x}{1}} = 1 \end{equation}

    and also

    \begin{equation} −1 \lt −\dmod{x}{1} \lt 0 ⇒ \dfloor{−\dmod{x}{1}} = −1 \end{equation}

    so then, too, \( \dfloor{−x} + \dceil{x} = \dceil{\dmod{x}{1}} + \dfloor{−\dmod{x}{1}} = 0 \) and so \( \dfloor{−x} = −\dceil{x} \).

  10. For \( \dceil{−x} = −\dfloor{x} = −\dceil{x} + C(x⊥1) \), the second \( = \) follows from Equation \eqref{eq:ceilfloor}.

    The first \( = \) follows from Equation \eqref{eq:minusfloor} if you replace every \( x \) by \( −x \).

  11. For \( \dmod{−x}{p} = \ddom{x}{p} = −\dmod{x}{p} + pC(x⊥p) \) the first \( = \) follows from

    \begin{align} \dmod{−x}{p} \| = −x − p\dfloorratio{−x}{p} \eqavide{eq:mod} \notag \\ \| = p\dceilratio{x}{p} − x \eqavide{eq:minusfloor} \notag \\ \| = \ddom{x}{p} \eqavide{eq:dom} \end{align}

    and the second \( = \) from

    \begin{align} \dmod{x}{p} \| = x − p\dfloorratio{x}{p} \notag \eqavide{eq:mod} \\ \| = x − p\dparen{\dceilratio{x}{p} − C\dparen{\dfrac{x}{p} ⊥ 1}} \notag \eqavide{eq:ceilfloor} \\ \| = x − p\dceilratio{x}{p} + pC\dparen{x ⊥ p} \notag \\ \| = −\ddom{x}{p} + pC\dparen{x ⊥ p} \eqavide{eq:dom} \end{align}

  12. \( \ddom{−x}{p} = \dmod{x}{p} = −\ddom{x}{p} + pC(x⊥p) \) follows from the previous one if you replace every \( x \) with \( −x \).

  13. \( \dfloor{x + y} = \dfloor{x} + \dfloor{y} + C\dparen{\dmod{x}{1} + \dmod{y}{1} ≥ 1} \) because

    \begin{align} \dfloor{x ± y} \| = x ± y − \dmod{x ± y}{1} \eqavide{eq:mod} \notag \\ \| = \dfloor{x} + \dmod{x}{1} ± \dparen{\dfloor{y} + \dmod{y}{1}} − \dmod{x ± y}{1} \eqavide{eq:mod} \notag \\ \| = \dfloor{x} ± \dfloor{y} + \dmod{x}{1} ± \dmod{y}{1} − \dmod{x ± y}{1} \notag \\ \| = \dfloor{x} ± \dfloor{y} + \dmod{x}{1} ± \dmod{y}{1} − \dmod{\dmod{x}{1} ± \dmod{y}{1}}{1} \eqavide{eq:dmod2} \label{eq:floorpm} \end{align}

    We have

    \begin{align} \dmod{\dmod{x}{1} + \dmod{y}{1}}{1} \| = \dmod{x}{1} + \dmod{y}{1} − C\dparen{\dmod{x}{1} + \dmod{y}{1} \ge 1} \\ \dmod{\dmod{x}{1} − \dmod{y}{1}}{1} \| = \dmod{x}{1} − \dmod{y}{1} + C\dparen{\dmod{x}{1} − \dmod{y}{1} \lt 0} \label{eq:mod1m} \end{align}

    because \( 0 \le \dmod{•}{1} \lt 1 \). So

    \begin{equation} \dfloor{x + y} = \dfloor{x} + \dfloor{y} + C\dparen{\dmod{x}{1} + \dmod{y}{1} \ge 1} \end{equation}

  14. \( \dfloor{x − y} = \dfloor{x} − \dfloor{y} + C\dparen{\dmod{x}{1} − \dmod{y}{1} \lt 0} \) follows from Eqs. \eqref{eq:floorpm} and \eqref{eq:mod1m}.
  15. \( \dmod{x + y}{p} = \dmod{x}{p} + \dmod{y}{p} − pC\dparen{\dmod{x}{p} + \dmod{y}{p} ≥ p} \) follows from

    \begin{align} \dmod{x ± y}{p} \| = x ± y − p\dfloorratio{x ± y}{p} \eqavide{eq:mod} \notag \\ \| = p\dfloorratio{x}{p} + \dmod{x}{p} ± \dparen{p\dfloorratio{y}{p} + \dmod{y}{p}} \notag \\ \| − p\dfloorratio{p\dfloorratio{x}{p} + \dmod{x}{p} ± \dparen{p\dfloorratio{y}{p} + \dmod{y}{p}}}{p} \notag \eqavide{eq:mod} \\ \| = p\dfloorratio{x}{p} + \dmod{x}{p} ± p\dfloorratio{y}{p} ± \dmod{y}{p} \notag \\ \| − p\dfloorratio{x}{p} ∓ p\dfloorratio{y}{p} − p\dfloorratio{\dmod{x}{p} ± \dmod{y}{p}}{p} \notag \\ \| = \dmod{x}{p} ± \dmod{y}{p} − p\dfloorratio{\dmod{x}{p} ± \dmod{y}{p}}{p} \label{eq:i15a} \end{align}

    We have

    \begin{align} \dfloorratio{\dmod{x}{p} + \dmod{y}{p}}{p} \| = C\dparen{\dmod{x}{p} + \dmod{y}{p} \ge p} \label{eq:i15c} \\ \dfloorratio{\dmod{x}{p} − \dmod{y}{p}}{p} \| = −C\dparen{\dmod{x}{p} − \dmod{y}{p} \lt 0} \label{eq:i15b} \end{align}

    because \( 0 \le \dmod{•}{p} \lt p \), so

    \begin{equation} \dmod{x}{p} + \dmod{y}{p} − p\dfloorratio{\dmod{x}{p} + \dmod{y}{p}}{p} = \dmod{x}{p} + \dmod{y}{p} − pC\dparen{\dmod{x}{p} + \dmod{y}{p} \ge p} \end{equation}

    which completes the proof.

  16. \( \dmod{x − y}{p} = \dmod{x}{p} − \dmod{y}{p} + pC\dparen{\dmod{x}{p} − \dmod{y}{p} \lt 0} \) follows from Eqs. \eqref{eq:i15a} and \eqref{eq:i15b}.
  17. \( \dceil{x + y} = \dceil{x} + \dceil{y} − C\dparen{\dmod{x}{1} + \dmod{y}{1} ≥ 1} \) because

    \begin{align} \dceil{x ± y} \| = x ± y + \ddom{x ± y}{p} \notag \eqavide{eq:dom} \\ \| = \dceil{x} − \ddom{x}{1} ± \dparen{\dceil{y} − \ddom{y}{1}} + \ddom{x ± y}{p} \notag \eqavide{eq:dom} \\ \| = \dceil{x} ± \dceil{y} − \ddom{x}{1} ∓ \ddom{y}{1} + \ddom{x ± y}{p} \label{eq:i17} \end{align}

    That leads to

    \begin{align} \dceil{x + y} \| = \dceil{x} + \dceil{y} − \ddom{x}{1} − \ddom{y}{1} + \ddom{x + y}{p} \notag \\ \| = \dceil{x} + \dceil{y} − \ddom{x}{1} − \ddom{y}{1} + \ddom{x}{1} + \ddom{y}{1} − C\dparen{\ddom{x}{1} + \ddom{y}{1} ≥ 1} \eqavide{eq:domp} \notag \\ \| = \dceil{x} + \dceil{y} − C\dparen{\ddom{x}{1} + \ddom{y}{1} ≥ 1} \end{align}

  18. \( \dceil{x − y} = \dceil{x} − \dceil{y} + C\dparen{\dmod{x}{1} − \dmod{y}{1} \lt 0} \) because

    \begin{align} \dceil{x − y} \| = \dceil{x} − \dceil{y} − \ddom{x}{1} + \ddom{y}{1} + \ddom{x − y}{p} \notag \eqavide{eq:i17} \\ \| = \dceil{x} − \dceil{y} − \ddom{x}{1} + \ddom{y}{1} + \ddom{x}{1} − \ddom{y}{1} + C\dparen{\ddom{x}{1} − \ddom{y}{1} \lt 0} \eqavide{eq:modp} \notag \\ \| = \dceil{x} − \dceil{y} + C\dparen{\ddom{x}{1} − \ddom{y}{1} \lt 0} \end{align}

  19. \( \ddom{x + y}{p} = \ddom{x}{p} + \ddom{y}{p} − pC\dparen{\ddom{x}{p} + \ddom{y}{p} ≥ p} \) because

    \begin{align} \ddom{x ± y}{p} = \| p\dceilratio{x ± y}{p} − \dparen{x ± y} \notag \eqavide{eq:dom} \\ = \| p\dceilratio{p\dceilratio{x}{p} − \ddom{x}{p} ± \dparen{p\dceilratio{y}{p} − \ddom{y}{p}}}{p} \notag \\ \| − \dparen{p\dceilratio{x}{p} − \ddom{x}{p} ± \dparen{p\dceilratio{y}{p} − \ddom{y}{p}}} \notag \eqavide{eq:dom} \\ = \| p\dceilratio{x}{p} ± p\dceilratio{y}{p} + p\dceilratio{−\ddom{x}{p} ∓ \ddom{y}{p}}{p} \notag \\ \| − \dparen{p\dceilratio{x}{p} ± p\dceilratio{y}{p} − \dparen{\ddom{x}{p} ± \ddom{y}{p}}} \notag \\ = \| \ddom{x}{p} ± \ddom{y}{p} + p\dceilratio{−\ddom{x}{p} ∓ \ddom{y}{p}}{p} \label{eq:i19a} \end{align}

    We have

    \begin{align} \dceilratio{−\ddom{x}{p} − \ddom{y}{p}}{p} \| = −C\dparen{\ddom{x}{p} + \ddom{y}{p} \ge p} \\ \dceilratio{−\ddom{x}{p} + \ddom{y}{p}}{p} \| = C\dparen{\ddom{x}{p} − \ddom{y}{p} \lt 0} \label{eq:i19b} \end{align}

    because \( 0 \le \ddom{•}{p} \lt 1 \), so

    \begin{equation} \ddom{x}{p} + \ddom{y}{p} + p\dceilratio{\ddom{x}{p} + \ddom{y}{p}}{p} = \ddom{x}{p} + \ddom{y}{p} − pC\dparen{\ddom{x}{p} + \ddom{y}{p} \ge p} \end{equation}

    which completes the proof.

  20. \( \ddom{x − y}{p} = \ddom{x}{p} − \ddom{y}{p} + pC\dparen{\ddom{x}{p} − \ddom{y}{p} \lt 0} \) follows from Eqs. \eqref{eq:i19a} and \eqref{eq:i19b}.

  21. \begin{eqnarray} n\dmod{x}{1} − \dmod{nx}{1} \| = \| n\dmod{x}{1} − \dmod{n\dfloor{x} + n\dmod{x}{1}}{1} \notag \\ \| = \| n\dmod{x}{1} − \dmod{n\dmod{x}{1}}{1} \qquad{(n ∥ 1)} \eqavide{eq:modp} \notag \\ \| = \| \dfloor{n\dmod{x}{1}} \eqavide{eq:mod} \end{eqnarray}
  22. \begin{align} \dfloor{x} − \dfloor{y} \| = \dparen{x − \dmod{x}{1}} − \dparen{y − \dmod{y}{1}} \notag \\ \| = x − y + \dmod{y}{1} − \dmod{x}{1} \end{align}

    Then

    \begin{equation} x − y − 1 \lt \dfloor{x} − \dfloor{y} \lt x − y + 1 \end{equation}

    because

    \begin{equation} 0 \le \dmod{•}{1} \lt 1 \end{equation}

  23. \begin{equation} \dceil{x − y} − 1 \le \dfloor{x} − \dfloor{y} \le \dfloor{x − y} + 1 \end{equation}

    because

    \begin{align} x − y − 1 \lt \dfloor{x} − \dfloor{y} \lt x − y + 1 \eqavide{eq:floordiffrange} \\ x − y \le \dceil{x − y} \| \quad (\dfloor{x} − \dfloor{y} ∥ 1) \end{align}

  24. \begin{align} \dceil{x} − \dceil{y} \| = \dparen{x + \ddom{x}{1}} − \dparen{y + \ddom{y}{1}} \notag \\ \| = x − y + \ddom{x}{1} − \ddom{y}{1} \end{align}

    Then

    \begin{equation} x − y − 1 \lt \dceil{x} − \dceil{y} \lt x − y + 1 \end{equation}

    because

    \begin{equation} 0 \le \ddom{•}{1} \lt 1 \end{equation}

  25. \begin{equation} \dceil{x − y} − 1 \le \dceil{x} − \dceil{y} \le \dfloor{x − y} + 1 \end{equation}

    because

    \begin{align} x − y − 1 \lt \dceil{x} − \dceil{y} \lt x − y + 1 \eqavide{eq:ceildiffrange} \\ x − y \le \dceil{x − y} \| \quad (\dfloor{x} − \dfloor{y} ∥ 1) \end{align}

  26. If \( y = 1 \):

    \begin{align} \dceilratio{x}{y} \| = \dceilratio{x}{1} = x \| \qquad (x ∥ 1) \\ \| = (x − 1) + 1 = \dfloorratio{x − 1}{1} + 1 = \dfloorratio{x − 1}{y} + 1 \end{align}

    If \( y \gt 1 \):

    \begin{align} \dceilratio{x}{y} \| = \dfloorratio{x}{y} + C(x⊥y) \eqavide{eq:ceilfloor} \notag \\ \| = \dfloorratio{x}{y} + C\dparen{\dmod{x}{y} \ge 1} \| \qquad (x, y ∥ 1) \notag \\ \| = \dfloorratio{x}{y} + C\dparen{\dmod{\dfrac{x}{y}}{1} \ge \dfrac{1}{y}} \notag \\ \| = \dfloorratio{x}{y} + C\dparen{\dmod{\dfrac{x}{y}}{1} \ge \dmod{\dfrac{1}{y}}{1}} \| (y \gt 1) \notag \\ \| = \dfloorratio{x}{y} − \dfloorratio{1}{y} + C\dparen{\dmod{\dfrac{x}{y}}{1} − \dmod{\dfrac{1}{y}}{1} \ge 0} \| (y \gt 1) \notag \\ \| = \dfloorratio{x}{y} − \dfloorratio{1}{y} + 1 − C\dparen{\dmod{\dfrac{x}{y}}{1} − \dmod{\dfrac{1}{y}}{1} \lt 0} \notag \\ \| = \dfloorratio{x − 1}{y} + 1 \eqavide{eq:floorm} \end{align}

  27. If \( y = 1 \):

    \begin{equation} \ddom{x}{y} = \ddom{x}{1} = 0 = y − 1 − \ddom{x − 1}{y} \qquad (x ∥ 1) \end{equation}

    If \( y \gt 1 \):

    \begin{align} \ddom{x}{y} \| = y\ddom{\dfrac{x}{y}}{1} \notag \\ \| = y\dparen{\dceil{\dfrac{x}{y}} − \dfrac{x}{y}} \eqavide{eq:dom} \notag \\ \| = y\dparen{\dfloorratio{x − 1}{y} + 1 − \dfrac{x}{y}} \eqavide{eq:ceil2floor} \notag \\ \| = y\dparen{\dfrac{x − 1}{y} − \dmod{\dfrac{x − 1}{y}}{1} + 1 − \dfrac{x}{y}} \eqavide{eq:mod} \notag \\ \| = x − 1 − y\dmod{\dfrac{x − 1}{y}}{1} + y − x \notag \\ \| = y − 1 − \dmod{x − 1}{y} \end{align}

  28. Let

    \begin{align} \{ k, α \} \| = \Div(x, y) \\ \{ m, β \} \| = \Div(α, 1) \end{align}

    so

    \begin{equation} x = ky + α = ky + m + β \end{equation}

    where

    \begin{align} k, m, y \| ∥ 1 \\ 0 \| ≤ m ≤ y − 1 \\ 0 \| ≤ β \lt 1 \end{align}

    then

    \begin{align} \dfloor{\dmod{x}{y}} \| = \dfloor{\dmod{ky + m + β}{y}} \notag \\ \| = \dfloor{\dmod{m + β}{y}} \notag \\ \| = \dfloor{m + β} \| (0 ≤ m + β \lt y) \notag \\ \| = m \end{align}

    and also

    \begin{align} \dmod{\dfloor{x}}{y} \| = \dmod{\dfloor{ky + m + β}}{y} \notag \\ \| = \dmod{ky + m}{y} = m \end{align}

    so

    \begin{equation} \dfloor{\dmod{x}{y}} = \dmod{\dfloor{x}}{y} \end{equation}

1.2. Large Intermediate Results

Many of the calendar calculations that are described below are of the type

\begin{align} y \| = \dfloorratio{fx + t}{d} \label{eq:template} \\ e \| = \dmod{fx + t}{d} = (fx + t) \bmod d \label{eq:template-e} \end{align}

i.e.,

\[ \{y, e\} = \Div(fx + t, d) \]

with

\begin{equation} fx + t = dy + e \end{equation}

where \( f \), \( t \), \( d \) are fixed whole numbers with \( f, d \gt 1 \) and \( 0 ≤ e \lt d \) and \( y \), \( x \) are variable but whole numbers. The intermediate result \( fx + t \) is then usually much greater in magnitude than \( x \) and than the end result. This can give trouble on calculators and in computer programs where whole numbers cannot exceed a certain size that we'll refer to as \( w \). (Floating-point numbers like 1.234 × 1020 can be much greater, but have limited accuracy, and then you have to worry about round-off errors.)

Intermediate results greater than \( w \) give trouble, so to avoid trouble we'd need roughly \( |fx| ≤ w \), so \( |x| ≤ w/f \), which is much less than \( w \) itself.

If \( |t| ≥ d \), then we can rewrite equation \eqref{eq:template}ff to

\begin{align} fx + t \| = fx + \dfloorratio{t}{d} d + \dmod{t}{d} = dy + e \eqavide{eq:mod} \\ y \| = \dfloorratio{fx + t}{d} = \dfloorratio{t}{d} + \dfloorratio{fx + \dmod{t}{d}}{d} \\ e \| = \dmod{fx + t}{d} = \dmod{fx + \dmod{t}{d}}{d} \eqavide{eq:modmod} \end{align}

where the second term of the last two equations has the same form as equation \eqref{eq:template}, if you rename \( \dmod{t}{d} \) to a new \( t \). Below, we assume that that has been done, which means that \( 0 ≤ t \lt d \).

If \( f \gt d \), then we can rewrite equation \eqref{eq:template}ff to

\begin{align} fx + t \| = \dparen{\dfloorratio{f}{d} d + \dmod{f}{d}} x + t = dy + e \eqavide{eq:mod} \\ y \| = \dfloorratio{f}{d} x + \dfloorratio{\dmod{f}{d} x + t}{d} \label{eq:template2} \\ e \| = \dmod{\dmod{f}{d} x + t}{d} \end{align}

Then \( |x| \) may be roughly as great as \( w/\dmod{f}{d} \) before we run into trouble.

Equations \eqref{eq:template2}ff again have the form of equations \eqref{eq:template}ff, if you rename \( \dmod{f}{d} \) to a new \( f \). Below, we assume that this has been done, so that \( f \lt d \).

If we could first do the division by \( d \) and then the multiplication by \( f \), then the intermediate results would stay smaller than \( x \) itself. We'd like to calculate something like \( f\dfloorratio{x}{d} \) which is roughly equal to \( y \), and then apply a correction to get the real \( y \).

We can rewrite equation \eqref{eq:template}ff to

\begin{align} fx + t \| = f\dparen{ \dfloorratio{x}{d} d + \dmod{x}{d}} + t \eqavide{eq:mod} \\ y \| = f\dfloorratio{x}{d} + \dfloorratio{f\dmod{x}{d} + t}{d} \label{eq:detour1} \\ e \| = \dmod{f\dmod{x}{d} + t}{d} \end{align}

Now the greatest intermediate result is no longer \( fx + t \), which can get as large as \( fw \), but \( f\dmod{x}{d} + t \), which cannot exceed \( fd \lt d^2 \).

Equations \eqref{eq:detour1}ff again have the form of equations \eqref{eq:template}ff, if you rename \( \dmod{x}{d} \) to a new \( x \).

For example, let \( d = 12345 \), \( f = 8432 \), \( t = 871 \), and \( w = 2^{31} − 1 = 2147483647 \), and suppose we want to calculate \( y \), \( e \) for \( x = 300000 \).

We use the detour of equation \eqref{eq:detour1}. We find

\begin{align*} \dfloorratio{x}{d} \| = \dfloorratio{300000}{12345} = 24 \\ \dmod{x}{d} \| = \dmod{300000}{12345} = 3720 \\ y = \| f\dfloorratio{x}{d} + \dfloorratio{f\dmod{x}{d} + t}{d} \\ \| = 8432×24 + \dfloorratio{8432×3720 + 871}{12345} \\ \| = 202368 + \dfloorratio{31367911}{12345} \\ \| = 202368 + 2540 = 204908 \\ e \| = \dmod{31367911}{12345} = 11611 \end{align*}

The greatest intermediate result is 31'367'911, which is much less than \( w \).

If we could have used arbitrarily large intermediate results, then we'd have found

\begin{align*} y \| = \dfloorratio{fx + t}{d} = \dfloorratio{8432×300000 + 871}{12345} = \dfloorratio{2529600871}{12345} = 204908 \\ e \| = \dmod{2529600871}{12345} = 11611 \end{align*}

which are the same results, but with a greatest intermediate result of 2'529'600'871 which is greater than \( w \).

Sometimes an intermediate result of (nearly) \( fd \) is still too great, because the product of two numbers can be much greater than \( w \) even if the two numbers are each much less than \( w \). If the product must remain less than \( w \), then the two numbers cannot both be greater than \( \sqrt{w} \), which is a lot less than \( w \) itself. For example, for 32-bit numbers, \( w = 2^{31} − 1 = 2147483647 \) and \( \sqrt{w} = 46341 \), so even if \( d \) and \( f \) are both just a bit greater than 46431, which isn't really very large, then \( fd \) is already too great to fit into a 32-bit number.

Suppose we pick a whole number \( P \gt 0 \) and then calculate one time

\begin{align} \{Q, R\} \| = \Div(fP, d) \label{eq:PQR} \\ Q \| = \dfloorratio{fP}{d} \\ R \| = \dmod{fP}{d} \end{align}

so

\begin{equation} fP = dQ + R \end{equation}

Then, for each desired \( x \), calculate

\begin{align} \{q, r\} \| = \Div(x, P) \\ q \| = \dfloorratio{x}{P} \\ r \| = \dmod{x}{P} \end{align}

so

\begin{equation} x = qP + r \end{equation}

and then

\begin{align} fx + t \| = fqP + fr + t = qdQ + qR + fr + t \\ y \| = \dfloorratio{qdQ + qR + fr + t}{d} = qQ + \dfloorratio{qR + fr + t}{d} \label{eq:detour2} \\ e \| = \dmod{qdQ + qR + fr + t}{d} = \dmod{qR + fr + t}{d} \end{align}

Now the greatest intermediate result is \( m ≡ qR + fr + t \). To stay out of trouble we need \( \dabs{m} ≤ w \), so (because \( r \lt P \))

\begin{eqnarray} \dabs{x}\frac{R}{P} + fP + t ≤ w \\ \dabs{x} ≤ X ≡ \dparen{w − fP − t}\dfrac{P}{R} \end{eqnarray}

We have \( f \lt d \) so \( f \) is not a multiple of \( d \) so \( R \gt 0 \).

We must have \( 0 \lt P \lt \dfrac{w − t}{f} \), otherwise certainly \( X ≤ 0 \).

For example, let \( d = 765433 \), \( f = 25920 \), \( t = 13835 \), \( w = 2^{31} − 1 = 2147483647 \). Then we need, to get \( X ≥ 0 \),

\[ P \lt \dfrac{w − t}{f} = \dfrac{2147483647 − 13835}{25920} = 82849.9 \]

Some examples:

\({P}\) \({Q}\) \({R}\) \({X}\) \({X/w}\)
1 0 25920 82848.9 0.000
2 0 51840 82847.9 0.000
3 0 77760 82846.9 0.000
29 0 751680 82820.9 0.000
30 1 12167 5.29307 × 10+06 0.002
31 1 38087 1.74723 × 10+06 0.001
59 1 763847 165754 0.000
60 2 24334 5.29115 × 10+06 0.002
943 31 714137 2.8034 × 10+06 0.001
944 31 740057 2.70805 × 10+06 0.001
945 32 544 3.68789 × 10+09 1.717
33783 1144 8 5.37071 × 10+12 2500.932
82849 2805 406515 4836.65 0.000
82850 2805 432435 −419.198 −0.000

We want to make the range of \( x \) as large as possible, so we seek \( P \) and \( R \) such that \( X \) becomes as great as possible, given \( w \), \( f \), \( t \). This means that \( R \) should preferably be small.

If \( P \) increases slowly (with steps of 1) then \( R = \dmod{fP}{d} \) varies strongly between 0 and \( d − 1 \), so the value of \( R \) is independent of the global magnitude of \( P \), and many different values of \( P \) are possible for the same smallest value of \( R \).

If \( R \) has the smallest possible value (greater than zero), then roughly what should the value of \( P \) be that yields the greatest value of \( X \)?

Then \( X \) is a quadratic function of \( P \). If \( P \) could have any value then the greatest possible value of \( X \) would occur for

\begin{equation} P = P_0 = \dfrac{w − t}{2f} \end{equation}

and be equal to

\begin{equation} \max(X) = \dfrac{\dparen{w − t}^2}{4Rf} = \dfrac{fP_0^2}{R} \end{equation}

To get \( \max(X) ≥ w \) we'd certainly need

\begin{equation} R ≤ \dfrac{w}{4f}\dparen{1−\dfrac{t}{w}}^2 \label{eq:maxR} \end{equation}

The least possible value that \( R \) can have is equal to the greatest common divisor \( \gcd(f, d) \) of \( f \) and \( d \).

\begin{equation} R = R_1 ≡ \gcd(f, d) \end{equation}

The Extended Algorithm of Euclid produces that value, and also numbers \( x \) and \( y \) such that

\begin{equation} fx + dy = R \end{equation}

The \( x \) and \( y \) that you get are in some sense the smallest, but the following values also work (with \( k \) an arbitrary whole number):

\begin{equation} f\dparen{x + k\dfrac{d}{R}} + d\dparen{y − k\dfrac{f}{R}} = R \end{equation}

Because \( R \) is a divisor of \( d \) and also of \( f \), the divisions by \( R \) in this formula always produce whole numbers.

The corresponding possible values of \( P \) and \( Q \) are then:

\begin{align} P \| = x + k\dfrac{d}{R} \\ Q \| = −y + k\dfrac{f}{R} \end{align}

for arbitrary whole \( k \) for which \( P, Q \gt 0 \). The smallest usable positive \( P \) and the corresponding value of \( Q \) are

\begin{align} P_1 \| = \dmod{x}{d/R_1} \\ Q_1 \| = \dmod{−y}{f/R_1} \end{align}

These \( P_1, Q_1, R_1 \) comply with equations \eqref{eq:PQR}ff, because

\begin{equation} \dfloorratio{fP_1}{d} = \dfloorratio{dQ_1 + R_1}{d} = Q_1 + \dfloorratio{R_1}{d} = Q_1 \end{equation}

because \( \dfloor{R_1/d} = 0 \), because \( R_1 = \gcd(f,d) \lt d \), because\( f \lt d \). And if \( Q_1 \) fits then \( R_1 \) fits, too.

To get \( X \gt 0 \) we certainly need \( P ≤ w/f \), so the greatest usable \( P \) is

\begin{equation} P_\text{max} = \dfrac{w}{f} − \dmod{\dfrac{w}{f} − P_1}{d/R_1} \end{equation}

The step size between successive values of \( P \) that fit with this \( R \) is \( d/R \) so if \( d/R \gt w/f \) (so \( fd/R \gt w \)) then there may be no acceptable value of \( P \) for this \( R \) at all. In that case \( P_\text{max} \lt 0 \).

For the same case as in the previous example we find with the Extended Algorithm of Euclid

\[ fx + dy = 25920×99902 − 765433×3383 = 1 \]

so

\begin{align*} x \| = 99902 \\ y \| = −3383 \\ R_1 \| = 1 \\ P_1 \| = \dmod{x}{d/R} = \dmod{99902}{765433} = 99902 \\ Q_1 \| = \dmod{−y}{f/R} = \dmod{3383}{25920} = 3383 \\ P_\text{max} \| = \dfrac{w}{f} − \dmod{\dfrac{w}{f} − P_1}{d/R_1} \\ \| = \dfrac{2147483647}{25920} − \dmod{\dfrac{2147483647}{25920} − 99902}{765433/1} \\ \| = 82850.4493441 − \dmod{82850.4493441 − 99902}{765433} \\ \| = 82850.4493441 − \dmod{−17051.5506559}{765433} \\ \| = 82850.4493441 − 748381.449344 = −665531 \end{align*}

so \( P_\text{max} \lt 0 \) so for this \( R \) we cannot find an acceptable value of \( P \) in this way.

If you cannot find an acceptable value of \( P \) for \( R = R_1 \), then try ever greater multiples of \( R_1 \) until you do find an acceptable \( P \). That is possible, because we have

\begin{equation} fP_1 − dQ_1 = R_1 \end{equation}

so also

\begin{equation} fnP_1 − dnQ_1 = nR_1 \end{equation}

for arbitrary \( n \).

But \( nP_1 \) must not be greater than \( w/f \) so \( n \) must not be greater than \( w/(fP_1) \) which often isn't very large.

In our example, \( n \) must not be greater than

\[ \dfrac{w}{fP_1} = \dfrac{2147483647}{25920×99902} = \dfrac{2147483647}{2589459840} = 0.829317224321 \]

which is even less than 1.

To yet find more smaller values of \( P \) we can make use of the modulus function. If we use for \( P \)

\begin{equation} P = \dmod{nP_1}{(d/R_1)} \end{equation}

then

\begin{align} \dfrac{nP_1R_1}{d} \| = n\dfrac{R_1}{f}\dfrac{dQ_1 + R_1}{d} \\ P \| = \dfrac{d}{R_1}\dmod{\dfrac{nP_1R_1}{d}}{1} \\ \dfrac{fP}{d} \| = \dfrac{f}{R_1}\dmod{\dfrac{nP_1R_1}{d}}{1} = \dfrac{f}{R_1}\dmod{n\dfrac{R_1}{f}\dfrac{dQ_1 + R_1}{d}}{1} \notag \\ \| = \dmod{nQ_1 + \dfrac{nR_1}{d}}{(f/R_1)} \\ Q \| = \dfloorratio{fP}{d} = \dfloor{\dmod{nQ_1 + \dfrac{nR_1}{d}}{(f/R_1)}} \notag \\ \| = \dmod{\dfloor{nQ_1 + \dfrac{nR_1}{d}}}{(f/R_1)} = \dmod{nQ_1 + \dfloor{\dfrac{nR_1}{d}}}{(f/R_1)} \eqvide{eq:modfloor} \\ R \| = fP − dQ \notag \\ \| = d\dmod{nQ_1 + \dfrac{nR_1}{d}}{(f/R_1)} − d\dmod{nQ_1 + \dfloor{\dfrac{nR_1}{d}}}{(f/R_1)} \notag \\ \| = d\dmod{nQ_1 + \dfloor{\dfrac{nR_1}{d}} + \dmod{\dfrac{nR_1}{d}}{1}}{(f/R_1)} − d\dmod{nQ_1 + \dfloor{\dfrac{nR_1}{d}}}{(f/R_1)} \notag \\ \| = d\dmod{nQ_1 + \dfloor{\dfrac{nR_1}{d}}}{(f/R_1)} + d\dmod{\dmod{\dfrac{nR_1}{d}}{1}}{(f/R_1)} \notag \\ \| − \dfrac{fd}{R_1} C\dparen{\dmod{nQ_1 + \dfloor{\dfrac{nR_1}{d}}}{(f/R_1)} + \dmod{\dmod{\dfrac{nR_1}{d}}{1}}{(f/R_1)} ≥ \dfrac{f}{R_1}} \notag \\ \| − d\dmod{nQ_1 + \dfloor{\dfrac{nR_1}{d}}}{(f/R_1)} \notag \\ \| = d\dmod{\dmod{\dfrac{nR_1}{d}}{1}}{(f/R_1)} \notag \\ \| − \dfrac{fd}{R_1} C\dparen{\dmod{nQ_1 + \dfloor{\dfrac{nR_1}{d}}}{(f/R_1)} + \dmod{\dmod{\dfrac{nR_1}{d}}{1}}{(f/R_1)} ≥ \dfrac{f}{R_1}} \end{align}

We have \( f/R_1 ≥ 1 \) and \( 0 ≤ \dmod{nR_1/d}{1} \lt 1 \) so the \( \dmod{•}{(f/R_1)} \) surrounding that has no effect, so

\begin{equation} d\dmod{\dmod{\dfrac{nR_1}{d}}{1}}{(f/R_1)} = d\dmod{\dfrac{nR_1}{d}}{1} = \dmod{nR_1}{d} \end{equation}

Likewise, the formula within the \( C(•) \) can be rewritten as

\begin{equation} \dmod{nQ_1 + \dfloor{\dfrac{nR_1}{d}}}{(f/R_1)} + \dmod{\dfrac{nR_1}{d}}{1} ≥ \dfrac{f}{R_1} \end{equation}

We have

\begin{align} nQ_1 + \dfloorratio{nR_1}{d} \le \dfrac{f}{R_1} − 1 \\ \dmod{\dfrac{nR_1}{d}}{1} \lt 1 \end{align}

because \( nQ_1 \) and \( \dfloor{nR_1/d} \) and also \( f/R_1 \) are whole numbers. So the sum of those two is less than \( f/R_1 \), so the condition within the \( C(•) \) is never met, so that term drops away. All in all we find

\begin{align} P \| = \dmod{nP_1}{(d/R_1)} \\ Q \| = \dmod{nQ_1 + \dfloorratio{nR_1}{d}}{(f/R_1)} \\ R \| = \dmod{nR_1}{d} \end{align}

For calculating these numbers the following form is more convenient:

\begin{align} P \| = \dmod{nP_1}{(d/R_1)} \\ \{ ρ, R \} \| = \Div(nR_1, d) \\ Q \| = \dmod{nQ_1 + ρ}{(f/R_1)} \end{align}

As long as \( n \lt d/R_1 \), also \( \dfloor{nR_1/d} = 0 \) so \( Q = \dmod{nQ_1}{(f/R_1)} \). That will usually be the case in practice.

With our example (\( P_1 = 99902, Q_1 = 3383, R_1 = 1, d = 765433, f = 25920, t = 13835 \)) we find

\begin{align*} P \| = \dmod{99902n}{765433} \\ \{ ρ, R \} \| = \Div(n, 765433) \\ Q \| = \dmod{3383n + ρ}{25920} \end{align*}

\({n}\) \({P}\) \({ρ}\) \({R}\) \({Q}\) \({fP−dQ}\) \({X/w}\)
1 99902 0 1 3383 1 −20561.6
2 199804 0 2 6766 2 −141024.5
3 299706 0 3 10149 3 −261487.5
4 399608 0 4 13532 4 −381950.5
5 499510 0 5 16915 5 −502413.4
6 599412 0 6 20298 6 −622876.3
7 699314 0 7 23681 7 −743339.3
8 33783 0 8 1144 8 +2500.9
9 133685 0 9 4527 9 −9114.0
765432 665531 0 765432 22536 765432 −6.1
765433 0 1 0 0 0
765434 99902 1 1 3383 1 −20561.6
765435 199804 1 2 6766 2 −141024.5

so \( n = 8 \) is the smallest value of \( n \) for which we find \( X \gt w \).

To find

\begin{equation} X = (w − fP − t)\dfrac{P}{R} \gt 0 \end{equation}

we need \( P \) to be close enough to \( P_0 = (w − t)/(2f) \).

With \( P = \dmod{nP_1}{(d/R_1)} \), the smallest positive value \( n_0 \) of \( n \) for which exactly \( P = P_0 \) is \( n_0 = P_0/P_1 \) and each next value of \( n \) for which \( P = P_0 \) is \( d/P_1R_1 \) greater than the previous one, so the values \( n_k \) of \( n \) for which \( P = P_0 \) are

\begin{equation} n_k = \dfrac{P_0}{P_1} + k\dfrac{d}{P_1R_1} \end{equation}

for whole values \( 0 ≤ k \lt d \).

The whole number \( [n_k] = \dfloor{n_k + 1/2} \) nearest to \( n_k \) is then the candidate value of \( n \). Calculate \( X \) for that value of \( n \) to see if it meets the condition that \( X \gt w \).

In our example we find

\begin{align*} P_0 \| = \dfrac{w − t}{2f} = \dfrac{2147483647 - 13835}{2×25920} = 41424.9577932 \\ n_k \| = \dfrac{P_0}{P_1} + k\dfrac{d}{P_1R_1} = \dfrac{41424.9577932}{99902} + \dfrac{765433}{99902×1}k \\ \| = 0.414655940754 + 7.66183860183 k \\ n \| = \dfloor{n_k + ½} = \dfloor{0.914655940754 + 7.66183860183 k} \\ R \| = n \\ P \| = \dmod{99902n}{765433} \\ X \| = (w − fP − t)\dfrac{P}{R} = (2147469812 − 25920 P)\dfrac{P}{R} \end{align*}

The first couple of relatively good values of \( n \) are then

\({k}\) \({n_k}\) \({n}\) \({P}\) \({Q}\) \({R}\) \({X/w}\)
0 0.41 0 0 0 0 +0.0
1 8.08 8 33783 1144 8 +2500.9
2 15.74 16 67566 2288 16 +779.0
3 23.40 23 1447 49 23 +61.8
4 31.06 31 35230 1193 31 +653.2
5 38.72 39 69013 2337 39 +295.5
6 46.39 46 2894 98 46 +60.7
7 54.05 54 36677 1242 54 +378.5
8 61.71 62 70460 2386 62 +170.0

The first \( n \) is 0 and is no good. The first value about that is 8, as we saw before.

If we use equations \eqref{eq:detour1}ff then it is sufficient if \( X ≥ d \) to be able to handle all values \( |x| \lt w \), because then we can use equations \eqref{eq:detour1}ff to first make \( |x| \lt d \). If we do not use equations \eqref{eq:detour1}ff, then we must have \( X ≥ w \) to be able to handle all values \( |x| \lt w \).

There can be multiple combinations of \( P\), \( Q \), \( R \) that yield \( X ≥ w \) or \( X ≥ d \), and those are all good enough. Within that group, we prefer small \( P\), \( Q\), \( R \), because that makes for calculations that are easier to perform and read.

For the case from the previous example, \( X ≥ w \) for 1498 values of \( R \) between 8 and 20312, and \( X ≥ d \) for 82576 values of \( R \) between 8 and 765'426. We found solutions for \( X ≥ w \) so we do not need equations \eqref{eq:detour1}ff. Within the group with \( X ≥ w \) we search for the smallest values of \( PQR \). We find

\({R}\) \({P}\) \({Q}\) \({PQR}\) \({X}\)
23 1447 49 1.6 × 106 1.33 × 1011
46 2894 98 1.3 × 107 1.30 × 1011
544 945 32 1.6 × 107 3.69 × 109
69 4341 147 4.4 × 107 1.28 × 1011
92 5788 196 1.0 × 108 1.26 × 1011

We choose \( P = 1447 \), then \( R = 23 \) and \( Q = 49 \).

Now we calculate \( y \), \( e \) for \( x = 1710321 \). We find

\begin{align*} q \| = \dfloorratio{x}{P} = \dfloorratio{1710321}{1447} = 1181 \\ r \| = \dmod{x}{P}= \dmod{1710321}{1447} = 1414 \\ y \| = qQ + \dfloorratio{qR + fr + t}{d} \\ \| = 1181×49 + \dfloorratio{1181×23 + 25920×1414 + 13835}{765433} \\ \| = 57869 + \dfloorratio{36691878}{765433} \\ \| = 57869 + 47 = 57916 \\ e \| = \dmod{36691878}{765433} = 716527 \end{align*}

The greatest intermediate result is 36'691'878, which is considerably less than \( w \).

Had we been allowed arbitrarily large intermediate results, then we'd have found

\begin{align*} y \| = \dfloorratio{fx + t}{d} = \dfloorratio{25920×1710321 + 13835}{765433} = \dfloorratio{44331534155}{765433} = 57916 \\ e \| = \dmod{44331534155}{765433} = 716527 \end{align*}

which are the same results, but with the much greater intermediate result of 44'331'534'155.

Another example: \( d = 146097 \), \( f = 4800 \), \( t = 15793 \), \( w = 2^{31} − 1 = 2147483647 \). Then \( \dfrac{w}{f} = \dfrac{2^{31} − 1}{4800} ≈ 447392 \), so we seek a \( P \lt 146097 \lt 447392 \) and an \( R \) as small as possible.

The Extended Algorithm of Euclid (or a search) yields that \( −15188f + 499d = 3 \), and there is no sum of a multiple of \( f \) and a multiple of \( d \) that is closer to 0. It follows that

\begin{align*} \\ P_1 \| = \dmod{−15188}{(146097/3)} = \dmod{−15188}{48699} = 33511 \\ Q_1 \| = \dmod{−499}{(4800/3)} = \dmod{−499}{1600} = 1101 \\ R_1 \| = 3 \end{align*}

Let's check:

\[ fP_1 − dQ_1 = 4800×33511 − 146097×1101 = 3 = R_1 \]

so possibly good combinations are

\begin{align*} P \| = \dmod{nP_1}{(d/R_1)} = \dmod{33511n}{48699} \\ \{ ρ, R \} \| = \Div(nR_1, 146097) = \Div(3n, 146097) \\ Q \| = \dmod{nQ_1 + ρ}{(f/R_1)} = \dmod{1101n + ρ}{1600} \end{align*}

The best candidates for \( n \) are

\begin{align*} P_0 \| = \dfrac{w − t}{2f} = \dfrac{2147483647 − 15793}{2×4800} = 223694.568125 \\ n_k \| = \dfrac{P_0}{P_1} + k\dfrac{d}{P_1R_1} = \dfrac{223694.568125}{33511} + \dfrac{146097}{33511×3}k \\ \| = 6.67525791904 + 1.4532243144 k \end{align*}

For small \( R \) we find the following combinations:

\({R}\) \({P}\) \({Q}\)
3 33511 1101
6 67022 2202
9 100533 3303
12 134044 4404

There are 57132 combinations for which \( X ≥ w \). The combinations with the smallest \( PQR \) are:

\({P}\) \({Q}\) \({R}\) \({PQR}\) \({X/w}\)
487 16 48 3.74 × 105 10.1
974 32 96 2.99 × 106 10.1
761 25 375 7.13 × 106 41.3
1461 48 144 1.01 × 107 10.1
1248 41 423 2.16 × 107 37.7
6270 206 18 2.32 × 107 343.5

We choose \( P = 487 \), then \( Q = 16 \) and \( R = 48 \). We calculate \( y\), \( e \) for \( x = 731767 \). Then

\begin{align*} q \| = \dfloorratio{731767}{487} = 1502 \\ r \| = \dmod{731767}{487} = 293 \\ y \| = 1502×16 + \dfloorratio{1502×48 + 4800×293 + 15793}{146097} \\ \| = 24032 + \dfloorratio{1494289}{146097} = 24032 + 10 = 24042 \\ e \| = \dmod{1494289}{146097} = 33319 \end{align*}

The greatest intermediate result is 1'494'289, which is comfortably smaller than \( w \). With the original formula we'd have found

\begin{align*} y \| = \dfloorratio{4800×731767 + 15793}{146097} = \dfloorratio{3512497393}{146097} = 24042 \\ e \| = \dmod{3512497393}{146097} = 33319 \end{align*}

which are the same results as before but with the greatest intermediate result equal to 3'512'497'393, which is considerably larger than \( w \).

I've investigated for all \( w \) from 3 through 1024 what the greatest values of \( f \) and \( d \) are for which solutions can be found with \( X ≥ d \) or \( X ≥ w \), and what the greatest \( P \), \( Q \), \( R \), and \( X \) are among those solutions, and what the greatest number \( c \) of such solutions is, and the total count \( N \) of combinations of \( f \) and \( d \) for which there is at least one solution. I find

\begin{align} \max(f_w) \| ≈ \dfloorratio{w}{4} \\ \max(f_d) \| ≈ \dfloorratio{w}{3} \\ \max(d_w) \| ≈ w − 5 + ⌊w⌉_2 \\ \max(d_d) \| ≈ w − 1 \\ \max(c_w) \| ≈ \dfloorratio{w+8}{16} \\ \max(c_d) \| ≈ \dfloorratio{w−1}{2} \\ \max(P_w) \| ≈ \dfloorratio{w}{2} − 2 + ⌊w⌉_2 \\ \max(P_d) \| ≈ \dfloorratio{w}{2} − 1 \\ \max(Q) \| ≈ \dfloorratio{2\sqrt{w − 1} − 3}{2} \\ \max(R_w) \| ≈ \dfloorratio{\sqrt{w + 1} − 1}{2} \\ \max(R_d) \| ≈ \frac{\sqrt{w(w + 1100)}}{25} \\ \max(X) \| ≈ \dfloorratio{w^2}{8} \\ N_w \| ≈ \frac{1}{5}w^{5/3} \\ N_d \| ≈ \frac{1}{3}w^{5/3} \end{align}

where the variables with subscript \( w \) apply only to \( X \gt w \), those with subscript \( d \) apply only to \( X \gt d \), and those without a subscript apply to both. The formulas for \( \max(R_d) \), \( N_w \), and \( N_d \) are approximations; the other formulas are exact for \( 10 ≤ w ≤ 1024 \).

For example, for \( w = 1023 \) I find

\begin{align*} \max(f_w) \| ≈ \dfloorratio{1023}{4} = 255 \\ \max(f_d) \| ≈ \dfloorratio{1023}{3} = 341 \\ \max(d_w) \| ≈ 1023 − 5 + ⌊1023⌉_2 = 1019 \\ \max(d_d) \| ≈ 1023 − 1 = 1022 \\ \max(c_w) \| ≈ \dfloorratio{1023 + 8}{16} = 64 \\ \max(c_d) \| ≈ \dfloorratio{1023 − 1}{2} = 511 \\ \max(P_w) \| ≈ \dfloorratio{1023}{2} − 2 + ⌊1023⌉_2 = 510 \\ \max(P_d) \| ≈ \dfloorratio{1023}{2} − 1 = 510 \\ \max(Q) \| ≈ \dfloorratio{2×\sqrt{1023 − 1} − 3}{2} ≈ ⌊30.47⌋ = 30 \\ \max(R_w) \| ≈ \dfloorratio{\sqrt{1023 + 1} − 1}{2} ≈ ⌊15.50⌋ = 15 \\ \max(R_d) \| ≈ \frac{\sqrt{1023 × (1023 + 1100)}}{25} ≈ 58.95 \\ \max(X) \| ≈ \dfloorratio{1023^2}{8} = 130816 \\ N_w \| ≈ \frac{1}{5}1023^{5/3} ≈ 20772.52 \\ N_d \| ≈ \frac{1}{3}1023^{5/3} ≈ 34620.87 \end{align*}

These values are all correct, except that \( R_d = 59 \), \( N_w = 20014 \), and \( N_d = 33937 \).

If we extrapolate the above formules to \( w = 2^{31} − 1 \) (for values with a width of 32 bits), then we find, approximately

\begin{align*} \max(f_w) \| ≈ 5.4×10^8 \\ \max(f_d) \| ≈ 7.2×10^8 \\ \max(d) \| ≈ 2.1×10^9 \\ \max(c_w) \| ≈ 1.3×10^8 \\ \max(c_d) \| ≈ 1.1×10^9 \\ \max(P) \| ≈ 1.1×10^9 \\ \max(Q) \| ≈ 46340 \\ \max(R_w) \| ≈ 23170 \\ \max(R_d) \| ≈ 8.6×10^7 \\ \max(X) \| ≈ 5.8×10^{17} \\ N_w \| ≈ 7.1×10^{14} \\ N_d \| ≈ 1.2×10^{15} \end{align*}

so if we seek a solution for \( X ≥ w \) (so that we do not need equations \eqref{eq:detour1}ff), then we need try only up to 23170 different values of \( R \). If such a solution does not exist, then we seek a solution for \( X ≥ d \) (for which we do need to use equations \eqref{eq:detour1}ff) and then we must try up to 86 million values of \( R \), which is still much less than the up to 1100 million values of \( P \).

For a given \( w \) there are many combinations of \( d \) and \( f \) for which there are no solutions with \( X ≥ w \) or \( X ≥ d \). For example, for \( w = 499 \) there are only 6554 combinations of \( d \) and \( f \) with at least one solution with \( X ≥ w \), and 10560 combinations with at least one solution with \( X ≥ d \), but there are \( \frac{1}{2} (w − 2)×(w − 3) = 123256 \) combinations with \( 2 ≤ f \lt d \lt w \). The greater \( f \) is, the smaller is the chance that there is a solution for the combination of that \( f \) and an arbitrary \( d \gt f\).

However, when \( f \) and \( d \) are considerably less than \( w \), then there is a good chance of a solution. For example, for \( w = 2^{31} − 1 \) and for \( f \) and \( d \) near \( f ≈ 25920 \) and \( d ≈ 765433 \) there is no solution with \( X ≥ d \) for only roughly 1 out of every 9100 combinations of \( f \) and \( d \), and no solution with \( X ≥ w \) for only roughly 1 out of about 54 combinations.

1.3. The Simple Calendar

For convenience, we'll work with a calendar that has only "days" and "months", where the months are longer than the days, but the formulas also work for other units of time. Later, we'll discuss calendars with more than two time periods.

We'll indicate with \( m \) the month number, with \( d \) the day number within the month, and with \( s \) the running day number that is the start or endpoint of the calculation.

1.3.1. From Month and Day Number Within the Month to Running Day Number

In the simplest case, the average length of the month is equal to \( p \) days, and all months are either \( ⌊p⌋ \) or \( ⌊p⌋ + 1 \) days long. We define \( q \) as the length of the short months, and \( ψ \) as the fraction of months that are long. Then

\begin{eqnarray} q \| = \| \dfloor{p} \\ p \| = \| q + ψ \end{eqnarray}

The running day number \( s \) depends on the month number \( m \) and the day number \( d \) within the current month as follows:

\begin{eqnarray} σ \| ≡ \| ⌊pm + b⌋ \label{eq:sm} \\ s \| ≡ \| σ + d \label{eq:s} \end{eqnarray}

where \( m, d, s, σ \) are whole numbers, and \( p ≥ 1 \) and \( b \) are fixed values that depend on the calendar. The first month has \( m = 0 \) and the first day of the month has \( d = 0 \), for easier calculation. \( σ \) is the running day number of the beginning of the month. If values of \( σ \) for multiple months are of interest then we write \( σ[m] \) for the value of \( σ \) that goes with month \( m \).

The first day (\( d = 0 \)) of the first month (\( m = 0 \)) has running day number \( s = 0 \), so

\begin{equation} ⌊b⌋ = 0 ⇔ 0 ≤ b \lt 1 \end{equation}

So \( b \) must be between 0 and 1 (0 is allowed, 1 is not).

1.3.2. Month Length

The length \( L(m) \) of month \( m \) is

\begin{eqnarray} L(m) \| = \| σ(m + 1) − σ(m) \notag \\ \| = \| \dfloor{q (m + 1) + ψ (m + 1) + b} − \dfloor{qm + ψm + b} \notag \\ \| = \| q + \dfloor{ψm + ψ + b} − \dfloor{ψm + b} \notag \\ \| = \| q + \dfloor{ψm + b} + C\dparen{\dmod{ψm + b}{1} + ψ ≥ 1} − \dfloor{ψm + b} \eqavide{eq:splitfloor} \notag \\ \| = \| q + C\dparen{\dmod{ψm + b}{1} + ψ ≥ 1} \end{eqnarray}

All months are \( q \) or \( q + 1 \) days long. Month \( m \) is a long month (with \( q + 1 \) days) if \( \dmod{ψx + b}{1} ≥ 1 − ψ \). On average there is a long month after every

\begin{equation} Q = \frac{1}{ψ} \label{eq:Q} \end{equation}

months, but that isn't always a whole number, so in practice the number of months between two successive long months is equal to \( ⌊Q⌋ \) or \( ⌈Q⌉ \).

Also

\begin{align} L(m) \| = q + \dfloor{ψm + ψ + b} − \dfloor{ψm + b} \notag \\ \| = q + ψm + ψ + b − \dmod{ψm + b + ψ}{1} − \dparen{ψm + b − \dmod{ψm + b}{1}} \notag \\ \| = q + ψ + \dmod{ψm + b}{1} − \dmod{ψm + b + ψ}{1} \notag \\ \| = p + \dmod{ψm + b}{1} − \dmod{ψm + b + ψ}{1} \end{align}

1.3.3. Shifting the Pattern of Months

With this we get a certain pattern of long and short months. Suppose that that pattern would be exactly right if only it were shifted by \( ∆m \) months. So if \( σ_* \) goes with the old calendar (with \( b_* \)) and \( σ \) goes with the new calendar (with \( b = b_* + ∆b \)) then we want that the following holds for all values of \( m \):

\begin{equation} σ(m + 1) − σ(m) = σ_*(m + ∆m + 1) − σ_*(m + ∆m) \end{equation}

We have

\begin{align} \| σ(m + 1) − σ(m) \notag \\ \| = \dfloor{p(m + 1) + b} − \dfloor{pm + b} \notag \\ \| = \dfloor{pm + b + p} − \dfloor{pm + b} \notag \\ \| = \dfloor{\dfloor{pm + b} + \dmod{pm + b}{1} + p} − \dfloor{pm + b} \notag \\ \| = \dfloor{pm + b} + \dfloor{\dmod{pm + b}{1} + p} − \dfloor{pm + b} \notag \\ \| = \dfloor{\dmod{pm + b}{1} + p} \end{align}

and also

\begin{align} \| σ_*(m + ∆m + 1) − σ_*(m + ∆m) \notag \\ \| = \dfloor{p(m + ∆m + 1) + b_*} − \dfloor{p(m + ∆m) + b_*} \notag \\ \| = \dfloor{pm + p∆m + b_* + p} − \dfloor{pm + p∆m + b_*} \notag \\ \| = \dfloor{\dfloor{pm + p∆m + b_*} + \dmod{pm + p∆m + b_*}{1} + p} − \dfloor{pm + p∆m + b_*} \notag \\ \| = \dfloor{pm + p∆m + b_*} + \dfloor{\dmod{pm + p∆m + b_*}{1} + p} − \dfloor{pm + p∆m + b_*} \notag \\ \| = \dfloor{\dmod{pm + b_* + p∆m}{1} + p} \end{align}

so we get what we want if

\begin{equation} b = \dmod{b_* + p∆m}{1} \end{equation}

1.3.4. From Running Day Number to Month and Day Number Within the Month

We go from running month number \( m \) and day number \( d \) within the month to running day number \( s \) via a formula like

\begin{equation} s = \dfloor{pm + v} + d = σ + d \label{eq:sv} \end{equation}

where \( v \) is an arbitrary number. Equation \eqref{eq:s} is like Eq. \eqref{eq:sv} if \( v = b \). For finding \( m \) from \( s \) we look for a formula like:

\begin{equation} m = \dceilratio{s + u − p}{p} = \dceilratio{s + u}{p} − 1 \end{equation}

What should \( u \) be to make this work?

\begin{align} m \| = \dceilratio{s + u − p}{p} \notag \\ \| = \dceilratio{\dfloor{pm + v} + d + u − p}{p} \eqavide{eq:s} \notag \\ \| = \dceilratio{d + u + pm + v − \dmod{pm + v}{1} − p}{p} \notag \\ \| = m + \dceilratio{d + u + v − \dmod{pm + v}{1} − p}{p} \end{align}

so then we need

\begin{eqnarray} \dceilratio{d + u + v − \dmod{pm + v}{1} − p}{p} = 0 \notag \\ −1 \lt \dfrac{d + u + v − \dmod{pm + v}{1} − p}{p} ≤ 0 \notag \\ −p \lt d + u + v − \dmod{pm + v}{1} − p ≤ 0 \notag \\ \dmod{pm + v}{1} − v − d \lt u ≤ \dmod{pm + v}{1} − v − d + p \end{eqnarray}

This must hold for all values of \( d \) and \( m \) that can occur in the calendar. For the left-hand inequality \( … \lt u \) this means that we should substitute the least value that \( d \) can have, because greater values of \( d \) meet the inequality more easily than lesser values do. So we can substitute \( d = 0 \).

For the right-hand inequality \( u ≤ … \) we should substitute the greatest value of \( d \) that can occur, because lesser values of \( d \) meet the inequality more easily than greater values do. So we can substitute \( d = L(m) − 1 \), the day number (since the beginning of the month) of the last day in month \( m \). We rewrite it into a more form that is more convenient here.

\begin{align} L(m) \| = σ(m + 1) − σ(m) \notag \\ \| = \dfloor{p(m + 1) + v} − \dfloor{pm + v} \notag \\ \| = \dparen{pm + v + p − \dmod{pm + v + p}{1}} − \dparen{pm + v − \dmod{pm + v}{1}} \notag \\ \| = p + \dmod{pm + v}{1} − \dmod{pm + v + p}{1} \end{align}

Then we find

\begin{equation*} \dmod{pm + v}{1} − v \lt u ≤ \dmod{pm + v}{1} − v − \dparen{p + \dmod{pm + v}{1} − \dmod{pm + v + p}{1} − 1} + p \end{equation*}

\begin{equation} \dmod{pm + v}{1} − v \lt u ≤ \dmod{pm + v + p}{1} − v + 1 \end{equation}

For the left-hand inequality \( … \lt u \) we should substitute the greatest possible value of \( \dmod{pm + v}{1} \). That value is just less than 1, so if we substitute 1 then we can change the \( \lt \) to \( ≤ \). For the right-hand inequality \( u ≤ … \) we should substitute the least possible value of \( \dmod{pm + v + p}{1} \), which is 0. Then we find

\begin{equation} 1 − v ≤ u ≤ 1 − v \end{equation}

Those inequalities have exactly one solution:

\begin{equation} u = 1 − v \end{equation}

so

\begin{equation} m = \dceilratio{s + 1 − v − p}{p} = \dceilratio{s + 1 − v}{p} − 1 \label{eq:svtom} \end{equation}

In the above derivation it was necessary to use the inequality \( d ≤ L(m) − 1 \). If we had instead used \( d \lt L(m) \) then we would not have found a solution. That means that you may get the wrong answer if \( s \) is a fractional number that belongs to a moment of time during the last day of the month.

The transition from one value of \( m \) to the next happens when

\[ \dfrac{s + 1 − v − p}{p} \]

is exactly a whole number (which we'll call \( μ \)), so when

\[ s = pμ + p + v − 1 = p(μ + 1) + v − 1 \]

\( p \) and \( v \) need not be whole numbers, so the value of \( s \) at which Equation \eqref{eq:svtom} switches to a new month need not be a whole number, either.

So Equation \eqref{eq:svtom} requires \( s \) to be a whole number, otherwise you may get the wrong month number \( m \).

With \( p = 30.6 \) and \( v = b = 0 \) we find

\[ s = \dfloor{pm + b} + d = \dfloor{30.6m} + d \]

so month \( m = 2 \) begins on day

\[ s = \dfloor{30.6×2} + 0 = \dfloor{61.2} = 61 \]

From day \( s \) to month \( m \) goes via

\[ m = \dceilratio{s + 1 − b − p}{p} = \dceilratio{s − 29.6}{30.6} \]

That yields, for a few moments between \( s = 60 \) and \( s = 61 \):

\({s}\) \({\dfrac{s−29.6}{30.6}}\) \({m}\)
60 0.99346405 1
60.2 1. 1
60.21 1.0003268 2
61 1.0261438 2

so for \( s \) equal to or just below 60.2 we find \( m = 1 \) as desired, but for \( s \) greater than 60.2 and less than 61 we find \( m = 2 \) while that part, too, of day \( s = 60 \) still belongs to month \( m = 1 \).

And then we can also find \( d \).

\begin{align} pm \| = p\dceilratio{s + 1 − v}{p} − p \notag \\ \| = p\dparen{\dfrac{s + 1 − v}{p} + \ddom{\dfrac{s + 1 − v}{p}}{1}} − p \eqavide{eq:dom} \notag \\ \| = s + 1 − v − p + \ddom{s + 1 − v}{p} \\ d \| = s − \dfloor{pm + v} \notag \\ \| = s − \dfloor{s + 1 − p + \ddom{s + 1 − v}{p}} \notag \\ \| = −\dfloor{1 − p + \ddom{s + 1 − v}{p}} \eqavide{eq:minusfloor} (s ∥ 1) \notag \\ \| = \dceil{p − 1 − \ddom{s + 1 − v}{p}} \eqavide{eq:minusceil} \end{align}

So if we split \( s + 1 − v \) into

\begin{equation} s + 1 − v = pμ − δ = p\dceilratio{s + 1 − v}{p} − \ddom{s + 1 − v}{p} \end{equation}

then

\begin{align} m \| = μ − 1 \label{eq:mviaμ} \\ d \| = \dceil{p − 1 − δ} \label{eq:dviaδ} \end{align}

We can rewrite this so we can use the \( \Div \) function, through

\begin{equation}\{−μ,δ\} = \Div(−(s + 1 − v), p)\end{equation}

For the simple calendar \( v = b \) so then

\begin{align} \{−μ,δ\} \| = \Div(−(s + 1 − b), p) \\ m \| = μ − 1 = \dceilratio{s + 1 − b}{p} − 1 \label{eq:ynaarx} \\ d \| = \dceil{p − 1 − δ} = \dceil{p − 1 − \ddom{s + 1 − b}{p}} \end{align}

An example, with \( p = 30.6 \) and \( b = 0 \). We get

\begin{align*} σ \| = \dfloor{pm + b} = \dfloor{30.6 m} \\ L(m) \| = q + C\dparen{\dmod{ψm + b}{1} + ψ ≥ 1} = 30 + C\dparen{\dmod{0.6m}{1} ≥ 0.4} \\ μ \| = \dceilratio{s + 1 − b}{p} = \dceilratio{s + 1}{30.6} \\ m \| = μ − 1 = \dceilratio{s − 29.6}{30.6} \\ δ \| = \ddom{s + 1 − b}{p} = \ddom{s + 1}{30.6} \\ d \| = \dceil{p − 1 − δ} = \dceil{29.6 − δ} = \dceil{29.6 − \ddom{s + 1}{30.6}} \end{align*}

And then, for the first few months

\({m}\) \({pm}\) \({σ(m)}\) \({L(m)}\)
−1 −30.6 −31 31
0 0 0 30
1 30.6 30 31
2 61.2 61 30
3 91.8 91 31
4 122.4 122 31
5 153 153 30
6 183.6 183 31
7 214.2 214 30
8 244.8 244 31
9 275.4 275 31
10 306 306

Because \( 5p = 153 \) is a whole number, the pattern of month lengths repeats itself after 5 months. We see here the pattern 31-30-31-30-31 that in the Gregorian calendar describes the months March - July and August - December.

Now we go in the opposite direction, for a few running day numbers:

\({s}\) \({μ}\) \({δ}\) \({m}\) \({d}\)
−2 0 1. −1 29
−1 0 0. −1 30
0 1 29.6 0 0
1 1 28.6 0 1
90 3 0.8 2 29
91 4 30.4 3 0
120 4 1.4 3 29
121 4 0.4 3 30
122 5 30. 4 0
123 5 29. 4 1

1.4. With Whole Numbers Only

Calculating machines often work with a limited number of decimals behind the decimal mark, so you can get into trouble with round-off errors. If \( (s + 1 − b)/p = 6.99999999998 \) but because of a very small round-off error your calculating machine thinks that \( (s + 1 − b)/p = 7.00000000001 \), then your calculating machine thinks that \( \dceil{(s + 1 − b)/p} = 7 \) rather than 6, and then you find the wrong month.

Converting equations with \( ⌈•⌉ \) or \( ⌈•⌋_1 \) into equations with \( ⌊•⌋ \) or \( ⌊•⌉_1 \) or \( \Div \) is easier when only whole numbers are used, because for arbitrary whole numbers \( x \) and \( y \) (\( y \gt 0 \))

\begin{align} \dceilratio{x}{y} \| = \dfloorratio{x − 1}{y} + 1 \eqavide{eq:ceil2floor} \\ \ddom{x}{y} \| = y − 1 − \dmod{x − 1}{y} \eqavide{eq:ddom2dmod} \end{align}

If the average length \( p \) is a ratio (of whole numbers), then we can avoid round-off errors by rewriting the formulas based on ratios. We define

\begin{align} p \| = \dfrac{f}{g} = q + \dfrac{h}{g} \\ q \| = \dfloorratio{f}{g} \\ h \| = \dmod{f}{g} \end{align}

with \( f \), \( g \) whole numbers greater than zero. Then we find, for an arbitrary value \( x \),

\begin{equation} \dmod{x}{f/g} = \dmod{x}{p} = p\dmod{\dfrac{x}{p}}{1} = \dfrac{f \dmod{\dfrac{gx}{f}}{1}}{g} = \dfrac{\dmod{gx}{f}}{g} \label{eq:gmodp} \end{equation}

and likewise

\begin{equation} \ddom{x}{f/g} = \dfrac{\ddom{gx}{f}}{g} \label{eq:gdomp} \end{equation}

Then we go from month \( m \) and day \( d \) to running day number \( s \) through

\begin{equation} t ≡ vg \end{equation}

\begin{align} s \| = \dfloor{pm + v} + d \notag \\ \| = \dfloor{\dfrac{fm + vg}{g}} + d \notag \\ \| = \dfloor{\dfrac{fm + t}{g}} + d \end{align}

where we demand that \( t \) is a whole number, too. If \( v \) is a ratio then you can always achieve that by a suitable choice of \( g \).

For the month length we find

\begin{equation} L(m) = q + C\dparen{\dmod{hm + t}{g} ≥ g − h}\end{equation}

For the calculation of \( m \) and \( d \) from \( s \) we find:

\begin{align} m \| = \dceilratio{s + 1 − v}{p} − 1 = \dceilratio{gs + g − t}{f} − 1 \notag \\ \| = \dfloorratio{gs + g − t − 1}{f} \eqavide{eq:ceil2floor} (t,g,s,f ∥ 1) \label{eq:ynaarx2} \\ d \| = \dceil{p − 1 − \ddom{s + 1 − v}{p}} \notag \\ \| = \dceilratio{f − g − \ddom{gs + g − t}{f}}{g} \eqavide{eq:gdomp} \notag \\ \| = \dfloorratio{f − g − \ddom{gs + g − t}{f} − 1}{g} + 1 \eqavide{eq:ceil2floor} (t,g,s,f ∥ 1) \notag \\ \| = \dfloorratio{f − g − (f − 1 − \dmod{gs + g − t − 1}{f}) − 1}{g} + 1 \eqavide{eq:ddom2dmod} \notag \\ \| = \dfloorratio{\dmod{gs + g − t − 1}{f}}{g} \eqavide{eq:ddom2dmod} \end{align}

Summarizing, from running month number \( m \) and day number \( d \) in the month to running day number \( s \):

\begin{equation} s = \dfloorratio{fm + t}{g} + d \end{equation}

and from \( s \) to \( m \) and \( d \):

\begin{align} w \| = gs + g − t − 1 \\ m \| = \dfloorratio{w}{f} \label{eq:ynaarxr} \\ d \| = \dfloorratio{\dmod{w}{f}}{g} \end{align}

Now you can do all calculations with ratios, which have no numbers after the decimal mark and hence no round-off errors.

For the same calendar as before we have \( p = 30.6 = 153/5, b = 0 \), so \( f = 153, g = 5, t = 0 \). We then find

\begin{align*} s \| = \dfloorratio{153m}{5} + d \\ w \| = 5s + 4 \\ m \| = \dfloorratio{5s + 4}{153} \\ d \| = \dfloorratio{\dmod{5s + 4}{153}}{5} \end{align*}

and for the beginning of the first few months

\({m}\) \({σ}\) \({L(m)}\)
0 0 30
1 30 31
2 61 30
3 91 31
4 122 31
5 153 30
6 183 31
7 214 30
8 244 31
9 275 31
10 306 30
11 336 31
12 367

Now we go in the opposite direction, for a few running day numbers:

\({s}\) \({w}\) \({m}\) \({\dmod{w}{f}}\) \({d}\)
−2 −6 −1 147 29
−1 −1 −1 152 30
0 4 0 4 0
1 9 0 9 1
120 604 3 145 29
121 609 3 150 30
122 614 4 2 0
123 619 4 7 1

1.5. Very Unequal Months

So far we have assumed that a long month was only one day longer than a short month, but that difference can be larger. Suppose that short months are \( q \) days long and that long months are \( q + r \) days long, with \( q\), \( r \) whole numbers greater than 0, and that a fraction \( ψ \) (between 0 and 1) of all months is long. Then the average length of a month is equal to

\begin{equation} p = q + rψ \end{equation}

days. The formula to calculate the running day number \( σ \) of the first day of month \( m \) is then

\begin{equation} σ = qm + r\dfloor{ψm + b} \label{eq:sm2} \end{equation}

If we substitute \( r = 1 \) then we find

\begin{eqnarray} σ \| = \| qm + \dfloor{ψm + b} \notag \\ \| = \| \dfloor{(q + ψ)m + b} \notag \\ \| = \| \dfloor{pm + b} \end{eqnarray}

which is the same result as Eq. \eqref{eq:sm}.

With Eq. \eqref{eq:sm2}, the length \( L(m) \) of month \( m \) is

\begin{eqnarray} L(m) \| = \| σ(m + 1) − σ(m) \notag \\ \| = \| q(m + 1) + r\dfloor{ψ(m + 1) + b} − (qm + r\dfloor{ψm + b}) \notag \\ \| = \| q + rC\dparen{\dfloor{ψm + ψ + b} − \dfloor{ψm + b}} \notag \\ \| = \| q + rC\dparen{\ddom{ψm + b}{1} + ψ ≥ 1} \eqavide{eq:splitfloor} \end{eqnarray}

so the long months are those for which \(\ddom{ψm + b}{1} + ψ ≥ 1\), just like we found earlier for the simple calendar.

The formula to calculate the running day number \( s \) from month number \( m \) and day number \( d \) in the current month is then

\begin{eqnarray} s \| = \| σ + d \notag \\ \| = \| qm + r⌊ψm + b⌋ + d \label{eq:xnaary3} \end{eqnarray}

How do we go in the opposite direction? To figure that out we compare the running day number that comes from equation \eqref{eq:xnaary3} for the first day (\( d = 0 \)) of month \( m \) with the running day number that follows for the same \( m \) from the most similar simplest calendar. We write the outcome for the desired calendar as \( σ \) and for the simpler comparison calendar as \( σ_* \).

\begin{align} σ \| = qm + r\dfloor{ψm + b} \\ σ_* \| = qm + \dfloor{r(ψm + b)} = \dfloor{pm + rb} \label{eq:σ*} \\ ∆σ ≡ σ_* − σ \| = \dfloor{r(ψm + b)} − r\dfloor{ψm + b} \notag \\ \| = r(ψm + b) − \dmod{r(ψm + b)}{1} − \dparen{r(ψm + b) − r\dmod{ψm + b}{1}} \notag \\ \| = r\dmod{ψm + b}{1} − \dmod{r(ψm + b)}{1} \notag \\ \| = r\dmod{ψm + b}{1} − \dmod{r\dmod{ψm + b}{1}}{1} \notag \\ \| = \dfloor{r\dmod{ψm + b}{1}} \end{align}

This difference satisfies

\begin{equation} 0 \le σ_* − σ \le r − 1 \end{equation}

so the beginning of month \( m \) in the target calendar is between \( 0 \) and \( r − 1 \) days (inclusive) before the beginning of month \( m \) in the simple calendar.

For example, let \( q = 30, ψ = 0.12, r = 5, b = 0.1 \). Then \( p = q + rψ = 30.6 \) is the average length of a month. We find for the first 10 months

\({m}\) \({σ_*}\) \({σ}\) \({σ_* − σ}\) \({ψm + b}\) \({r\dmod{ψm + b}{1}}\)
0 0 0 0 0.1 0.5
1 31 30 1 0.22 1.1
2 61 60 1 0.34 1.7
3 92 90 2 0.46 2.3
4 122 120 2 0.58 2.9
5 153 150 3 0.7 3.5
6 184 180 4 0.82 4.1
7 214 210 4 0.94 4.7
8 245 245 0 1.06 0.3
9 275 275 0 1.18 0.9

and \( σ_* − σ \) varies between 0 and \( r − 1 = 5 − 1 = 4 \) as expected.

For that simpler calendar based on \( σ_* \) we have Equation \eqref{eq:svtom} to calculate the month number from the running day number:

\[ m_*(s) = \dceilratio{s + 1 − v}{p} − 1 \]

That means that

\begin{equation} m_↑ = m_*(s + \max(∆σ)) = \dceilratio{s + \max(∆σ) + 1 − v}{p} − 1 \end{equation}

is greater than or equal to the correct month number. And

\begin{equation} m_↓ = m_*(s + \min(∆σ)) = \dceilratio{s + \min(∆σ) + 1 − v}{p} − 1 \end{equation}

is less than or equal to the correct month number.

With \( m_↑ \) we find

\begin{equation} d_↑ = s − σ(m_↑) \end{equation}

If \( d_↑ \lt 0 \) then \( m_↑ \) is too great. If \( d_↑ \ge 0 \) then we have the correct month number but \( d_↑ \) is probably not the correct day number.

Can we find a recipe to calculate \( d \) with a fixed number of steps? Something like

\begin{align} m_↑ \| ≡ \dceilratio{s + \max(∆σ) + 1 − v}{p} − 1 \\ d_↑ \| = s − σ(m_↑) \\ m \| = m_↑ + ξ \\ d \| = s − σ(m) \end{align}

where \( ξ \) is the correction to apply to \( m_↑ \) to get \( m \). We cannot always find \( ξ \) in one go, but can sometimes find it if \( ξ = 0 \) or \( ξ = 1 \), i.e., when the difference between \( m_↑ \) and \( m \) is at most 1.

We then need \( ξ = 0 \) if \( d_↑ ≥ 0 \), and \( ξ = −1 \) if \( d_↑ \lt 0 \). That suggests something like

\begin{equation} ξ = \dfloorratio{d_↑}{Ξ} \end{equation}

because that yields \( ξ = 0 \) if \( 0 \le d_↑ \lt Ξ \) and yields \( ξ = −1 \) if \( −Ξ \le d_↑ \lt 0 \). What value should we use for \( Ξ \)? That depends on the least and greatest value that \( d_↑ \) can have, so let's deduce those first.

\begin{align} s \| = σ(m) + d \notag \\ \| = σ_*(m) − ∆σ(m) + d \notag \\ \| = \dfloor{pm + v} − ∆σ(m) + d \notag \\ \| = pm + v − \dmod{pm + v}{1} − ∆σ(m) + d \end{align}

so

\begin{align} m_↑ − m \| = \dceilratio{s + \max(∆σ) + 1 − v}{p} − 1 − m \notag \\ \| = \dceilratio{pm + v − \dmod{pm + v}{1} − ∆σ(m) + d + \max(∆σ) + 1 − v}{p} − 1 − m \notag \\ \| = \dceilratio{1 − \dmod{pm + v}{1} + \max(∆σ) − ∆σ(m) + d}{p} − 1 \end{align}

Always \( 0 \lt 1 − \dmod{•}{1} \le 1 \), and \( \max(∆σ) − ∆σ(m) \ge 0 \), and \( d ≥ 0 \), so the part between \( \dceil{} \) is always greater than 0, so \( m_↑ − m ≥ 0 \) as desired. When is certainly \( m_↑ − m ≤ 1 \)? Then we must have

\begin{align*} \dceilratio{1 − \dmod{pm + v}{1} + \max(∆σ) − ∆σ(m) + d}{p} ≤ 2 \\ \dfrac{1 − \dmod{pm + v}{1} + \max(∆σ) − ∆σ(m) + d}{p} ≤ 2 \\ 1 − \dmod{pm + v}{1} + \max(∆σ) − ∆σ(m) + d ≤ 2p \end{align*}

The greatest value that \( d \) can have is \( \max(L) − 1 \), so we certainly have \( m_↑ − m \le 1 \) if

\begin{equation} ρ ≡ \max(∆σ) − \min(∆σ) + \max(L) − 2p ≤ 0 \label{eq:singlepass} \end{equation}

If \( m_↑ = m \) then the least value of \( d_↑ \) is 0 and the greatest value is \( \max(L) − 1 \).

If \( m_↑ = m + 1 \) then

\begin{align} d_↑ \| = s − σ(m_↑) \notag \\ \| = s − σ(m + 1) \notag \\ \| = \dparen{pm + v − \dmod{pm + v}{1} − ∆σ(m) + d} \notag \\ \| − \dparen{pm + v + p − \dmod{pm + v + p}{1} − ∆σ(m + 1)} \notag \\ \| = d − p + \dparen{\dmod{pm + v + p}{1} − \dmod{pm + v}{1}} + ∆σ(m + 1) − ∆σ(m) \notag \\ \| \gt 0 − p + \min\dparen{\dmod{pm + v + p}{1} − \dmod{pm + v}{1}} + \min(∆σ(m + 1) − ∆σ(m)) \notag \\ \| = −p + (\dmod{p}{1} − 1) + \min(∆σ(m + 1) − ∆σ(m)) \eqavide{eq:domp} \notag \\ \| = −p + \dmod{p}{1} − 1 + \min(L_*(m) − L(m)) \notag \\ \| = −\dfloor{p} − 1 + \min(L_*(m) − L(m)) \eqavide{eq:mod} \notag \\ \| ≥ −\dfloor{p} − 1 + \min(L_*) − \max(L) \notag \\ \| = −\dfloor{p} − 1 + \dfloor{p} − \max(L) \notag \\ \| = −1 − \max(L) \notag \\ d_↑ \| \gt −1 − \max(L) \\ d_↑ \| ≥ −\max(L) \end{align}

so

\begin{equation} −\max(L) ≤ d_↑ ≤ \max(L) − 1 \end{equation}

For

\[ ξ = \dfloorratio{d_↑}{Ξ} \]

we then need both of the following conditions satisfied:

\begin{align*} \dfloorratio{−\max(L)}{Ξ} = −1 \\ \dfloorratio{\max(L) − 1}{Ξ} = 0 \end{align*}

Both those conditions are equivalent to

\begin{equation} Ξ ≥ \max(L) \end{equation}

So if we use

\begin{equation} Ξ = \max(L) \end{equation}

(or a greater value) then we meet both constraints.

Summarizing, if

\[ ρ = \max(∆σ) − \min(∆σ) + \max(L) − 2p ≤ 0 \]

then

\begin{align} m_↑ \| = \dceilratio{s + \max(∆σ) + 1 − v}{p} − 1 \\ d_↑ \| = s − σ_↑ = s − σ(m_↑) \\ ξ \| = \dfloorratio{d_↑}{\max(L)} \\ m \| = m_↑ + ξ \\ d \| = s − σ(m) \end{align}

With

\begin{align*} v \| = rb \\ \max(∆σ) \| = r − 1 \\ \min(∆σ) \| = 0 \\ \max(L) \|= q + r \\ p \| = q + rψ \end{align*}

we find

\begin{align} m_↑ \| = \dceilratio{s + r − rb}{p} − 1 \label{eq:ynaarx3} \\ d_↑ \| = s − σ_↑ = s − σ(m_↑) \\ ρ \| = 2r(1 − ψ) − 1 − q \\ ξ \| = \dfloorratio{d_↑}{q + r} \\ m \| = m_↑ + ξ \\ d \| = s − σ(m) \end{align}

so \( ρ ≤ 0 \) corresponds to

\begin{equation} r ≤ \dfrac{1}{2} \dfrac{q + 1}{1 − ψ} ≥ q + 1 \end{equation}

If \( ρ \gt 0 \) then the recipe doesn't always work so then you shouldn't use it. Then see Section 1.7.

For the calendar from the previous example we find

\begin{align*} m_↑ \| = \dceilratio{s + r − rb}{p} − 1 = \dceilratio{s − 26.1}{30.6} \\ ρ \| = 2r(1 − ψ) − q − 1 = 2×5×(1 − 0.12) − 1 − 30 = −22.2 ≤ 0 \end{align*}

Because \( ρ ≤ 0 \) we can use the recipe with the fixed number of steps.

\({s}\) \({m}\) \({\dfrac{s-26.1}{30.6}}\) \({m_↑}\) \({m_↑-m}\) \({d_↑}\) \({ξ}\) \({m}\) \({d}\)
−5 −1 −1.0163399 −1 0 30 0 −1 30
−4 −1 −0.98366013 0 1 −4 −1 −1 31
0 0 −0.85294118 0 0 0 0 0 0
1 0 −0.82026144 0 0 1 0 0 1
29 0 0.094771242 1 1 −1 −1 0 29
30 1 0.12745098 1 0 0 0 1 0
59 1 1.0751634 2 1 −1 −1 1 29
60 2 1.1078431 2 0 0 0 2 0
89 2 2.0555556 3 1 −1 −1 2 29
90 3 2.0882353 3 0 0 0 3 0
119 3 3.0359477 4 1 −1 −1 3 29
120 4 3.0686275 4 0 0 0 4 0
148 4 3.9836601 4 0 28 0 4 28
149 4 4.0163399 5 1 −1 −1 4 29
150 5 4.0490196 5 0 0 0 5 0
153 5 4.1470588 5 0 3 0 5 3
154 5 4.1797386 5 0 4 0 5 4
209 6 5.9771242 6 0 29 0 6 29
210 7 6.0098039 7 0 0 0 7 0
214 7 6.1405229 7 0 4 0 7 4
215 7 6.1732026 7 0 5 0 7 5

and there indeed \( m_↑ ≥ m\).

If \( p = \dfrac{f}{g} \), \( ψ = \dfrac{h}{g}\) and \( b = \dfrac{t}{g} \) are ratios of whole numbers (\( f, g, h, t \) are whole numbers with \( f \gt g \gt 0 \), \( 0 \le h, t \lt g \)), then we find

\begin{align} s \| = σ + d = qm + r\dfloorratio{hm + t}{g} + d \label{eq:xnaary3r} \\ m_↑ \| = \dceilratio{s + \max(∆σ) + 1 − v}{p} − 1 \notag \\ \| = \dceilratio{gs + g\max(∆σ) + g − gv}{f} − 1 \notag \\ \| = \dceilratio{gs + g\max(∆σ) + g − t}{f} − 1 \notag \\ \| = \dfloorratio{gs + g\max(∆σ) + g − t − 1}{f} \eqavide{eq:ceil2floor} \\ ρ \| = 2r\dparen{1 − \dfrac{h}{g}} − 1 − q \\ d_↑ \| = s − qm_↑ − r\dfloorratio{hm_↑ + t}{g} \\ ξ \| = \dfloorratio{d_↑}{q + r} \\ m \| = m_↑ + ξ \\ d \| = s − qm − r\dfloorratio{hm + t}{g} \end{align}

For the calendar from the previous examples we find (with \( q = 30, ψ = 0.12 = 3/25, r = 5, b = 0.1 = 1/10 \)) \( f = 1530, g = 50, h = 6, t = 5 \) and then

\begin{align*} σ \| = 30m + 5\dfloorratio{6m + 5}{50} \\ s \| = 30m + 5\dfloorratio{6m + 5}{50} + d \\ m_↑ \| = \dfloorratio{50s + 50×4 + 50 − 5 − 1}{1530} = \dfloorratio{50s + 244}{1530} \\ d_↑ \| = s − 30m_↑ − 5\dfloorratio{6m_↑ + 5}{50} \\ ξ \| = \dfloorratio{d_↑}{35} \\ m \| = m_↑ + ξ \\ d \| = s − 30m − 5\dfloorratio{6m + 5}{50} \end{align*}

\({m}\) \({σ}\)
0 0
1 30
2 60
3 90
4 120
5 150
6 180
7 210
8 245
9 275

\({s}\) \({m_↑}\) \({d_↑}\) \({ξ}\) \({m}\) \({d}\)
−5 −1 30 0 −1 30
−4 0 −4 −1 −1 31
−1 0 −1 −1 −1 34
0 0 0 0 0 0
1 0 1 0 0 1
148 4 28 0 4 28
149 5 −1 −1 4 29
150 5 0 0 5 0
209 6 29 0 6 29
210 7 0 0 7 0

1.6. Many Kinds of Unequal Months

We now allow there to be more than two kinds of months with different month lengths. In equation \eqref{eq:xnaary3}, \( \dfloor{ψm + b} \) gave the pattern of long months (with a shift as desired). We now allow an arbitrary number of such pattern, each with its own length difference \( r_i \) (that can be positive or negative), relative frequency \( 0 \lt ψ_i \lt 1 \), and pattern shift \( b_i \). Then we find

\begin{align} σ \| = qm + \sum_{i}r_i \dfloor{ψ_im + b_i} \\ s = σ + d \| = qm + \sum_{i}r_i \dfloor{ψ_im + b_i} + d \label{eq:xnaary4} \end{align}

The derivation of the formulas for the opposite direction goes analogous to that of equation \eqref{eq:ynaarx3}. We find

\begin{align} σ_* \| = qm + \dfloor{\sum_i r_i (ψ_i m + b_i)} \notag \\ \| = \dfloor{pm + \sum_i r_i b_i} \notag \\ \| = pm + \sum_i r_i b_i − \dmod{\sum_i r_i \dparen{ψ_i m + b_i}}{1} \label{eq:σ*∑} \eqavide{eq:mod} \\ ∆σ = σ_* − σ \| = \dfloor{\sum_{i}r_{i}(ψ_{i}m + b)} − \sum_{i} r_{i}\dfloor{ψ_{i}m + b_{i}} \notag \\ \| = \dfloor{\sum_{i} r_{i}\dmod{ψ_{i}m + b_{i}}{1}} \eqavide{eq:dmoddiff1} \notag \end{align}

so

\begin{align} v \| = \sum_i r_ib_i \\ \max(L) \| ≤ q + \sum_{r_i \gt 0} r_i \\ \max(∆σ) \| ≤ \dparen{\sum_{r_i \gt 0} r_i} − 1 \\ \min(∆σ) \| ≥ \sum_{r_i \lt 0} r_i \\ ρ \| = \max(∆σ) − \min(∆σ) + \max(L) − 2p \notag \\ \| ≤ 2\dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_{r_i \lt 0} r_i} − q − 1 − 2\dparen{\sum_i r_i ψ_i} \end{align}

It is permissible to use a higher value for \( \max(L) \) in the below formulas, such as the value given above at the right hand side of \( \max(L) ≤ \), and similarly for the other inequalities. For convenience, we pretend that those inequalities are equalities.

And so

\begin{align} m_↑ \| = \dceilratio{s + \max(∆σ) + 1 − v}{p} − 1 \notag \\ \| = \dceilratio{s + \dparen{\sum_{r_i \gt 0} r_i} − \sum_i r_i b_i}{p} − 1 \label{eq:ynaarx4} \\ d_↑ \| = s − qm_↑ − \sum_i r_i\dfloor{ψ_i m_↑ + b_i} \\ ρ \| = 2\dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_{r_i \lt 0} r_i} − q − 1 − 2\dparen{\sum_i r_i ψ_i} \end{align}

If \( ρ ≤ 0 \) then \( m \) and \( m_↑ \) differ by at most 1. That condition corresponds to

\begin{align} 2\dparen{\sum_{r_i \gt 0} r_i} \| − \dparen{\sum_{r_i \lt 0} r_i} − q − 1 − 2\dparen{\sum_i r_i ψ_i} ≤ 0 \notag \\ \dparen{\sum_{r_i \gt 0} r_i} \| + \dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_{r_i \lt 0} r_i} − q − 1 − 2\dparen{\sum_i r_i ψ_i} ≤ 0 \notag \\ \dparen{\sum_{r_i \gt 0} r_i} \| + \dparen{\sum_{i} |r_i|} − q − 1 − 2\dparen{\sum_i r_i ψ_i} ≤ 0 \notag \\ \sum_{r_i \gt 0} r_i \| ≤ q + 1 + 2\dparen{\sum_i r_i ψ_i} − \dparen{\sum_{i} |r_i|} \eqavide{eq:singlepass} \end{align}

Then

\begin{align} ξ \| = \dfloorratio{d_↑}{q + \sum_{r_i \gt 0} r_i} \\ m \| = m_↑ + ξ \label{eq:xζ4} \\ d \| = s − qm − \sum_i r_i\dfloor{ψ_i m + b_i} \end{align}

If all \( ψ_i = h_i/g_i \) and \( b_i = t_i/g_i \) are ratios of whole numbers, and if condition \eqref{eq:singlepass} is met, then we find

\begin{align} p \| = q + \sum_i r_iψ_i = q + \sum_i \dfrac{r_i h_i}{g_i} \\ g \| = \lcm(g_i) \\ γ_i \| = \dfrac{g}{g_i} \\ f \| = pg = gq + \sum_i γ_i r_i h_i \\ s \| = qm + \sum_i r_i\dfloorratio{h_im + t_i}{g_i} + d \label{eq:xnaary4r} \\ m_↑ \| = \dceilratio{s + \dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_i r_i b_i}}{f/g} − 1 \notag \\ \| = \dceilratio{gs + g\dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_i γ_i r_i t_i}}{f} − 1 \notag \\ \| = \dfloorratio{gs + g\dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_i γ_i r_i t_i} − 1}{f} \label{eq:ynaarx4r} \\ d_↑ \| = s − qm_↑ − \sum_i r_i\dfloorratio{h_im_↑ + t_i}{g_i} \\ ρ \| = 2\dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_{r_i \lt 0} r_i} − q − 1 − 2\dparen{\sum_i \dfrac{r_i h_i}{g_i}} \\ ξ \| = \dfloorratio{d_↑}{q + \sum_{r_i \gt 0} r_i} \\ m \| = m_↑ + ξ \\ d \| = s − qm − \sum_i r_i\dfloorratio{h_im + t_i}{g_i} \end{align}

\( g \) is the least common multiple of the denominators of all \( ψ_i \) and \( b_i \).

Suppose we want a calendar in which each month has 17 days, with each 3rd month getting 2 days extra, and each 5th month getting 3 days extra. Then the first couple of months contain the following number of days: 17, 17, 19, 17, 20, 19, 17, 17, 19, 20, 17, 19, 17, 17, 22.

Then we have \( q = 17 \) and

\({i}\) \({r_i}\) \({ψ_i}\) \({g_i}\) \({h_i}\) \({t_i}\) \({γ_i}\)
1 2 1/3 3 1 0 5
2 3 1/5 5 1 0 3

and \( g = 15 \) (the product of denominators 3 and 5). Then

\begin{align*} p \| = 17 + 2×\dfrac{1}{3} + 3×\dfrac{1}{5} = 18 + \dfrac{4}{15} \\ f \| = pg = 15×\dparen{18 + \dfrac{4}{15}} = 274 = 15×17 + 5×2×1 + 3×3×1 \\ s \| = 17m + 2\dfloorratio{m}{3} + 3\dfloorratio{m}{5} \\ m_↑ \| = \dfloorratio{15s + 15×5 − 0 − 1}{274} = \dfloorratio{15s + 74}{274} \\ d_↑ \| = s − 17m_↑ − 2\dfloorratio{m_↑}{3} − 3\dfloorratio{m_↑}{5} \\ ρ \| = 2×5 − 0 − 17 − 1 − 2×\dparen{\dfrac{2×1}{3} + \dfrac{3×1}{5}} = −10 − \dfrac{8}{15} \\ ξ \| = \dfloorratio{d_↑}{22} \\ m \| = m_↑ + ξ \\ d \| = s − 17m − 2\dfloorratio{m}{3} − 3\dfloorratio{m}{5} \end{align*}

The following table shows the results that you get when you calculate for certain calendar dates \( m \), \( d \) what the corresponding running day number \( s \) is, and then calculate from that \( s \) what the month number \( m \) is.

\({m}\) \({d}\) \({s}\) \({m_↑}\) \({d_↑}\) \({ξ}\) \({m_↑+ξ}\)
0 0 0 0 0 0 0
0 16 16 1 −1 −1 0
1 0 17 1 0 0 1
1 16 33 2 −1 −1 1
2 0 34 2 0 0 2
2 18 52 3 −1 −1 2
3 0 53 3 0 0 3
3 16 69 4 −1 −1 3
4 0 70 4 0 0 4
4 19 89 5 −1 −1 4
5 0 90 5 0 0 5
5 18 108 6 −1 −1 5
6 0 109 6 0 0 6
6 16 125 7 −1 −1 6
7 0 126 7 0 0 7
7 16 142 8 −1 −1 7
8 0 143 8 0 0 8
8 18 161 9 −1 −1 8
9 0 162 9 0 0 9
9 19 181 10 −1 −1 9
10 0 182 10 0 0 10
10 16 198 11 −1 −1 10
11 0 199 11 0 0 11
11 18 217 12 −1 −1 11
12 0 218 12 0 0 12
12 16 234 13 −1 −1 12
13 0 235 13 0 0 13
13 16 251 14 −1 −1 13
14 0 252 14 0 0 14
14 21 273 15 −1 −1 14
15 0 274 15 0 0 15
15 16 290 16 −1 −1 15

1.7. Very Unequal Month Lengths

The preceding methods for calculating the calendar date from the running day number for calendars with very unequal month lengths assumed that the variation in month lengths is sufficiently small that the month number calculated with the corresponding simple calendar is at most one off: \( 2\dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_{r_i \lt 0} r_i} − q − 1 − 2\dparen{\sum_i r_iψ_i} ≤ 0 \). What to do if that condition is not met?

In that case you'll have to find the correct month by searching for it. First try month number \( m_↑ \) and calculate the corresponding \( d_↑ \) and \( ξ \). Is that \( ξ \lt 0 \)? Then subtract 1 from \( m_↑ \) and try again, until \( ξ = 0 \): then you have found the correct \( m \).

Warning! In previous sections we added \( ξ \) to \( m_↑ \) to correct the month number, but here we are not certain that that could never produce a month number that is too small. A month number that is too large is easy to detect (then \( d_↑ \lt 0 \)) but a month number that is too small is not easy to detect. So we must not add \( ξ \). If \( ξ \lt 0 \), then subtract 1 from the month number and try again with that new month number, and repeat until \( ξ = 0 \).

Let \( q = 3 \), \( r = 7 \), \( ψ = 1/3 \), then \( h = 1 \), \( g = 3 \), \( f = 16 \), \( p = q + rψ = 3 + 5/3 = 5 \frac{2}{3} \), and \( ρ = \dfloor{2×5\frac{2}{3}} + 1 − 10 = 2 \) so the condition \( r ≤ ρ \) is not satisfied. In this calendar the first three months have length \( 3, 3, 10 \) days, and that pattern repeats every three months. For this calendar, we have

\begin{align*} m_↑ \| = \dfloorratio{3s + 20}{16} \\ σ \| = 3m + 7\dfloorratio{m}{3} \end{align*}

We find

\({s}\) \({m_↑}\) \({σ_↑}\) \({d₁}\) \({ξ₁}\) \({m₂}\) \({σ₂}\) \({d₂}\) \({ξ₂}\) \({m₃}\) \({σ₃}\) \({d₃}\) \({ξ₄}\) \({m}\) \({d}\)
0 1 3 −3 −1 0 0 0 0 0 0
1 1 3 −2 −1 0 0 1 0 0 1
2 1 3 −1 −1 0 0 2 0 0 2
3 1 3 0 0 1 0
4 2 6 −2 −1 1 3 1 0 1 1
5 2 6 −1 −1 1 3 2 0 1 2
6 2 6 0 0 2 0
7 2 6 1 0 2 1
8 2 6 2 0 2 2
9 2 6 3 0 2 3
10 3 16 −6 −1 2 6 4 0 2 4
11 3 16 −5 −1 2 6 5 0 2 5
12 3 16 −4 −1 2 6 6 0 2 6
13 3 16 −3 −1 2 6 7 0 2 7
14 3 16 −2 −1 2 6 8 0 2 8
15 4 19 −4 −1 3 16 −1 −1 2 6 9 0 2 9
16 4 19 −3 −1 3 16 0 0 3 0

For example: for \( s = 11 \) we find

\begin{align*} m_1 \| = m_↑ = \dfloorratio{3×11 + 20}{16} = 3 \\ σ_1 \| = σ(m_1) = 3×3 + 7\dfloorratio{3}{3} = 16 \\ d_1 \| = s − σ_1 = 11 − 16 = −5 \end{align*}

That \( d_1 \) is negative, so we decrement \( m \) by one (\( m_2 = m_1 − 1 = 2 \)) and try again. With the new \( m \) we find

\begin{align*} σ_2 \| = 3×2 + 7\dfloorratio{2}{3} = 6 \\ d_2 \| = s − σ_2 = 11 − 6 = 5 \end{align*}

which is no longer negative, so we're done. For \( s = 11 \) we find \( m = 2 \) and \( d = 5 \).

If you want to, then you can use this procedure also if condition \eqref{eq:singlepass} is met.

The greatest number of months that you may have to try before you find the right one is equal to the greatest value that \( m_↑ − m_↓ \) can attain, plus 1.

\begin{align} m_↑ − m_↓ \| = \dceilratio{s + \max(∆σ) + 1 − v}{p} − \dceilratio{s + \min(∆σ) + 1 − v}{p} \\ \| ≤ \dceilratio{\max(∆σ) − \min(∆σ)}{p} + 1 \eqavide{eq:ceildiffrange} \end{align}

so the number of months that you have to try at most is

\begin{equation} N = \dceilratio{\max(∆σ) − \min(∆σ)}{p} + 2 \end{equation}

For a calendar with two month lengths this means

\begin{equation} N = \dceilratio{r − 1}{p} + 2 \end{equation}

and for a calendar with more month lengths

\begin{align} N \| ≤ \dceilratio{\dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_{r_i \lt 0} r_i}}{p} + 2 \\ \| = \dceilratio{\sum_i |r_i|}{p} + 2 \end{align}

1.8. Month Lengths Without Internal Patterns

Previously we looked at calendars of which the month lengths can be expressed as the sum of various patterns, for example with an extra day every second month and two additional days every third month, so that each sixth month gets three extra days in total. But what if the months repeat after some time but show no other discernable pattern?

As an example we use a calendar with the following month lengths, that keep repeating themselves indefinitely: 7, 13, 5, 11, 4 days. The "year" is 7 + 13 + 5 + 11 + 4 = 40 days long. The relationship between running day number \( s \), running month number \( m \), and day number \( d \) in the current month (all starting at 0) is then

\({s}\) \({m}\) \({d}\)
0 0 0
1 0 1
6 0 6
7 1 0
19 1 12
20 2 0
24 2 4
25 3 0
35 3 10
36 4 0
39 4 3

The calendar has \( f \) days in \( g \) months, for an average month length of \( p = f/g \) days. The relationship between the running day number \( s \) and the running month number \( m \) is a step function: after a (varying) number of days, the month number increases by one. Such a step of 1 that occurs just before day \( a \) and repeats itself every \( f \) days can be obtained through the formula

\begin{equation} \dfloorratio{s − a}{f} + 1 = \dfloorratio{s + f − a}{f} \end{equation}

The \( + 1 \) arranges that the result is 0 for \( s \lt a \) and 1 for \( s ≥ a \).

For the example calendar we have \( f = 40 \) and \( g = 5 \) and there is (amongst others) a step just before day \( a = 7 \). We get that with

\[ m = \dfloorratio{s − 7}{40} + 1 = \dfloorratio{s + 33}{40} \]

\({s}\) \({s−7}\) \({m}\)
0 −7 0
1 −6 0
6 −1 0
7 0 1
8 1 1
46 39 1
47 40 2
48 41 2

When there are \( g \) months, each with its own \( a_i \) (for \( i \) from 0 through \( g − 1 \)), then their combined effect is

\begin{equation} \sum_{i=0}^{g−1} \dparen{\dfloorratio{s − a_i}{f} + 1} = g + \sum_{i=0}^{g−1} \dfloorratio{s − a_i}{f} = \sum_{i=0}^{g−1} \dfloorratio{s + f − a_i}{f} \end{equation}

As usual we want that month \( m = 0 \) begins when \( s = 0 \), so then we must have \( a_0 = 0 \).

If the successive month lengths are \( L_i \), then the first step is at \( a_0 = 0 \), the second one is at \( a_1 = a_0 + L_0 = L_0 \), the third one is at \( a_2 = a_1 + L_1 = L_0 + L_1 \), and in general

\begin{equation} a_i = \sum_{j=0}^{i-1} L_j \end{equation}

Then

\begin{equation} m = \sum_{i=0}^{g−1} \dfloorratio{s + f − \sum_{j=0}^{i−1} L_j}{f} \label{eq:willekeurigynaarx} \end{equation}

For the example calendar we have \( f = 40 \), \( g = 5 \), and

\({i}\) \({L_i}\) \({a_i}\)
1 7 0
2 13 7
3 5 20
4 11 25
5 4 36

and so

\begin{align*} m \| = \dfloorratio{s + 40 − 0}{40} + \dfloorratio{s + 40 − 7}{40} + \dfloorratio{s + 40 − 20}{40} + \dfloorratio{s + 40 − 25}{40} + \dfloorratio{s + 40 − 36}{40} \\ \| = \dfloorratio{s + 40}{40} + \dfloorratio{s + 33}{40} + \dfloorratio{s + 20}{40} + \dfloorratio{s + 15}{40} + \dfloorratio{s + 4}{40} \end{align*}

For example, if \( s = 21 \) then

\[ m = \dfloorratio{61}{40} + \dfloorratio{54}{40} + \dfloorratio{41}{40} + \dfloorratio{36}{40} + \dfloorratio{25}{40} = 1 + 1 + 1 + 0 + 0 = 3 \]

If we go in the other direction then we also have a staircase, but now days and months exchange roles as length and height of the steps. With that, the formula for the running day number \( σ \) of the first day of month \( m \) is:

\begin{equation} σ = \sum_{i=0}^{g−1} L_i \dfloorratio{m + g − 1 − i}{g} \end{equation}

With again the same calendar, we find

\[ σ = 7 \dfloorratio{m + 4}{5} + 13 \dfloorratio{m + 3}{5} + 5 \dfloorratio{m + 2}{5} + 11 \dfloorratio{m + 1}{5} + 4 \dfloorratio{m}{5} \]

\({x}\) \({7⌊(x+4)/5⌋}\) \({{13⌊(x+3)/5⌋}}\) \({5⌊(x+2)/5⌋}\) \({11⌊(x+1)/5⌋}\) \({4⌊x/5⌋}\) \({c}\)
0 0 0 0 0 0 0
1 7 0 0 0 0 7
2 7 13 0 0 0 20
3 7 13 5 0 0 25
4 7 13 5 11 0 36
5 7 13 5 11 4 40

The formula to go from running month number \( m \) and day number \( d \) in the current month to running day number \( s \) (all beginning at 0) is then (with \( i \) running from 0 through \( g − 1 \))

\begin{equation} s = σ + d = \sum_{i=0}^{g−1} m_i \dfloorratio{m + g − 1 − i}{g} + d \label{willekeurigxnaary} \end{equation}

These formulas are, if you write out the summation, a lot longer than the formulas that we found earlier for calendars with internal patterns, so it is convenient if you recognize such patterns for a calendar, but not every calendar has such patterns.

The simple calendar from section 1.3 has \( s = \dfloor{pm + v} + d \). To have that calendar begin month \( m \) at running day \( s \) we need \( s = \dfloor{pm + v} \), from which follows \( s ≤ pm + b \lt s + 1 \), hence (for \( m \gt 0 \)) \( (s − b)/m ≤ p \lt (s + 1 − b)/m \). We know that \( 0 ≤ b \lt 1 \), so

\[ \frac{s − 1}{m} \lt \frac{s − b}{m} \le p \lt \frac{s + 1 − b}{p} \le \frac{s + 1}{p} \]

so

\begin{equation} \frac{s − 1}{m} \lt p \lt \frac{s + 1}{m} \label{eq:beperkp} \end{equation}

Not every \( p \) that meets these restrictions yields a simple calendar, but if a \( p \) does not meet these restrictions for at least one of its months, then there is certainly no simple calendar for these month lengths.

Our example calendar does not meet the restrictions that are necessary for the simple formulas from section 1.3 to apply, because there are more than two different month lengths, even if we leave the last month out of consideration.

We can also see this using equation \eqref{eq:beperkp}. The first day of month \( m = 1 \) has \( s = 7 \), so we must have \( 6 \lt p \lt 8 \). The first day of month \( m = 2 \) has \( s = 20 \) so we need \( 9\frac{1}{2} = 19/2 \lt p \lt 21/2 = 10\frac{1}{2} \), and already we're in trouble, because to have the beginning of month \( x = 1 \) in the right place \( p \) must be less than 8, but to have the beginning of month \( x = 2 \) in the right place \( p \) must be greater than 9½, and those restrictions cannot be met at the same time.

1.9. Combinations of straight lines

It is rare for a calendar to be fully defined by just one period \( p \), so usually you have to combine several periods. Let's assume that we have formulas for two periods:

\begin{align} y_1 \| = D_1(\{ x_1, z_1 \}) \\ y_2 \| = D_2(\{ x_2, z_2 \}) \end{align}

We don't use \( m \), \( d \), and \( s \) here because we may want to use different units of time than months and days. \( x \) counts the larger unit, and \( y \) and \( z \) count the smaller unit. \( y \) is the running number of smaller units, and \( z \) is the number of smaller units since the beginning of the most recent larger unit. Mapping \( D_1 \) says that you can calculate \( y_1 \) from \( x_1 \) and \( z_1 \), and likewise for mapping \( D_2 \).

In the other direction we have

\begin{align} \{ x_1, z_1 \} \| = U_1(y_1) \\ \{ x_2, z_2 \} \| = U_2(y_2) \end{align}

where \( U \) and \( D \) are each other's inverse:

\begin{align} y \| = D(U(y)) \\ \{ x, z \} \| = U(D(\{ x, z \})) \end{align}

To go from calendar date to running day number we first apply \( D_1 \) and then \( D_2 \). There are two ways to combine these: We can equate \( y_1 \) to \( z_2 \) or to \( x_2 \). In the first case, the two periods have the same leap unit, for example days. In the second case, the two periods have different leap units (for example, sometimes an extra day for the first period, and sometimes an extra month for the second period). If the larger period needs no leap rules at all, then you can choose which combination method to use.

Let's call the first case the "flat" combination, because the leap units remain the same. Let's call the second case the "stepped" combination, because the leap unit goes a step higher.

The combination of months and years in most (perhaps all) solar calendars (such as the Gregorian calendar, the Julian calendar, and the Egyptian calendar) is flat, i.e. of the first kind. A month can be a day shorter or longer than another month (and exactly one month can be a lot shorter), and a year can be a day shorter or longer than another year. The number of days in a year does not depend on the months, because the rules to calculate the length of the year depend on the year number but not on the month number.

The combination of months and years in a lunisolar calendar (such as the Hebrew and Babylonian calendars) can be flat with very unequal year lengths (\( r \gt 1 \)) or stepped (then usually \( r = 1 \)). A month can be a day longer or shorter than another month, and a year can be a month shorter or longer than another month. (For the flat combination, one month of the year can be much shorter.) In this way you can follow two separate (astronomical) cycles: the motion of the Sun (with the year) and the motion of the Moon (with the month). If the combination is stepped, then the length of the year depends on the length of the months, because the year is then defined in terms of a fixed number of months, not a fixed number of days.

Lunar calendars that are not lunisolar (such as the administrative Islamic calendar) usually do not have any leap rules, so then both methods can be used.

If a calendar has more than two important large periods with leap rules (for example, not just for the month and the year, but also for the century), then it is possible that some combinations are flat and others are stepped.

If we have to deal with more than one period, then it is important for translating a running day number into a calendar date to have the running numbers begin at 0 for each of those periods.

As an example we'll take for the first calendar a simple one with \( p_1 = 7/3 = 2 \frac{2}{3} \) and for the second calendar a simple one with \( p_2 = 37/5 = 7 \frac{2}{5} \). Then we have

\begin{align*} y_1 \| = \dfloorratio{7x_1}{3} + z_1 = σ_1 + z_1 \\ x_1 \| = \dfloorratio{3y_1 + 2}{7} \\ z_1 \| = \dfloorratio{⌊3y_1 + 2⌉_7}{3} \end{align*}

\begin{align*} y_2 \| = \dfloorratio{37x_2}{5} + z_2 = σ_2 + z_2 \\ x_2 \| = \dfloorratio{5y_2 + 4}{37} \\ z_2 \| = \dfloorratio{⌊5y_2 + 4⌉_{37}}{5} \end{align*}

and also

\begin{align*} \{x_1, r_1\} \| = \Div(3y_1 + 2, 7) \\ z_1 \| = \dfloorratio{r_1}{3} \\ \{x_2, r_2\} \| = \Div(5y_2 + 4, 37) \\ z_2 \| = \dfloorratio{r_2}{5} \end{align*}

1.9.1. Flat combination

In this case \( z_2 = y_1 \), so

\begin{align} y_1 \| = D_1\dparen{\{x_1, z_1\}} \\ y_2 \| = D_2\dparen{\{x_2, z_2\}} = D_2\dparen{\{x_2, D_1\dparen{\{x_1, z_1\}}\}} \label{eq:vlak} \end{align}

and in the other direction

\begin{align} \{ x_2, z_2 \} \| = U_2(y_2) \label{eq:vlakr} \\ \{ x_1, z_1 \} \| = U_1(z_2) \end{align}

or, with a diagram

  x₂ ──────────┐
  x₁ ┐         │
  z₁ ┴ y₁ = z₂ ┴ y₂

The lowest row has the smallest calendar unit, usually days. Higher rows have greater calendar units, for example months and years. \( x_2 \) could be the year number, \( x_1 \) the month number within the year, \( z_1 \) the day number within the month, \( y_1 \) the day number within the year, and \( y_2 \) the running day number.

To calculate the calendar date from the running day number, we must first calculate the larger period (\( x_2 \), \( z_2 \)), and then the smaller period (\( x_1 \), \( z_1 \)).

The two example calendars then yield, for the calculation of \( x_2, x_1, z_1 \) from \( y_2 \) for the first 20 days:

\({y_2}\) \({x_2}\) \({r_2}\) \({z_2=y_1}\) \({x_1}\) \({r_1}\) \({z_1}\) \({\{x_2,x_1,z_1\}}\)
0 0 4 0 0 2 0 {0, 0, 0}
1 0 9 1 0 5 1 {0, 0, 1}
2 0 14 2 1 1 0 {0, 1, 0}
3 0 19 3 1 4 1 {0, 1, 1}
4 0 24 4 2 0 0 {0, 2, 0}
5 0 29 5 2 3 1 {0, 2, 1}
6 0 34 6 2 6 2 {0, 2, 2}
7 1 2 0 0 2 0 {1, 0, 0}
8 1 7 1 0 5 1 {1, 0, 1}
9 1 12 2 1 1 0 {1, 1, 0}
10 1 17 3 1 4 1 {1, 1, 1}
11 1 22 4 2 0 0 {1, 2, 0}
12 1 27 5 2 3 1 {1, 2, 1}
13 1 32 6 2 6 2 {1, 2, 2}
14 2 0 0 0 2 0 {2, 0, 0}
15 2 5 1 0 5 1 {2, 0, 1}
16 2 10 2 1 1 0 {2, 1, 0}
17 2 15 3 1 4 1 {2, 1, 1}
18 2 20 4 2 0 0 {2, 2, 0}
19 2 25 5 2 3 1 {2, 2, 1}

Running day number \( y_2 = 16 \) corresponds to day \( z_1 = 0 \) of month \( x_1 = 1 \) of year \( x_2 = 2 \).

1.9.2. Stepped combination

In this case \( x_2 = y_1 \), so

\begin{align} y_1 \| = D_1\dparen{\{x_1, z_1\}} \\ y_2 \| = D_2\dparen{\{x_2, z_2\}} = D_2\dparen{D_1\dparen{\{x_1, z_1\}, z_2}} \end{align}

and in the other direction

\begin{align} \{x_2, z_2\} \| = U_2(y_2) \\ \{x_1, z_1\} \| = U_1(x_2) \end{align}

or, with a diagram

  x₁ ┐
  z₁ ┴ y₁ = x₂ ┐
  z₂ ──────────┴ y₂

Here \( x_1 \) could be the year number, \( z_1 \) the month number within the year, \( z_2 \) the day number within the month, \( y_1 \) the running month number, and \( y_2 \) the running day number.

With our two example calendars we find

\({y_2}\) \({x_2=y_1}\) \({r_2}\) \({z_2}\) \({x_1}\) \({r_1}\) \({z_1}\) \({\{x_1,z_1,z_2\}}\)
0 0 4 0 0 2 0 {0, 0, 0}
1 0 9 1 0 2 0 {0, 0, 1}
2 0 14 2 0 2 0 {0, 0, 2}
3 0 19 3 0 2 0 {0, 0, 3}
4 0 24 4 0 2 0 {0, 0, 4}
5 0 29 5 0 2 0 {0, 0, 5}
6 0 34 6 0 2 0 {0, 0, 6}
7 1 2 0 0 5 1 {0, 1, 0}
8 1 7 1 0 5 1 {0, 1, 1}
9 1 12 2 0 5 1 {0, 1, 2}
10 1 17 3 0 5 1 {0, 1, 3}
11 1 22 4 0 5 1 {0, 1, 4}
12 1 27 5 0 5 1 {0, 1, 5}
13 1 32 6 0 5 1 {0, 1, 6}
14 2 0 0 1 1 0 {1, 0, 0}
15 2 5 1 1 1 0 {1, 0, 1}
16 2 10 2 1 1 0 {1, 0, 2}
17 2 15 3 1 1 0 {1, 0, 3}
18 2 20 4 1 1 0 {1, 0, 4}
19 2 25 5 1 1 0 {1, 0, 5}

Running day number \( y_2 = 16 \) corresponds to day \( z_2 = 2 \) of month \( z_1 = 0 \) of year \( x_1 = 1 \).

1.10. Simultaneous Cycles

Most calendars have higher and lower periods, and the number of a higher period changes much less often than the number of a lower period. After the 7th day of the 3rd month follows the 8th day of the 3rd month − the month number changes less often than the day number.

Some calendars have different periods that change simultaneously. The most common calendars often have such a case, too, in the form of weekdays and days of the month. When the next day arrives, then the day number of the month changes, but the weekday changes, too. After Monday the 7th we get Tuesday the 8th − the weekday and the day number change equally fast.

We don't need the weekday to be able to point to a unique day (30 August 2011 indicates exactly one day; for that we do not need to know that that day was a Tuesday), so the weekday does not usually play a role in calendar calculations. However, there are calendars (for example the calendars of Central America) which do use simultaneous cycles to point at particular days. We look at this type of calendar below.

Suppose that a calendar uses simultaneous periods \( p_i \) (all of them whole numbers greater than 1) for \( i \) from 1 through \( n \). The day number in each period begins at 0. We write a date in that calendar as \( \{x\} = \{x_1,x_2,…,x_n\} \), where \( x_i \) is the day number from period \( i \). If \( x_i \) is equal to \( p_i − 1 \), then the next day has \( x_i = 0 \) again.

For example, in a calendar with \( p_1 = 13 \) and \( p_2 = 20 \), we get after day \( \{11,8\} \) the following days: \( \{12,9\} \), \( \{0,10\} \), \( \{1,11\} \), …, \( \{9,19\} \), \( \{10,0\} \), \( \{11,1\} \).

1.10.1. From Running Day Number to Date

To translate running day number \( s \) into \( \{x\} \) we can use the following formula:

\begin{equation} x_i = ⌊s + a_i⌉_{p_i} = (s + a_i) \bmod p_i \label{eq:cycli} \end{equation}

Here \( a_i \) is the value of \( x_i \) when \( s = 0 \). That value depends on the calendar.

Suppose that in the calendar from the previous example \( s = 0 \) corresponds to \( \{11, 8\} \). Then \( a_1 = 11 \) and \( a_2 = 8 \), and then

\begin{align*} x_1 = \dmod{s + 11}{13} \\ x_2 = \dmod{s + 8}{20} \end{align*}

Day \( s = 11 \) then corresponds to

\begin{align*} x_1 \| = \dmod{11 + 11}{13} = \dmod{22}{13} = 9 \\ x_2 \| = \dmod{11 + 8}{20} = \dmod{19}{20} = 19 \end{align*}

so the date is \( \{9, 19\} \).

A few more examples:

\({s}\) \({x_1}\) \({x_2}\)
0 11 8
1 12 9
2 0 10
3 1 11
10 8 18
11 9 19
12 10 0
13 11 1

1.10.2. From Date to Running Day Number

It is a lot more difficult to go in the other direction. Then we have to find a \( s \) for which equation \eqref{eq:cycli} is satisfied for all \( i \), i.e.,

\begin{equation} s ≡ x_i − a_i \pmod{p_i} \end{equation}

We define

\begin{equation} c_i = x_i − a_i \end{equation}

We first look at the case \( n = 2 \). Then we need to solve \( s \) from

\begin{align} s \| ≡ c_1 \pmod{p_1} \label{eq:c1} \\ s \| ≡ c_2 \pmod{p_2} \label{eq:c2} \end{align}

There are only solutions if

\begin{equation} c_1 ≡ c_2 \pmod{g} \end{equation}

where

\begin{equation} g = \gcd(p_1, p_2) \end{equation}

is the greatest common divisor of \( p_1 \) and \( p_2 \), so we assume that that condition is met. Then there are numbers \( n_1, n_2 \) for which

\begin{equation} g = n_1 p_1 + n_2 p_2 \end{equation}

and then the solutions are

\begin{equation} s ≡ \dfrac{c_1 n_2 p_2 + c_2 n_1 p_1}{g} \pmod{\dfrac{p_1 p_2}{g}} \label{eq:cyclitoj} \end{equation}

The numerators of these ratios have a divisor equal to \( g \) so these divisions always yield whole numbers.

We again use the calendar from the previous example, with \( p_1 = 13 \), \( p_2 = 20 \), \( a_1 = 11 \) and \( a_2 = 8 \). The greatest common divisor of 13 and 20 is 1, so \( g = 1 \). Now we seek a solution of

\[ 1 = 13 n_1 + 20 n_2 \]

You can find \( g, n_1, n_2 \) using the Extended Euclidean Algorithm. You can also find \( n_1, n_2 \) by trying successive values of \( n_1 \) (or \( n_2 \)) until you find one that fits. You've found a correct \( n_1 \) when

\[ \dmod{13 n_1 − 1}{20} = 0 \]

If you begin with \( n_1 = 1 \) and increment it in steps of 1 then you need to try at most \( n_2 − 1 \) different values.

\({n_1}\) \({\dmod{13n_1 − 1}{20}}\)
1 12
2 5
3 18
4 11
5 4
6 17
7 10
8 3
9 16
10 9
11 2
12 15
13 8
14 1
15 14
16 7
17 0
18 13
19 6
20 19
21 12

We see that \( n_1 = 17 \) is a solution. Then

\[ n_2 = \dfrac{1 − 13 n_1}{20} = \dfrac{1 − 13×17}{20} = −11 \]

so

\[ 1 = 17×13 − 11×20 = 221 − 220 \]

Then the running day numbers are

\begin{align*} s \| ≡ \dfrac{c_1n_2p_2 + c_2n_1p_1}{g} \pmod{\dfrac{p_1p_2}{g}} \\ \| ≡ \dfrac{(x_1 − 11)×(−220) + (x_2 − 8)×221}{1} \pmod{\dfrac{13×20}{1}} \\ \| ≡ −220 x_1 + 221 x_2 + 11×220 − 8×221 \\ \| ≡ −220 x_1 + 221 x_2 + 652 \pmod{260} \end{align*}

Because the preceding formula has \( \pmod{260} \) you can add an arbitrary multiple of 260 to every term, so if you don't want negative numbers in the formula then you can rewrite it to

\begin{align*} s \| ≡ (260 − 220) x_1 + 221 x_2 + (652 − 2×260) \\ \| ≡ 40 x_1 + 221 x_2 + 132 \pmod{260} \end{align*}

For date \( \{9,19\} \) we then find

\[ s ≡ 40×9 + 221×19 + 132 ≡ 4691 ≡ 11 \pmod{260} \]

so day \( s = 11 \) and every 260 days earlier or later correspond to date \( \{9, 19\} \).

Warning! Formula \eqref{eq:cyclitoj} only gives useful results for \( \{x\} \) that really occur in the calendar. If \( g \) is not equal to 1, then not all possible \( \{x\} \) can occur. If you enter an \( \{x\} \) that does not occur in the calendar, then you get a reasonable-looking result from the formula, but that result then still won't be valid.

Now we look at a calendar for which the greatest common divisor \( g \) of the periods is not 1, with \( p_1 = 10 \), \( p_2 = 15 \), \( a_1 = a_2 = 0 \). Then

\begin{align*} x_1 = \dmod{s}{10} \\ x_2 = \dmod{s}{15} \end{align*}

A solution for

\[ g = n_1 p_1 + n_2 p_2 \]

is

\[ 5 = −1×10 + 1×15 \]

so

\[ s ≡ \dfrac{15x_1 − 10x_2}{5} ≡ 3x_1 − 2x_2 \pmod{30} \]

\({s}\) \({x_1}\) \({x_2}\) \({3x_1−2x_2}\) \({\dmod{3x_1−2x_2}{30}}\)
0 0 0 0 0
9 9 9 9 9
10 0 10 −20 10
14 4 14 −16 14
15 5 0 15 15
19 9 4 19 19
20 0 5 −10 20
29 9 14 −1 29
30 0 0 0 0

Because \( g \) is not equal to 1, many combinations of \( x_1 \) and \( x_2 \) do not occur in this calendar. Only combinations for which \( x_1 − x_2 \) is evenly divisible by 5 occur in this calendar. For example, \( \{3, 7\} \) does not occur. If we apply the equation for \( s \) to that date, then we find \( s ≡ 3×3 − 2×7 ≡ −5 ≡ 25 \pmod{30} \), but for \( s = 25 \) we find \( x_1 = \dmod{25}{10} = 5 \) and \( x_2 = \dmod{25}{15} = 10 \), so \( \{5, 10\} \) and not \( \{3, 7\} \). For impossible \( \{x\} \) we still get nice-looking \( s \) out of the formula.

1.10.3. More than Two Periods

Using equation \eqref{eq:cyclitoj}, \( s ≡ c_1 \pmod{p_1} \) and \( s ≡ c_2 \pmod{p_2} \) lead to another equation \( s ≡ C_2 \pmod{P_2} \) with

\begin{align} C_2 \| ≡ \dfrac{c_1n_2p_2 + c_2n_1p_1}{\gcd(p_1, p_2)} \\ P_2 \| ≡ \dfrac{p_1p_2}{\gcd(p_1, p_2)} \end{align}

That formula has the same form as the two formulas that we started with, so we can combine that formula in the same manner with \( s ≡ c_3 \pmod{p_3} \), and so on until we've handled all \( p_i \). We end up with a formula that again has the same form \( s ≡ C_n \pmod{P_n} \), and that is the solution of the \( n \) equations.

The procedure is then as follows. Given \( x_i \), \( p_i \), \( a_i \) for \( i \) from 1 through \( n \),

  1. Assign

    \begin{align*} C_1 \| = x_1 − a_1 \\ P_1 \| = p_1 \end{align*}

  2. For \( i \) from 2 through \( n \):

    1. Determine \( g_i = \gcd(P_{i−1}, p_i) \), the greatest common divisor of \( P_{i−1} \) and \( p_i \).

    2. Find \( n_i, m_i \) such that

      \[ g_i = n_i P_i + m_i p_i \]

    3. Assign

      \begin{align*} C_i \| = \dfrac{C_{i−1} m_i p_i + c_i n_i P_{i−1}}{g_i} \\ P_i \| = \dfrac{P_{i−1}p_i}{g_i} \end{align*}

  3. The solution is

    \begin{equation} s ≡ C_n \pmod{P_n} \label{eq:cycle} \end{equation}

Let's look at a calendar with the following characteristics:

\({i}\) \({p_i}\) \({a_i}\)
1 4 3
2 5 1
3 6 2

You translate running day number \( s \) to calendar date \( \{x\} \) as follows:

\begin{align*} x_1 \| = \dmod{s + 3}{4} \\ x_2 \| = \dmod{s + 1}{5} \\ x_3 \| = \dmod{s + 2}{6} \end{align*}

In the opposite direction we find:

\begin{align*} P_1 \| = 4 \\ C_1 \| ≡ x_1 − 3 \pmod{P_1 = 4} \\ c_2 \| = x_2 − 1 \\ g_2 \| = \gcd(4, 5) = 1 = −1×4 + 1×5 = m_2P_1 + n_2p_2 \\ P_2 \| = \dfrac{P_1p_2}{g_2} = \dfrac{4×5}{1} = 20 \\ C_2 \| ≡ \dfrac{C_1 n_2 p_2 + c_2 m_2 P_1}{g_2} \\ \| ≡ 5 (x_1 − 3) − 4 (x_2 − 1) \\ \| ≡ 5 x_1 − 4 x_2 − 11 \\ \| ≡ 5 x_1 + 16 x_2 + 9 \pmod{P_2 = 20} \\ g_3 \| = \gcd(20, 6) = 2 = 1×20 − 3×6 = m_3P_2 + n_3p_3 \\ P_3 \| = \dfrac{P_2p_3}{g_3} = \dfrac{20×6}{2} = 60 \\ C_3 \| ≡ \dfrac{C_2 n_3 p_3 + c_3 m_3 P_2}{g_3} \\ \| ≡ −9 (5 x_1 + 16 x_2 + 9) + 10 (x_3 − 2) \\ \| ≡ ―45 x_1 − 144 x_2 + 10 x_3 − 101 \\ \| ≡ 15 x_1 + 36 x_2 + 10 x_3 + 19 \pmod{P_3 = 60} \end{align*}

The solution is

\[ s ≡ C_3 ≡ 15x_1 + 36x_2 + 10x_3 + 19 \pmod{60} \]

\({s}\) \({x_1}\) \({x_2}\) \({x_3}\) \({C_2}\) \({C_3}\)
0 3 1 2 0 0
1 0 2 3 1 1
2 1 3 4 2 2
3 2 4 5 3 3
4 3 0 0 4 4
5 0 1 1 5 5
6 1 2 2 6 6
7 2 3 3 7 7
8 3 4 4 8 8
9 0 0 5 9 9
10 1 1 0 10 10
11 2 2 1 11 11
12 3 3 2 12 12
13 0 4 3 13 13
14 1 0 4 14 14
27 2 3 5 7 27
28 3 4 0 8 28
29 0 0 1 9 29
30 1 1 2 10 30
31 2 2 3 11 31
654 1 0 2 14 54

For example, if \( s = 654 \), then \( x_1 = \dmod{654 + 3}{4} = 1 \), \( x_2 = \dmod{654 + 1}{5} = 0 \), \( x_3 = \dmod{654 + 2}{6} = 2 \). And from \( \{x\} \) to \( s \) we find

\[ s ≡ 15×1 + 36×0 + 10×2 + 19 ≡ 54 \pmod{60} \]

which is correct, because \( 54 ≡ 654 \pmod{60} \).

1.10.4. To One Solution

Because the date \( \{x\} \) is given as a collection of positions in repeating periods, there is an infinite number of days that fit that date. For the running day number \( s \) that corresponds to date \( \{x\} \) we find formula \eqref{eq:cycle}, which gives a solution \( \bmod P_n \). If you have found a running day number \( s \) that fits date \( \{x\} \), then you can add or subtract arbitrary multiples of \( P_n \) and then you find another \( s \) that belongs to date \( \{x\} \). How do we pick the right one?

To pick the right solution out of an infinite collection of solutions, we need to use additional information. One appropriate way is to pick the solution that is closest to a date \( J_0 \) chosen by us. One can interpret "closest to" in at least four different ways:

We investigate these cases one by one, for \( s ≡ C_n \pmod{P_n} \).

1.10.4.1. The last one at or before

We seek the last \( s \) at or before \( s_0 \) that satisfies \( s ≡ C_n \pmod{P_n} \). Then, with \( k \) a whole number,

\begin{align} s_0 − P_n \| \lt s ≤ s_0 \\ s_0 − P_n \| \lt C_n + kP_n ≤ s_0 \\ \frac{s_0 − C_n}{P_n} − 1 \| \lt k ≤ \frac{s_0 − C_n}{P_n} \end{align}

There is only one whole number \( k \) that satisfies this inequality, and that is

\begin{equation} k = \dfloorratio{s_0 − C_n}{P_n} \end{equation}

so

\begin{align} s \| = C_n + P_n \dfloorratio{s_0 − C_n}{P_n} \notag \\ \| = C_n + P_n \dparen{\frac{s_0 − C_n}{P_n} − \mod1ratio{s_0 − C_n}{P_n}} \notag \\ \| = C_n + (s_0 − C_n) − P_n \mod1ratio{s_0 − C_n}{P_n} \notag \\ \| = s_0 − \dmod{s_0 − C_n}{P_n} \notag \\ \| = s_0 − ((s_0 − C_n) \bmod P_n) \label{eq:nearestle} \end{align}

For example, what is the last day at or before \( s_0 = 700 \) that corresponds to date \( \{1,0,2\} \) in the calendar from the previous example?

There we found \( C_n = 15x_1 + 36x_2 + 10x_3 + 19 \) and \( P_n = 60 \), so \( C_n = 15×1 + 36×0 + 10×2 + 19 = 54 \). With formula \eqref{eq:nearestle} we then find \( s = 700 − \dmod{700 − 54}{60} = 700 − \dmod{646}{60} = 700 − 46 = 654 \) which is correct: 654 + 60 = 714 is greater than 700, so 654 is the last \( s \) that is less than or equal to \( s_0 \) and that fits date \( \{1,0,2\} \).

We should find \( s = 654 \) for all \( s_0 \) from 654 through 654 + 60 − 1 = 713, and we do:

\({s_0}\) \({s_0−C_n}\) \({\dmod{s_0−C_n}{P_n}}\) \({s}\)
653 599 59 594
654 600 0 654
712 658 58 654
713 659 59 654
714 660 0 714

1.10.4.2. The first at or after

We seek the first \( s \) at or after \( s_0 \) that satisfies \( s ≡ C_n \pmod{P_n} \). Then, with \( k \) a whole number,

\begin{align} s_0 \| ≤ s \lt s_0 + P_n \\ s_0 \| ≤ C_n + kP_n \lt s_0 + P_n \\ \dfrac{s_0 − C_n}{P_n} \| ≤ k \lt \dfrac{s_0 − C_n}{P_n} + 1 \end{align}

There is only one whole number \( k \) that satisifies this inequality, and that number is

\begin{equation} k = \dceilratio{s_0 − C_n}{P_n} \end{equation}

so

\begin{align} s \| = C_n + P_n \dceilratio{s_0 − C_n}{P_n} \notag \\ \| = C_n + P_n \dparen{\dfrac{s_0 − C_n}{P_n} + \dom1ratio{s_0 − C_n}{P_n}} \notag \\ \| = C_n + (s_0 − C_n) + P_n \dom1ratio{s_0 − C_n}{P_n} \\ \| = s_0 + \ddom{s_0 − C_n}{P_n} \\ \| = s_0 + \dmod{C_n − s_0}{P_n} \\ \| = s_0 + ((C_n − s_0) \bmod P_n) \label{eq:nearestge} \end{align}

For example, what is the first day at or after \( s_0 = 700 \) that corresponds to date \( \{1,0,2\} \) in the calendar from the previous example?

We had \( C_n = 54 \). With formula \eqref{eq:nearestge} we then find \( s = 700 + \dmod{54 − 700}{60} = 700 + \dmod{−646}{60} = 700 + 14 = 714 \) which is correct: 714 − 60 = 654 is less than 700, so 714 is the first \( s \) that is greater than or equal to \( s_0 \) and that fits date \( \{1,0,2\} \).

We should find \( s = 714 \) for all \( s_0 \) from 714 − 60 + 1 = 655 through 714, and we do:

\({s_0}\) \({s_0−C_n}\) \({\dmod{C_n−s_0}{P_n}}\) \({s}\)
654 600 0 654
655 601 59 714
656 602 58 714
713 659 1 714
714 660 0 714
715 661 59 774

1.10.4.3. The last one before

The last \( s \) before \( s_0 \) is one \( P_n \) before the first \( s \) at or after \( s_0 \), so

\begin{equation} s = s_0 − P_n − \dmod{s_0 − C_n}{P_n} \end{equation}

1.10.4.4. The first one after

The first \( s \) after \( s_0 \) is one \( P_n \) after the last \( s \) at or before \( s_0 \), so

\begin{align} s \| = s_0 + P_n + ⌈s_0 − C_n⌋_{P_n} \\ \| = s_0 + P_n + ⌊C_n − s_0⌉_{P_n} \end{align}

1.11. Summary

This summary is in terms of days and months, but holds also for other units of time. We distinguish four different degrees of difficulty: from 1 to 4 the formulas get more complicated but also more capable.

  1. The simplest calendar has two different month lengths, with the greatest being one greater than the smallest, and allows no shifting of the pattern of month lengths. Month 0 is always a short month, and month \( g − 1 \) (the last month before everything repeats itself) is always a long month.

  2. Calendar type 2 is equal to type 1, except that shifting of the pattern of month lengths is allowed.

  3. Calendar type 3 is equal to type 2, but now the difference between the month lengths can be more than one.

  4. Calendar type 4 is equal to type 3, but allows more than two month lengths.

The following table summarizes the calendar formulas, and uses the following definitions:

 #     
2 3 4
\({p}\) \({q + \dfrac{h}{g}}\) \({q + r\dfrac{h}{g}}\) \({q + \sum_i \dfrac{r_i h_i}{g_i}}\)
\({f}\) \({qg + h}\) \({qg + rh}\) \({qg + \sum_i γ_i r_i h_i}\)
\({σ}\) \({\dfloorratio{fm + t}{g}}\) \({qm + r\dfloorratio{hm + t}{g}}\) \({qm + \sum_i r_i\dfloorratio{h_i m + t_i}{g_i}}\)
\({ρ}\) \({2r(1 − ψ) − 1 − q}\) \({2\dparen{\sum_{r_i\gt0}r_i} − \dparen{\sum_{r_i\lt0}r_i} − q − 1 − 2\sum_i \dfrac{r_ih_i}{g_i}}\)
\({w}\) \({gs + g − t − 1}\) \({gs + gr − rt − 1}\) \({gs + g\dparen{\sum_{r_i \gt 0}r_i} − \dparen{\sum_i γ_i r_i t_i} − 1}\)
\({m}\) \({\dfloorratio{w}{f}}\) \({\dfloorratio{w}{f}}\) \({\dfloorratio{w}{f}}\)
\({d}\) \({\dfloorratio{\dmod{w}{f}}{g}}\) \({s-qm-r\dfloorratio{hm+t}{g}}\) \({s−qm−\dparen{\sum_ir_i\dfloorratio{h_im+t_i}{g_i}}}\)
\({ξ}\) \({\dfloorratio{d}{q+r}}\) \({\dfloorratio{d}{q+\sum_{r_i \gt 0} r_i}}\)

Calendar type 1 is like calendar type 2 but with \( t = 0 \).

For calendar types 3 and 4, the formula for \( m \) yields an upper boundary for the month number. The general procedure for finding the right month number is then:

Try the \( m \) from the formula in the table. Calculate the corresponding \( σ \) and then \( d = s − σ \) and \( ξ \). If \( ξ \) is equal to 0, then you're done. Otherwise, subtract 1 from \( m \) and try again.

If \( ρ ≤ 0 \) then you need to calculate \( ξ \) at most once. Otherwise you may need to do it more often.

If there are more that two relevant units of time, then different calendar levels must be combined.

2. Derivation for Specific Calendars

Almost all calendars distinguish between three basic periods: days, months, and years. One calendar level (straight line) links two periods, so at least two calendar levels are needed to link three periods.

In the next sections we give a diagram for each calendar calculation. We explain those diagrams based on the following example:

  j ───────────┐ ┌─ a₁ ──────────┐
               (1)               │
  m ─(−1)─ m₀ ─┘ └─ m₁ ─┐        │
  d ─(−1)─ d₀ ─────────(2)─ d₁ ─(3)─ s ─(+J₀)─ J

The calendar date with year, month, day \( j, m, d \) is on the left, and the CJDN \( J \) is on the right. The symbols between parentheses, like (1) and (+J₀), indicate a transformation. The other symbols, like a₁ and d₁, indicate intermediate results. The lines show which intermediate results from one transformation are used in the next transformation. The intermediate results with names with an a in them (like a₁) are measured in years, those with m are measured in months, and those with d, s, or J are measured in days. Names with a c in them (like c₁, not in the above diagram) are measured in centuries of 100 years each. The calculations with larger units are higher up in the diagram than those with smaller units.

The transformations for which there is a plus or minus sign between the parentheses indicate the corresponding increment or decrement when going from left (calendar date) to right (CJDN). So (+J₀) means that you add \( J_0 \). When going from right (CJDN) to left (calendar date) then you do the opposite, so then (+J₀) means that you should subtract \( J_0 \).

For a transformation without a plus or minus sign between the parentheses the number identifies the transformation. More details are given later. Most such transformations (such as transformations 2 and 3 here) transform one number into two, or two numbers into one, and correspond to one of the calendar levels described in the previous chapter. And sometimes there is a transformation (such as transforomation 1 here) that adjusts two numbers, produces two numbers from two numbers. If the number is shown between parentheses () then it represents a calendar level of types 1 or 2. If it is instead shown between square brackets [] then it represents a calendar level of types 3 or 4.

Let's look at the month \( m \). Going from left to right, we first encounter (−1) so 1 is subtracted and yields \( m_0 \). That goes into transformation 1 together with \( j \) which yield \( a_1 \) (measured in years) and \( m_1 \) (measured in months). \( m_1 \) and \( d_0 \) go into transformation 2 and yield \( d_1 \). That one goes into transformation 3 together with \( a_1 \) and yields \( s \). Then \( J_0 \) is added, and we find the end result \( J \).

You can also read the diagram from right to left. We begin with CJDN \( J \) on the right-hand side. Going toward the left we first encounter (+J₀) so we must subtract \( J_0 \) (because we're going from right to left) and get \( s \). That goes into transformation 3 which yields \( a_1 \) and \( d_1 \). That \( d_1 \) goes into transformation 2, yielding \( d_0 \) and \( m_1 \). \( m_1 \) and \( a_1 \) go into transformation 1 which yield \( m_0 \) and \( j \). Then we reach (−1) so we must add 1 (because we're going from right to left) and then we find \( m \).

In the calculations that follow we sometimes use intermediate results that aren't shown in the diagrams. We indicate them using \( α_i \) if they are measured in years, \( μ_i \) if they are measured in months, \( δ_i \) if they are measured in days, and \( ω_i \) if they are measured in a different unit. Those names can be used for different things in the opposite calendar directions, so \( α_1 \) in the description of how to calculate the running day number from the calendar date can refer to something different than \( α_1 \) in the description of how to calculate the calendar date from the running day number.

2.1. A Lunar Calendar With Many Fixed Month Lengths

If the distribution of days across months is independent from the distribution of months across years, then a particular month of the year won't have the same length in all years. For example, the first month of year 1 of the Babylonian calendar has 29 days, but the first month of year 2 has 30 days. For doing calendar calculations in your head, it would be convenient if most months have the same length in each year. Can we construct a lunisolar calendar that has that?

With the characteristics of the Cycle of Meton it is not possible to make each month have the same length in every year that contains it. If we make a fixed number of the first 12 months of every year long (with 30 days), then the corresponding number of long months in the 19-year cycle is a multiple of 19. If the 13th month is always short, then the total number of long months would be a multiple of 19, and if the 13th month is always long, then the total number would be 7 (the number of years in the 19-year cycle that have 13 months) plus a multiple of 19, but the Cycle of Meton requires 125 = 6×19 + 11 long months, which fits neither of the two possibilities, so the length of one or more months cannot be the same in all years.

We want to use the smallest number of different month lengths, which means only months of 29 or 30 days.

If we want to use the calendar calculation techniques discussed before, then it is most convenient if we need only a single calendar level for the calculations between months and days, but then we need both short and long years to use the same straight line and thus to have the same month lengths, apart from the shortening at the very end of the year.

We need 125 long months (of 30 days) and 110 short months (of 29 days) in every period of 19 years = 235 months. If \( n \) of the first 11 months of each year are long then that produces \( 19n \) long months in the period of 19 years, and then \( 125 − 19n \) of the 26 12th and 13th months must be long, so \( 125 − 19n \) must be between 0 and 26 (inclusive), and that means that \( n = 6 \), because \( n = 5 \) yields 30 (too large) and \( n = 7 \) yields −8 (too small).

\( n = 6 \) yields 11 long 12th and 13th months. Those 11 long months cannot all be 13th months because there are only 7 13th months, so some 12th months must be long.

A long month may turn into a short month if it is at the end of the year, but not if at least one more month follows in the same year, so if some 12th months are long then there can only be short 12th months if they are the last month of the year, i.e., when the year has 12 months. So we can have the following kinds of years:

Month 12 Month 13 Year Length
short none 354
long none 355
long short 384
long long 385

There must be 7 long years (of 13 months) and 12 short years (of 12 months) in every 19 years. If those long and short years are both as short as possible (i.e., 354 and 384 days) then that yields 7×384 + 12×354 = 6936 days, so only 4 more days are needed. That means that 4 years must gain a day each, to become 355 or 385 days long. That means that at least three different year lengths are needed, and that cannot be done with a calendar level of types 2 or 3, so we need a calendar level of type 4.

We link the lunisolar calendar to the modern (Gregorian) calendar based on the following considerations:

  1. The calendar year in the lunisolar calendar should be roughly the same as the calendar year in the modern (Gregorian) calendar.

  2. Because this calendar is a lunisolar one, the intention is that every month begins at New Moon, so we want to tie the calendars in a year in which there is a New Moon at the beginning of that year in the modern calendar.

  3. The Cycle of Meton includes 19 years, and we can choose which of those 19 years we use to tie the lunar calendar to the modern calendar. This gives us about a month of leeway in where the average of the start of the lunar year is compared to the start of the year in the modern calendar. We arrange the connection such that the beginning of the lunar year is on average as close as possible to the beginning of winter in the northern hemisphere (December 21).

  4. The Cycle of Meton doesn't follow the average length of lunar months exactly, so in the course of time our lunisolar calendar will inevitably get more and more out of sync with the lunar phases, so the year in which we tie our lunar calendar to the modern one must not be far from now.

I inspected all modern calendar years between 1900 and 2100 and looked for the one in which the lunar phase of the mean Moon at the beginning of the year (0 UTC at the beginning of January 1st) is the closest to New Moon, and that turns out to be the year 2033. So we equate January 1st, 2033 in the modern calendar with the first day of the first month of year 2033 in the lunisolar calendar.

2.1.1. From Lunar Calendar to CJDN

The calculations are schematically as follows:

  j ────────────────────┐
  m ─(−1)─ m₀ ──┐       │
  d ─(−1)─ d₀ ─(1)─ d₁─(2)─ s ─(+J₀)─ J

The summary of the calendar levels is

(n) \({f}\) \({g}\) \({q}\) \({r_i}\) \({h_i}\) \({t_i}\) \({g_i}\) \({γ_i}\) \({p}\)
1 384 13 29 7 7 29.538462
2.1 6940 19 354 30 7 2 19 1 365.26316
2.2 1 4 18 19 1

  1. First we subtract 1 from the month number and day number so they begin at 0 instead of 1.

    \begin{align*} m_0 \| = m − 1 \\ d_0 \| = d − 1 \end{align*}

    As an example we'll calculate the CJDN of the first day of year 2033 in the lunar calendar, so \( \{ j, m, d \} = \{ 2033, 1, 1 \}\). We find

    \begin{align*} m_0 \| = m − 1 = 1 − 1 = 0 \\ d_0 \| = d − 1 = 1 − 1 = 0 \end{align*}

  2. Transformation 1 produces day number \( d_1 \) since the first day of the year, from the calculation month number \( m_0 \) and the calculation day number \( d_0 \):

    \begin{align} δ_1 \| = \dfloorratio{384m_0 + 7}{13} \\ d_1 \| = δ_1 + d_0 \end{align}

    This yields a long month 12 (with 30 days) and a short month 13 (with 29 days), and that 6 of the first 11 months are always long months.

    For example, if \( m_0 = 4 \), then we find

    \[ δ_1 = \dfloorratio{384×4 + 7}{13} = \dfloorratio{1543}{13} = 118 \]

    so the first day of the fifth month (\( m_0 = 4 \)) is the 118th day since the first day of the year.

    For the first day of the first 14 months, we get the following results (with \( L \) the length of the month):

    \({m}\) \({m_0}\) \({δ_1}\) \({L}\)
    1 0 0 30
    2 1 30 29
    3 2 59 30
    4 3 89 29
    5 4 118 30
    6 5 148 29
    7 6 177 30
    8 7 207 29
    9 8 236 30
    10 9 266 29
    11 10 295 30
    12 11 325 30
    13 12 355 29
    14 13 384

  3. Transformation 2 yields the running day number \( s \) from the year \( j \) and the day number \( d_1 \) since the first day of the year.

    \begin{align} δ_2 \| = 354j + 30\dfloorratio{7j + 2}{19} + \dfloorratio{4j + 18}{19} \\ s \| = d_1 + δ_2 \end{align}

    The following table shows the running day numbers for 20 years, and the year length \( L \) for the first 19 of those years. That pattern of year lengths repeats itself every 19 years. There are 8 years of 354 days, 4 years of 355 days, and 7 years of 384 days, which makes 6940 days in 19 years.

    \({j}\) \({δ_2}\) \({L}\)
    0 0 355
    1 355 354
    2 709 384
    3 1093 354
    4 1447 355
    5 1802 384
    6 2186 354
    7 2540 384
    8 2924 354
    9 3278 355
    10 3633 384
    11 4017 354
    12 4371 354
    13 4725 384
    14 5109 355
    15 5464 384
    16 5848 354
    17 6202 354
    18 6556 384
    19 6940

    In our example \( j = 2033, d_1 = 0 \), so

    \begin{align*} δ_2 \| = 354j + 30\dfloorratio{7j + 2}{19} + \dfloorratio{4j + 18}{19} \\ \| = 354×2033 + 30\dfloorratio{7×2033 + 2}{19} + \dfloorratio{4×2033 + 18}{19} \\ \| = 719682 + 30\dfloorratio{14233}{19} + \dfloorratio{8150}{19} \\ \| = 719682 + 30×749 + 428 = 742580 \\ s \| = d_1 + δ_2 = 742580 \end{align*}

  4. And then we add the CJDN \( J_0 \) of day 1 of month 1 of year 0. We link the two calendars at the first day of year 2033. In the modern calendar that day corresponds to CJDN 2463599. In the lunar calendar we found above that for the first day of year 2033 \( s = 742580 \). It follows that \( J_0 = 2463599 − 742580 = 1721019 \).

    \begin{equation} J = s + J_0 = s + 1721019 \end{equation}

    For our example this means

    \[ J = s + J_0 = 742580 + 1721019 = 2463599 \]

    so the CJDN that belongs to the first day of year 2033 in this lunar calendar is 2463599, which corresponds to the first day of year 2033 in the modern (Gregorian) calendar.

These calculations condense to

\begin{equation} J = 354j + 30\dfloorratio{7j + 2}{19} + \dfloorratio{4j + 18}{19} + \dfloorratio{384m − 377}{13} + d + 1721018 \end{equation}

For our example (with \( j = 2033, m = 1, d = 1 \)) we find

\begin{align*} J \| = 354×2033 + 30\dfloorratio{7×2033 + 2}{19} + \dfloorratio{4×2033 + 18}{19} \\ \| + \dfloorratio{384×1 − 377}{13} + 1 + 1721018 \\ \| = 719682 + 30\dfloorratio{14233}{19} + \dfloorratio{8150}{19} + \dfloorratio{7}{13} + 1721019 \\ \| = 2440701 + 30×749 + 428 + 0 = 2463599 \end{align*}

so day 1 of month 1 of year 2033 in the lunar calendar corresponds to CJDN 2463599, which corresponds to January 1st, 2033 in the modern (Gregorian) calendar.

2.1.2. From CJDN to Lunar Calendar

The calculations are schematically as follows:

  j ────────────────────┐
  m ─(−1)─ m₀ ──┐       │
  d ─(−1)─ d₀ ─(1)─ d₁─(2)─ s ─(+J₀)─ J

The summary of the calendar levels is

(n) \({f}\) \({g}\) \({q}\) \({r_i}\) \({h_i}\) \({t_i}\) \({g_i}\) \({γ_i}\) \({p}\)
1 384 13 29 7 7 29.538462
2.2 6940 19 354 30 7 2 19 1 365.26316
2.3 1 4 18 19 1

The calculations are as follows:

  1. First we subtract the CJDN \( J_0 \) of day 1 of month 1 of year 0 from the CJDN of the desired date to find the running day number \( s \):

    \begin{equation} s = J − J_0 = J − 1721019 \end{equation}

    As an example we'll calculate the date that corresponds to CJDN 2459695. Then

    \[ s = J − J_0 = 2459695 − 1721019 = 738676 \]

  2. Transformation 2 deduces from the running day number \( s \) the year number \( j \) and the calculation day number \( d_1 \) since the first day of the year:

    \begin{align} ω_1 \| = gs + g\dparen{\sum_{r_i \gt 0} r_i} − \dparen{\sum_i γ_ir_it_i} − 1 \notag \\ \| = 19s + 19×31 − (1×30×2 + 1×1×18) = 19 s + 511 \\ α_1 \| = \dfloorratio{ω_1}{6940} \\ δ_1 \| = s − qα_1 − \dparen{\sum_i r_i \dfloorratio{h_iα_1 + t_i}{g_i}} \notag \\ \| = s − 354α_1 − 30 \dfloorratio{7α_1 + 2}{19} − \dfloorratio{4α_1 + 18}{19} \\ j \| = α_1 + \dfloorratio{δ_2}{q + \sum_{r_i \gt 0} r_1} = α_1 + \dfloorratio{δ_1}{385} \notag \\ d_1 \| = s − 354j − 30 \dfloorratio{7j + 2}{19} − \dfloorratio{4j + 18}{19} \end{align}

    For our example we find

    \begin{align*} \\ ω_1 \| = 19s + 511 = 19×738676 + 511 = 14035355 \\ α_1 \| = \dfloorratio{ω_1}{6940} = \dfloorratio{14035355}{6940} = 2022 \\ δ_1 \| = s − 354α_1 − 30 \dfloorratio{7α_1 + 2}{19} − \dfloorratio{4α_1 + 18}{19} \\ \| = 738676 − 354×2022 − 30 \dfloorratio{7×2022 + 2}{19} − \dfloorratio{4×2022 + 18}{19} \\ \| = 738676 − 715788 − 30 \dfloorratio{14156}{19} − \dfloorratio{8106}{19} \\ \| = 22888 − 30×745 − 426 = 112 \\ j \| = α_1 + \dfloorratio{δ_1}{385} = 2022 + \dfloorratio{112}{385} = 2022 \\ d_1 \| = s − 354j − 30 \dfloorratio{7j + 2}{19} − \dfloorratio{4j + 18}{19} = 112 \end{align*}

  3. Transformation 1 derives the calculation month number \( m_0 \) and the calculation day number \( d_0 \) since the first day of the month from the calculation day number \( d_1 \) since the first day of the year.

    \begin{align} \{ m_0, ω_2 \} \| = \Div(gd_1 + g − t − 1, f) = \Div(13d_1 + 13 − 7 − 1, 384) \notag \\ \| = \Div(13d_1 + 5, 384) \\ d_0 \| = \dfloorratio{ω_2}{g} = \dfloorratio{ω_2}{13} \end{align}

    For our example we find

    \begin{align*} \{ m_0, ω_2 \} \| = \Div(13d_1 + 5, 384) = \Div(13×112 + 5, 384) \\ \| = \Div(1461, 384) = \{ 3, 309 \} \\ d_0 \| = \dfloorratio{ω_2}{13} = \dfloorratio{309}{13} = 23 \end{align*}

  4. And then we add 1 to the calculation month \( m_0 \) and calculation day \( d_0 \) to get the calendar month \( m \) and calendar day \( d \).

    \begin{align} m \| = m_0 + 1 \\ d \| = d_0 + 1 \end{align}

    For our example we find

    \begin{align} m \| = m_0 + 1 = 3 + 1 = 4 \\ d \| = d_0 + 1 = 23 + 1 = 24 \end{align}

    so the sought date was day 24 of month 4 of year 2022.

The above equations condense a bit, to

\begin{align} s \| = J − 1721019 \\ α_1 \| = \dfloorratio{19s + 511}{6940} \\ δ_1 \| = s − 354α_1 − 30 \dfloorratio{7α_1 + 2}{19} − \dfloorratio{4α_1 + 18}{19} \\ j \| = α_1 + \dfloorratio{δ_1}{385} \notag \\ d_1 \| = s − 354j − 30 \dfloorratio{7j + 2}{19} − \dfloorratio{4j + 18}{19} \\ \{ m, ω_2 \} \| = \Div(13d_1 + 389, 384) \\ d \| = \dfloorratio{ω_2}{13} + 1 \end{align}

For our example we find

\begin{align*} s \| = J − 1721019 = 2459695 − 1721019 = 738676 \\ α_1 \| = \dfloorratio{19s + 511}{6940} = \dfloorratio{19×738676 + 511}{6940} = \dfloorratio{14035355}{6940} = 2022 \\ δ_1 \| = s − 354α_1 − 30 \dfloorratio{7α_1 + 2}{19} − \dfloorratio{4α_1 + 18}{19} \\ \| = 738676 − 354×2022 − 30 \dfloorratio{7×2022 + 2}{19} − \dfloorratio{4×2022 + 18}{19} \\ \| = 738676 − 715788 − 30 \dfloorratio{14156}{19} − \dfloorratio{8106}{19} \\ \| = 22888 − 30×745 − 426 = 112 \\ j \| = α_1 + \dfloorratio{δ_1}{385} = 2022 + \dfloorratio{112}{385} = 2022 \\ d_1 \| = s − 354j − 30 \dfloorratio{7j + 2}{19} − \dfloorratio{4j + 18}{19} = 112 \\ \{ m, ω_2 \} \| = \Div(13d_1 + 389, 384) = \Div(13×112 + 389, 384) = \Div(1845, 384) = \{ 4, 309 \} \\ d \| = \dfloorratio{ω_2}{13} + 1 = \dfloorratio{309}{13} + 1 = 23 + 1 = 24 \end{align*}

so the sought date is day 24 of month 4 of year 2022, just like above.

Here are the dates (year-month-day) in the modern (Gregorian) calendar that correspond to new year in the lunar calendar for a couple of years:

\({j}\) D
2019 2018-12-08 8
2020 2019-12-27 27
2021 2020-12-15 15
2022 2022-01-03 34
2023 2022-12-23 23
2024 2023-12-13 13
2025 2024-12-31 31
2026 2025-12-20 20
2027 2026-12-09 9
2028 2027-12-28 28
2029 2028-12-17 17
2030 2030-01-05 5
2031 2030-12-25 25
2032 2031-12-14 14
2033 2033-01-01 32
2034 2033-12-22 22
2035 2034-12-11 11
2036 2035-12-30 30
2037 2036-12-18 18
2038 2037-12-08 8
-------+------------+------
19.5



[AA]

languages: [en] [nl]

//aa.quae.nl/en/reken/juliaansedag.html;
Last updated: 2023-12-17