Post date: Aug 20, 2014 12:39:27 AM
"What value for the perimeter of a right triangle, less than or equal to 1000, are there the most possible combinations of side lengths, if the three sides and the perimeter must all be integers?"
This is basic application of the Pythagorean Theorem. We will filter the list to include only combos where side b is greater than or equal to side a, and side c is the same for side b, because if we don't, it just multiplies the number of all results, which is useless.
CREATE TABLE #Integers (i INT PRIMARY KEY NOT NULL)
; WITH Integers (i) AS
(
SELECT 1
UNION ALL
SELECT i + 1
FROM Integers
WHERE i < 1000
)
INSERT INTO #Integers
SELECT i
FROM Integers
OPTION (MAXRECURSION 1000)
SELECT TOP (1) p.i
FROM #Integers a
CROSS JOIN #Integers b
CROSS JOIN #Integers c
CROSS JOIN #Integers p
WHERE SQUARE(a.i) + SQUARE(b.i) = SQUARE(c.i)
AND a.i + b.i + c.i = p.i
AND a.i <= b.i
AND b.i <= c.i
GROUP BY p.i
ORDER BY COUNT(*) DESC
DROP TABLE #Integers