# Enigmatic Code

Programming Enigma Puzzles

## Enigma 338: Square money

From New Scientist #1486, 12th December 1985 [link]

“I’m not broke, but you’ve got more money than I have,” Bubbles complained.

“True,” said Hippocrene, blushing. “But less than twice what you’ve got. Now listen. If you square the money I’ve got, and add that to what you’ve got, you get the square of what Johnny’s got. See?”

“No,” said Bubbles, toying nervously with her beads. “I don’t see how you can square money.”

“That’s easy. Just express it as pounds. I mean the square of £1.20 is 1.44. The square of 15½p is 0.024025 — that is, 0.155². And so on. OK?”

“Yes,” said Bubbles, with a cheerful wink. “Go on.”

“And if you square yours and add that to mine, you get the square of Johnny’s too. Isn’t that amazing?”

“Moderately so.”

When this conversation took place, the halfpenny was still in use.

How much had Hippocrene and Bubbles respectively?

[enigma338]

### 4 responses to “Enigma 338: Square money”

1. Jim Randell 25 March 2016 at 7:03 am

This Python program runs in 35ms.

```from itertools import count
from enigma import irange, is_square, printf

# to keep things in integers we represent monetary amounts
# (in pounds) multipled up by 40000
#
# then 1/2 p = 0.005 is represented by 200
# and (1/2 p)^2 = (0.005)^2 is represented by 1

def generate():
# consider the amount that Bubbles has...
for b in count(200, step=200):
b2 = (b * b) // 40000

# and the amount that Hippocrene has...
for h in irange(b + 200, 2 * b - 200, step=200):
h2 = (h * h) // 40000

# then j^2 is both b^2 + h and b + h^2
j2 = b2 + h
if j2 != b + h2: continue
j = is_square(j2 * 40000)
if j is None: continue

yield (b, h, j)

# look for the first solution
for (b, h, j) in generate():
# output amounts in pence (not pounds)
printf("b = {b}p, h = {h}p, j = {j}p", b = b / 400.0, h = h / 400.0, j = j / 400.0)
break
```

• Jim Randell 25 March 2016 at 7:41 am

Analytically:

j² = h² + b = b² + h

h² − b² = h − b
(h + b)(h − b) = (h − b)
h + b = 1 (as h ≠ b)

Also:

b < h < 2b
⇒ 1/2 < h < 2/3

So we only need to check h in the range 50½p to 66½p (33 values).

Here’s a Python program to save us the bother:

```from enigma import irange, is_square, printf

for h in irange(20200, 26600, step=200):
b = 40000 - h

j = is_square(h * h + b * 40000)
if j is None: continue

printf("h={h}p, b={b}p, j={j}p", h = h / 400.0, b = b / 400.0, j = j / 400.0)
```
• Brian Gladman 26 March 2016 at 4:38 pm

Line 16 can step in 200’s Jim