How can I get index of combination from element indices
(BTW, I think the code in the first edition of the question was the issue -- not the background info with respect to the sexagenary cycle, which is actually good to include to understand the motivation for the problem).
To put the problem a bit differently, you have the sets $\mathbb{Z}/m\mathbb{Z}$ =  $\{0,1,...,m-1\}$ and $\mathbb{Z}/n\mathbb{Z} = \{0,1,...,n-1\}$, and you start with the pair (0,0), then (1,1), etc., basically applying the mapping $(x,y)\mapsto(x+1 \mod m,y+1 \mod n)$, until you wrap back around to $(0,0)$. In the easy case where $m=n$ then, you can see that $(i,i)$ was the $i$-th term in the sequence  (0-based). In general, if you are asking about the index of $(i,j)$, then you are effectively trying to solve the system of congruences
\begin{align*}
x &amp;\equiv i \mod m \\
x &amp;\equiv j \mod n
\end{align*}
for $x$. Here $x$ represents how many times you did the $(+1,+1)$ operation starting with $(0,0)$.
When $m$ and $n$ are coprime, the Chinese remainder theorem gives you an algorithm to compute the solution.  When they are not coprime, then that system in general may not have a solution, but in the case where the system was created by the above construction, it does. This math.stackexchange answer shows you how to find the (unique) solution in the range $[0,LCM(m,n))$.