# Enigmatic Code

Programming Enigma Puzzles

## Enigma 190: Purely by coincidence

From New Scientist #1335, 9th December 1982 [link]

It took quite some time to drive my nephew Henry home last Saturday. There were a number of delays caused by road works and traffic jams but it didn’t seem to take so long because of the excellent programmes on the radio. Henry was quieter than usual so at the end of the journey I asked him: “Do you like classical music?”

“I wasn’t listening — I was multiplying the outer two digits of your 24-hour clock and noting when the result was equal to the product of the two inner digits.

“I should think that such coincidences occurred quite often,” I said.

“Not if one excludes products that are zero — and those formed by the same two digits such as at 12:12 when we started. The last coincidence was just before you switched off the ignition at 23:46.”

When did the other coincidences occur?

[enigma190]

### 2 responses to “Enigma 190: Purely by coincidence”

1. Jim Randell 10 May 2014 at 8:16 am

This Python program runs in 39ms.

```from enigma import irange, multiply, printf

# generate times starting at h0:m0
def generate(h0, m0):
while True:
for h in irange(h0, 23):
for m in irange(m0, 59):
yield (h, m)
m0 = 0
h0 = 0

for (h, m) in generate(12, 12):
if (h, m) > (23, 46): break
# extract the digits
(h0, h1) = divmod(h, 10)
(m0, m1) = divmod(m, 10)
d1s = sorted((h0, m1))
d2s = sorted((h1, m0))
# skip cases where the digits are the same
if d1s == d2s: continue
# compute the products
p1 = multiply(d1s)
p2 = multiply(d2s)
# we want the cases where the products are the same non-zero value
if p1 == p2 != 0:
printf("{h:02d}:{m:02d}")
```

Solution: The other instances occurred at 12:24, 12:36, 12:48, 13:26, 13:39, 14:28 and 21:42.

2. Jim Randell 10 May 2014 at 10:10 am

I thought I might learn Julia. So after a quick look at the documentation here’s a stab at the same program in Julia.

For this program I get an external runtime of 2.9s, and an internal runtime of 767ms (measured using tic() and toc()). These compare to an external runtime of 39ms and an internal runtime of 3.2ms (measured using the timer() object from enigma.py) for the Python program above, so I’m not seeing a great speed benefit from using Julia on this simple problem (in fact, using the internal timings, Julia is about 240× slower than Python and Perl).

```# generate times starting at h0:m0
function generate(h0, m0)
while true
for h = h0:23
for m = m0:59
produce(h, m)
end
m0 = 0
end
h0 = 0
end
end

for (h, m) in @task generate(12, 12)
(h, m) > (23, 46) && break
# extract the digits
(h0, h1) = divrem(h, 10)
(m0, m1) = divrem(m, 10)
d1s = sort([h0, m1])
d2s = sort([h1, m0])
# skip cases where the digits are the same
(d1s == d2s) && continue
# compute the products
p1 = prod(d1s)
p2 = prod(d2s)
if p1 == p2 != 0
@printf("%02d:%02d\n", h, m)
end
end
```

This site uses Akismet to reduce spam. Learn how your comment data is processed.