# SQL

Pick an engine:

## Thrice Unfactorised

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. DROP TABLE t_num; DROP TABLE t_num1; CREATE TABLE t_num (i INTEGER PRIMARY KEY); INSERT INTO t_num VALUES(0); INSERT INTO t_num VALUES(1); INSERT INTO t_num VALUES(2); CREATE TABLE t_num1 (i INTEGER PRIMARY KEY); INSERT INTO t_num1 SELECT i FROM t_num; INSERT INTO t_num1 SELECT 3*b.i+a.i+3 FROM t_num b, t_num a; DELETE FROM t_num; INSERT INTO t_num SELECT 12*b.i+a.i FROM t_num1 a, t_num1 b; SELECT i, i*i SQUARE, i*(i+1)/2 TRIANGLE FROM t_num