Open Source · 7 Languages · 1007 Problems + Bonus

Project Euler Solutions

Complete solutions in C++, Python & Java — with step-by-step mathematical explanations

All Problems

Problem 891: Ambiguous Clock

View on Project Euler

Project Euler Problem 891 Solution

EulerSolve provides an optimized solution for Project Euler Problem 891, Ambiguous Clock, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary Let \(T=43200\) denote one full 12-hour cycle measured in seconds. If the hour, minute, and second hands are modeled by the speed triple $$s_1=1,\qquad s_2=12,\qquad s_3=720,$$ then at time \(t\in[0,T)\) their angular positions are \(s_1t\), \(s_2t\), and \(s_3t\) modulo \(T\). The task is to count the moments for which the visible three-point pattern on the dial is ambiguous: the same observed configuration can be explained by at least two different assignments of the labels “hour”, “minute”, and “second”. Mathematical Approach The C++, Python, and Java implementations solve the problem one permutation at a time. For each non-identity relabeling, the geometry becomes a pair of linear congruences, which then reduces to a finite rational grid of candidate times. Step 1: Write the configuration equations up to a common rotation Suppose that the configuration seen at time \(t\) can also be explained by another time \(\tau\) after a nontrivial permutation \(\pi\) of the three hand labels. Because the observed pattern is unlabeled, the two descriptions may differ by a global rotation \(r\). Therefore $$s_i t \equiv s_{\pi(i)}\tau + r \pmod{T},\qquad i=1,2,3.$$ Subtract the first equation from the second and third to eliminate the unknown rotation....

Detailed mathematical approach

Problem Summary

Let \(T=43200\) denote one full 12-hour cycle measured in seconds. If the hour, minute, and second hands are modeled by the speed triple

$$s_1=1,\qquad s_2=12,\qquad s_3=720,$$

then at time \(t\in[0,T)\) their angular positions are \(s_1t\), \(s_2t\), and \(s_3t\) modulo \(T\). The task is to count the moments for which the visible three-point pattern on the dial is ambiguous: the same observed configuration can be explained by at least two different assignments of the labels “hour”, “minute”, and “second”.

Mathematical Approach

The C++, Python, and Java implementations solve the problem one permutation at a time. For each non-identity relabeling, the geometry becomes a pair of linear congruences, which then reduces to a finite rational grid of candidate times.

Step 1: Write the configuration equations up to a common rotation

Suppose that the configuration seen at time \(t\) can also be explained by another time \(\tau\) after a nontrivial permutation \(\pi\) of the three hand labels. Because the observed pattern is unlabeled, the two descriptions may differ by a global rotation \(r\). Therefore

$$s_i t \equiv s_{\pi(i)}\tau + r \pmod{T},\qquad i=1,2,3.$$

Subtract the first equation from the second and third to eliminate the unknown rotation. Since

$$s_2-s_1=11,\qquad s_3-s_1=719,$$

we obtain

$$11t \equiv \alpha_{\pi}\tau \pmod{T},\qquad 719t \equiv \beta_{\pi}\tau \pmod{T},$$

where

$$\alpha_{\pi}=s_{\pi(2)}-s_{\pi(1)},\qquad \beta_{\pi}=s_{\pi(3)}-s_{\pi(1)}.$$

At this point the clock picture has been converted into pure modular arithmetic.

Step 2: Eliminate the alternative time and get a discrete time grid

Multiply the first congruence by \(\beta_{\pi}\) and the second by \(\alpha_{\pi}\), then subtract. The term containing \(\tau\) disappears:

$$\left(11\beta_{\pi}-719\alpha_{\pi}\right)t \equiv 0 \pmod{T}.$$

Define the determinant-like quantity

$$\Delta_{\pi}=719\alpha_{\pi}-11\beta_{\pi},\qquad N_{\pi}=|\Delta_{\pi}|.$$

Then every candidate moment for that permutation must satisfy

$$\Delta_{\pi}t \equiv 0 \pmod{T},$$

so the solutions in \([0,T)\) are exactly

$$t_n=\frac{Tn}{N_{\pi}},\qquad n=0,1,\dots,N_{\pi}-1.$$

Thus each permutation contributes a finite arithmetic grid of rational times rather than a continuous search over the interval.

Step 3: Recover the second interpretation with Bézout coefficients

For every non-identity permutation in this problem, the pair \((\alpha_{\pi},\beta_{\pi})\) is coprime. Hence there exist integers \(u_{\pi},v_{\pi}\) such that

$$u_{\pi}\beta_{\pi}+v_{\pi}\alpha_{\pi}=1.$$

Multiply the congruence \(11t\equiv \alpha_{\pi}\tau\pmod{T}\) by \(v_{\pi}\) and the congruence \(719t\equiv \beta_{\pi}\tau\pmod{T}\) by \(u_{\pi}\), then add them. The left-hand side becomes \(\tau\):

$$\tau \equiv \lambda_{\pi} t \pmod{T},\qquad \lambda_{\pi}=11v_{\pi}+719u_{\pi}.$$

Because \(t=t_n=Tn/N_{\pi}\), the partner time also lies on the same grid:

$$\tau=t_{n'},\qquad n' \equiv \lambda_{\pi}n \pmod{N_{\pi}}.$$

A moment is genuinely ambiguous for that permutation exactly when \(n'\ne n\). Fixed points are ignored because they do not produce a second distinct interpretation.

Step 4: Reduce every candidate to one canonical rational time

The same real moment can arise from different permutations, so the times must be deduplicated globally. For one permutation we start from

$$t_n=\frac{Tn}{N_{\pi}}.$$

Let

$$g_0=\gcd(T,N_{\pi}),\qquad T_0=\frac{T}{g_0},\qquad N_0=\frac{N_{\pi}}{g_0}.$$

Then

$$t_n=\frac{T_0 n}{N_0}.$$

Reducing once more by \(g=\gcd(n,N_0)\) gives the canonical fraction

$$t_n=\frac{T_0(n/g)}{N_0/g}.$$

Storing the pair \(\left(T_0(n/g),\,N_0/g\right)\) guarantees that equal moments coming from different permutations are merged correctly.

Step 5: Worked example

Take the permutation that swaps the hour and minute roles while keeping the fastest hand in the third position, so that

$$\left(s_{\pi(1)},s_{\pi(2)},s_{\pi(3)}\right)=(12,1,720).$$

Then

$$\alpha_{\pi}=1-12=-11,\qquad \beta_{\pi}=720-12=708,$$

and therefore

$$\Delta_{\pi}=719(-11)-11(708)=-15697,\qquad N_{\pi}=15697.$$

One Bézout identity is

$$3\cdot 708 + 193\cdot(-11)=1,$$

so we may take \(u_{\pi}=3\) and \(v_{\pi}=193\). This yields

$$\lambda_{\pi}\equiv 11\cdot 193 + 719\cdot 3 \equiv 4280 \pmod{15697}.$$

Hence the candidate times are

$$t_n=\frac{43200n}{15697},\qquad \tau_n=\frac{43200\left(4280n\bmod 15697\right)}{15697}.$$

For \(n=1\) we get

$$t=\frac{43200}{15697}\approx 2.752118239\text{ s},\qquad \tau=\frac{43200\cdot 4280}{15697}\approx 11779.066063579\text{ s}.$$

Because \(4280\not\equiv 1\pmod{15697}\), this is a genuine ambiguous moment. The implementations repeat exactly this calculation for all five non-identity permutations and then remove overlaps.

How the Code Works

The C++, Python, and Java implementations iterate over the five non-identity permutations of the speed triple \((1,12,720)\). For each permutation they compute the two speed differences, the determinant magnitude \(N_{\pi}\), and one Bézout pair that reconstructs the partner index \(n'\) from \(n\).

They then scan every \(n\) with \(0\le n<N_{\pi}\), discard the fixed points satisfying \(n'=n\), and convert the surviving time \(Tn/N_{\pi}\) into a reduced numerator-denominator pair. This reduction is purely arithmetic, so no floating-point comparison is needed.

The C++ implementation collects all reduced fractions and performs a final sort-and-unique pass. The Python and Java implementations deduplicate immediately with set-style storage. Despite that storage difference, all three implementations enumerate the same rational moments and return the size of the final unique collection.

Complexity Analysis

Let \(N_{\pi}=|719\alpha_{\pi}-11\beta_{\pi}|\). The main work is the enumeration of all indices \(n=0,\dots,N_{\pi}-1\) over the five non-identity permutations, so the arithmetic scan costs

$$O\!\left(\sum_{\pi\ne\mathrm{id}} N_{\pi}\right).$$

Each candidate requires only a constant amount of gcd arithmetic. If \(M\) candidate fractions survive the fixed-point test, the final deduplication cost is \(O(M\log M)\) for the sort-based implementation and expected \(O(M)\) for the set-based ones. Memory usage is \(O(M)\). For this concrete speed triple, the total scan length is \(2052026\), so the method is easily practical.

Footnotes and References

  1. Problem page: Project Euler 891
  2. Modular arithmetic: Wikipedia - Modular arithmetic
  3. Bézout's identity: Wikipedia - Bézout's identity
  4. Extended Euclidean algorithm: Wikipedia - Extended Euclidean algorithm
  5. Permutations: Wikipedia - Permutation

Mathematical approach · C++ solution · Python solution · Java solution

Previous: Problem 890 · All Project Euler solutions · Next: Problem 892