The following problem appeared as ENIGMA 1162 in New Scientist 24 November 2001

TRIANGULAR OR SQUARE

Richard England

HARRY and Tom each chose a four-digit number that was either a perfect square or a triangle number and told me its last two digits. I deduced that Harry's number was one of exactly two four-digit perfect squares or one of exactly two four-digit triangular numbers, but that Tom's number was one of exactly three four-digit perfect squares or one of exactly three four-digit triangular numbers.

Then they told me that the sum of the digits of Harry's number was the same as the sum of of the digits of Tom's number. What were (a) Harry's number and (b) Tom's number?

We need fewer than 144 numbers to generate all four digit squares and triangle. The following code generates the numbers 0, 1, 2 .. 143 as attribute i in table t_num.

1.
Check that the tables primes exists by viewing all primes less than 50.