How to tell if two recurring events will meet in time?

209 Views Asked by At

I am trying to solve some kind of a math problem here... The particular use-case is not a mathematical one, but it boils down to a maths issue. I'm trying to figure out how to tell if two recurring events collide in time.

Event 1 takes place on the 1st week, then it doesn't happen for 2 weeks, then it happens again (1st, 4th, 7th, 10th, 13th, 16th, 19th, 22th, etc.)

Event 2 takes place on the 2nd week, then it doesn't happen for 4 weeks, then it happens again (2nd, 7th, 12th, 17th, 22th, etc.)

As you can see, these events sometimes happen on the same week: 7th, 22th, etc.

The problem here is that I have 3 variables:

  • difference in weeks between the first and the second event (in this example, the difference is equal to 1)
  • what is the repeat schedule of each of the two events: once a week, once every 2 weeks, once every 3 weeks, etc. (in this example, Event 1 happens once every 3 weeks; Event 2 happens once every 5 weeks)

Obviously, there are two certain conditions that can be moved away:

  • having a difference of 0 weeks (means that the events occur on the same day, so practically they collide)
  • having a repeat schedule of "once a week" for at least one event

NOTE: The events are recurring and some may not have an ending date, meaning they could last forever.

What I'm trying to achieve here is tell whether the events will occur in the same week. Ever.

1

There are 1 best solutions below

3
On BEST ANSWER

Let $A_n = 1 + 3n$, $B_m = 2 + 5m$.

You want to see if there are any solutions $A_n =^? B_m$ for $n \ge 0, m\ge 0$

$$1 + 3n =^? 2 + 5m$$ $$3n - 5m=^? 1 $$

At this point, since $\gcd(3,5) = 1$, you know there are solutions $m,n$ in the integers. If they are not both positive, you can simultaneously add $5$ to $n$ and $3$ to $m$ until they are both positive.

In general, a collision exists if and only if $\gcd(a,b)$ divides$ |B_0 - A_0|$

In the language of ideals, you have that $ \gcd(a,b)\mathbb{Z} \,= a \mathbb {Z} + b \mathbb Z$. If you are confused about why it's true, you could make another question or I'm sure it's one of the early facts in most number theory textbooks, and sometimes called Bezout's Identity