-1

Suppose I have a binary $s$ sequence described by $P$ and $o$: $s_n = 1 \iff n \equiv o \pmod P$.

Given two such sequences $s^1$ and $s^2$ described by $P_1, o_1$ and $P_2, o_2$ respectively, I want to know:

  • Does there exist $n$ such that $n \equiv o_1 \pmod{P_1}$ and $n \equiv o_2 \pmod{P_2}$? (Do the sequences collide?)
  • If the sequences collide, what is the smallest such $n \in \mathbb{N}_0$ which satisfies the above?

I've found a solution using code (iterate through sequence members until there is a match or we exceed $lcm(P_1, P_2)$), but I am interested to know if there is a closed-form solution.

base12
  • 11

1 Answers1

0

The answer to this question is essentially the contents of the Chinese Remainder Theorem.

If $\gcd(P_1,P_2)=1$, then things work out nicely and there is always such an $n$. The solution is also unique mod $P_1P_2$, so the smallest integer $n\geq 0$ can be found by taking any solution and taking the representative $0\leq n < P_1P_2$.

As far as finding such an $n$, there isn't a closed form formula, but one particularly nice way is to use Bezout's identity. This says that for any integers $a,b$, there exist other integers $x,y$ such that $ax+by=\gcd(x,y)$. These can be computed pretty quickly using the extended Euclidean algorithm. Applying it to $P_1,P_2$, there exist integers $k_1,k_2$ such that $$ P_1k_1+P_2k_2=1. $$ Then the $n$ given by $$ n = o_1 P_2k_2 + o_2 P_1k_1 $$ satisfies the two congruences. This isn't quite closed form, but it's probably as close as you can get. (This and a few other methods for finding solutions are on the Wikipedia page.)

If $\gcd(P_1,P_2)\neq 1$, however, then solutions only exist if $$ o_1 \equiv o_2 \mod \gcd(P_1,P_2). $$ To see this, observe if we have a solution $n$, then there are integers $m_1,m_2$ such that $$ n=o_1+P_1m_1 = o_2+P_2m_2, $$ and then we reduce mod $\gcd(P_1,P_2)$, which gives the above equation. When a solution exists in this case, a solution will be $$ n = \frac{o_1 P_2k_2 + o_2 P_1k_1}{\gcd(P_1,P_2)} $$ where $P_1k_1+P_2k_2=\gcd(P_1,P_2)$, and which will be unique mod $\mathrm{lcm}(P_1,P_2)$.