' Puzzlet #044
' To find a 4-digit integer abcd such that
' ab^2 + cd^2 = abcd
' By Dave Ellis.
declare PrintResult()
def int1, int2, prodSquares: int
openconsole
print "Searching ...": print
for int1 = 10 to 99
for int2 = 10 to 99
prodSquares = int1^2 + int2^2
if prodSquares = 100*int1 + int2
PrintResult()
endif
next int2
next int1
print: print "No more!"
print: print "Press any key to exit ... ",
do: until inkey$ <> ""
closeconsole
end
sub PrintResult
' pretty printer
print ltrim$(str$(int1)),
print "^2 + ",
print ltrim$(str$(int2)),
print "^2 = ",
print prodSquares
return
PROGRAM NOTES
The explanatory term "ab" is represented by variable it1 and that of "cd" by variable it2. Both of these variables are cycled through all their possible values by the two nested FOR loops.
At each iteration, the sum of the squares of the these two variables are stored in prodSquares. All that remains to do is to compare prodSquares with 100*it1 + it2.
If they are equal, it's the same thing as saying ab^2 + cd^2 = abcd, and thus we have a solution which is printed to the screen by the subroutine PrintResults().
When you run the code, it will produce the inset screen very quickly.
These are the only two solutions.
Searching ...
12^2 + 33^2 = 1233
88^2 + 33^2 = 8833
No more!
Press any key to exit ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.