As of October, 2016, Embarcadero is offering a free release
of Delphi (Delphi
10.1 Berlin Starter Edition ). There
are a few restrictions, but it is a welcome step toward making
more programmers aware of the joys of Delphi. They do say
"Offer may be withdrawn at any time", so don't delay if you want
to check it out. Please use the
link to let me know if the link stops working.
Support DFF - Shop
If you shop at Amazon anyway, consider using
this link. We receive a few cents from each purchase.
Support DFF - Donate
If you benefit from the website, in terms of
knowledge, entertainment value, or something otherwise useful,
consider making a donation via PayPal to help defray the
costs. (No PayPal account necessary to donate via credit
card.) Transaction is secure.
e-mail with your comments about this program (or anything else).
This program solve problem #1 from Clessa's
PC Puzzle book: "... Find
the smallest area Pythagorean triangle whose perimeter is a perfect square and
whose area is a perfect cube."
The solution itself is not very interesting, but there are a few things that
may be new for beginners. The heart of the program is a nested loop that
increments variables a and b and for each pair applies 3 tests
|a2 + b2 = c2 for some integer
c (i.e. it is a Pythagorean triple)|
|(a + b + c) = n2 for some integer n (i.e. perimeter is a perfect
|1/2 (a*b) = n3 for some integer n (i.e. area is a perfect cube)|
As soon as any test is failed, we can continue with the next b
value. If all three tests are passed, we have a solution.
If you're a beginner, there are couple of things worth noting
here, you'll see them lots:
The statements Screen.cursor:=crHourGlass and Screen.Cursor:=crDefault
to set he cursor to "busy" while solving and reset it when
The "trunc" function converts real
numbers to integer types.
It is frequently desirable (or necessary) to measure the time
that some task performs. I put a couple of timing tests in here just to
see how they work. I originally used the "Now"
function which gets date and time values but it's not very accurate for
measurements under a second (solving the program takes 6 milliseconds
(.oo6 seconds) on my computer. I won't go into the detail of
date/time formats here, suffice it to say that Now usually
returned 0 ms with an occaisonal 50 ms result thrown in - kind of
useless for timing. Two more accurate functions used here are "timeGetTime"
and "QueryPerformanceCounter". These are calls to
Windows functions and are described in the "Windows SDK" Help
file. (SDK stands for System Development Kit, by the
As always, feel free to email me if you have problems or
Running/Exploring the Program
To view the code, click here.
To download the source code, click here.
To download the executable code, click here.
Suggestions for Further Study
|I wonder what the second smallest triangle is? |
|There's some commented code in the program that tries to use a
"generating function" to generate the triples for testing without
trying all combinations. I couldn't get it to generate the required
solution, but I think I've seen several web sites that claim that it
generates all triples. Do a search on "Pythagorean triples",
and you'll find lots of links. Anyway, since the long way of
solving the problem only took .006 seconds, I guess I can't justify spending
too much time looking for a faster algorithm.|
|Replace the SQR (square) functions with multiplies (e.g.
replace Sqr(a) with a*a, etc.) Does it
affect the run time? Can you find any other ways to speed up the