PDA

View Full Version : Programming Puzzle 15 - Sum the Cubes

ABC123

Tom Cone Jr
07-05-2011, 04:26 PM
No, we're not talking ice cubes here, but cubed integers.

Consider this: The number 153 equals 1 cubed, plus 5 cubed, plus 3 cubed.

i.e. 153 = 13 + 53 + 33

Here's the challenge:

Find all other 3 digit numbers that have the same property. Display them in a message box, or write them to the trace window, at your option.

Have fun.

-- tom

StephenP
07-06-2011, 12:33 AM
dim a as n
dim b as n
dim c as n
dim sum_cubes as c = "The Sum Cubes are:" + crlf()

for x = 100 to 999
a = val(substr(str(x,3),1,1))
b = val(substr(str(x,3),2,1))
c = val(substr(str(x,3),3,1))

if a^3+b^3+c^3 = x then
sum_cubes = sum_cubes + x + " [" + a + "^3=" + a^3 + " | " + b + "^3=" + b^3 + " | " + c + "^3=" + c^3 + "]" + crlf()
end if

next x

? sum_cubes
= The Sum Cubes are:
153 [1^3=1 | 5^3=125 | 3^3=27]
370 [3^3=27 | 7^3=343 | 0^3=0]
371 [3^3=27 | 7^3=343 | 1^3=1]
407 [4^3=64 | 0^3=0 | 7^3=343]

Tom Cone Jr
07-06-2011, 08:49 AM
Very nice!

To display the answer in a desktop app change the last line to: