' Puzzlet #024
' Find three different 2-digit
' integers such that the sum of any
' two of them is a perfect square.
' Example: 12, 52, 69. 12 + 52 = 8^2,
' 12 + 69 = 9^2, 52 + 69 = 11^2.
' Find every possible triplet with
' these properties.
' By Dave Ellis.
declare allSquares()
declare printResults()
def i1, i2, i3: int
def sums[4]: int
openconsole
print "Calculating ...": print
for i1 = 10 to 97
for i2 = (i1 + 1) to 98
for i3 = (i2 + 1) to 99
sums[1] = i1 + i2
sums[2] = i1 + i3
sums[3] = i2 + i3
if allSquares()
printResults()
endif
next i3
next i2
next i1
print: print "That's all!"
print: print "Press a key to exit ..."
do: until inkey$ <> ""
closeconsole
end
sub allSquares
' if every element in the sums
' array is a perfect square,
' returns 1, otherwise returns 0.
def flag, i: int
def root: float
i = 1
flag = 1
do
root = sqrt(sums[i])
if root <> int(root)
flag = 0
endif
i = i + 1
until flag = 0 | i > 3
return flag
sub printResults
' pretty printer
def i, j: int
def j$: string
print i1, i2, i3,
print "--> ",
for i = 1 to 3
j = sqrt(sums[i])
j$ = str$(j)
if j < 10
j$ = " " + j$
endif
print j$, "^2", " ",
next i
return
PROGRAM NOTES
Three iterators, it1, it2, and it3 are used to generate all possible combinations of three different 2-digit integers using these lines:
for i1 = 10 to 97
for i2 = (i1 + 1) to 98
for i3 = (i2 + 1) to 99
At every combination, the iterators themselves hold the integers. Now, there are three different ways of adding two items from three items: 1 + 2, 1 + 3, and 2 + 3. In our case this equates to it1 + it2, it1 + it3, and it2 + it3. These are saved in the specially-created array sums[] by the following lines:
sums[1] = i1 + i2
sums[2] = i1 + i3
sums[3] = i2 + i3
Now the function AllSquares() can be invoked to check whether all three sums are also perfect squares. If they are, we have a solution, and it's printed out by the subroutine PrintResults(). The complete process is repeated for all possible combinations of three different 2-digit integers.
How does AllSquares() work? A DO loop works through all three sums stored in the array sums[]. Each is checked to see if it's a perfect square. This is accomplished by taking its square root as a floating point number (that is, decimals are allowed). The result is compared with a version of the root from which the decimal part has been removed (by use of the int function), with these lines:
root = sqrt(sums[i])
if root <> int(root)
flag = 0
endif
If the two results are identical, the square root must not have contained a decimal part, and is threrefore a perfect square. If not, variable flag, which was previously set to 1 (TRUE) is changed to 0 (FALSE). When either all three elements in sums[] have been examined, or the flag has been changed to 0, the function exits and returns the state of flag to the calling routine.
When you run the code, it will produce the inset screen.
There are nine solutions in all, and that is the complete list.
It's interesting to note that one of the triplets produces the three squares 7², 8², and 9².
Calculating ...
10 54 90 --> 8^2 10^2 12^2
12 52 69 --> 8^2 9^2 11^2
14 35 86 --> 7^2 10^2 11^2
15 34 66 --> 7^2 9^2 10^2
16 33 48 --> 7^2 8^2 9^2
26 74 95 --> 10^2 11^2 13^2
29 52 92 --> 9^2 11^2 12^2
30 51 70 --> 9^2 10^2 11^2
48 73 96 --> 11^2 12^2 13^2
That's all!
Press a key to exit ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.