Post date: Jul 13, 2014 2:38:9 PM
"Find all numbers greater than 1 that are equal to the sum of the fifth powers of all their digits. Sum them."
We know this is a 6 digit number or less because the sum for the greatest 5 digit number, 99999, is 295246, a 6 digit number, several times greater than the number that went in. The answer for 999999 is still 6 digits. So it's a simple query of all 6 digit integers to find which ones equal the sum of their fifth powers.
CREATE TABLE #Integers (i INT PRIMARY KEY NOT NULL)
INSERT INTO #Integers VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9)
; WITH Results (i) AS
(
SELECT 100000 * k100.i + 10000 * k10.i + 1000 * k.i + 100 * h.i + 10 * t.i + o.i
FROM #Integers o
CROSS JOIN #Integers t
CROSS JOIN #Integers h
CROSS JOIN #Integers k
CROSS JOIN #Integers k10
CROSS JOIN #Integers k100
WHERE 100000 * k100.i + 10000 * k10.i + 1000 * k.i + 100 * h.i + 10 * t.i + o.i = POWER(k100.i,5) + POWER(k10.i,5) + POWER(k.i,5) + POWER(h.i,5) + POWER(t.i,5) + POWER(o.i,5)
)
SELECT SUM(i)
FROM Results
WHERE i > 1
DROP TABLE #Integers