THE PUZZLET PAGE

PUZZLET 028 - THE SOLUTION

' Puzzlet #028
' A right-angled triangle whose sides are all
' integers has a perimeter which is a perfect
' square and an area which is a perfect cube.
' Find the sides, perimeter, and area of the
' smallest possible solution.
' By Dave Ellis.


declare PrintResult()
def base, side, flag: int
def hyp, peri, area: float
def squareRootPeri, cubeRootArea: float
openconsole
print
"Calculating ...": print
side = 4
flag = 0

do
    base = 3
    do
        hyp = sqrt(base*base + side*side)
        if int(hyp) = hyp
            peri = base + side + hyp
            squareRootPeri = sqrt(peri)
            if int(squareRootPeri) = squareRootPeri
                area = 0.5 * base * side
                cubeRootArea = area^(1/3)
                if int(cubeRootArea) = cubeRootArea
                    flag = 1
                endif
            endif
        endif

        base = base + 1
    until base = side | flag
    side = side + 1
until flag

PrintResult()
print: print "That's all!"
print: print "Press a key to exit ..."
do: until inkey$ <> ""
closeconsole
end

sub
PrintResult
' pretty printer
print "Base: ", base - 1
print "Side: ", side - 1
print "Hypotenuse: ", hyp
print "Perimeter: ", peri,
print "(=", str$(sqrt(peri)), "^2)"
print "Area: ", area,
print "(=", str$(cubeRootArea), "^3)"
return

           
PROGRAM NOTES

The nomenclature used for this problem is simple. Every right triangle has a shortest side, and I choose to call that base.  

The next shortest side I have called side, and the remaining (longest) side hyp for hypotenuse.



All the other variable names in the code are self-explanatory: perisquareRootPeri for the perimeter's square root, and cubeRootArea for the cube root of the area, etc.  Since the smallest right triangle is 3:4:5, base is initialised to 3 and side is initialised to 4.

The whole program runs within two loops.  The outer loop sets the size of variable base. 
for perimeter,
The inner loop investigates the properties for the smallest right triangle it can generate using the current value stored in base and side.

I've numbered the lines of code that do all the work to make the explanation simpler.

1
2
3
4
5
6
7
8
9

hyp = sqrt(base*base + side*side)
    if int(hyp) = hyp
       peri = base + side + hyp
       squareRootPeri = sqrt(peri)
        if int(squareRootPeri) = squareRootPeri
           area = 0.5 * base * side
           cubeRootArea = area^(1/3)
           if int(cubeRootArea) = cubeRootArea
             flag = 1


Line 1
calculates the hypotenuse for each triangle using Pythagorus.

Line 2 checks that hyp is an integer.   If it's not, this can't be a right triangle, and the investigation for those particular values of base and side is abandoned.

Line 3 calculates the value of the perimeter by summing all three sides.

LIne 4 finds the square root of peri.

Line 5 checks that the square root of peri is an integer.  If it is, peri has met the requirement of being a perfect square.  If not, this particular triangle isn't investigated further.

Line 6 calculates the triangle's area in the usual way.

Line 7 extracts the cube root of area.

Line 8 checks that area's cube root is an integer. If it is, area itself meets the requirement of being a cube.
 If not, this particular triangle isn't investigated further.

Line 9 sets variable flag (initialised before running the program at 0 to indicate FALSE) to 1 for TRUE. This is a signal to the DO loops to quit so that the program can print out the details of the current triangle and stop.

When you run the code,  it will produce the inset screen.  

As you can see, everything about this triangle is based on a factor of 24.  The base is 6 x 24.  The side is 8 x 24.  The hypotenuse is 10 x 24.   The perimeter is 24 x 24, and the area is 576 x 24.

Note also that this is just a 3:4:5 triangle multiplied by 48!


Calculating ...

Base: 144
Side: 192
Hypotenuse: 240
Perimeter: 576 (= 24^2)
Area: 13824 (= 24^3)

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.