unit U_MacGregor;
{Copyright  © 2002, Gary Darby,  www.DelphiForFun.org
 This program may be used or modified for any non-commercial purpose
 so long as this original notice remains in place.
 All other rights are reserved
 }

{A simple puzzle }
{Mr. MacGregor always planted his cabbage patch in a square configuration. This 
summer he decided to plant extra for his rabbit friends and ended up planting an extra 
47 cabbages. How big was his patch last year and this year?
 
This puzzle can be solved without a computer if we examine the equation
 
                    Thisyear2 - Lastyear2 = 47}
{Hint:   A little knowledge of factoring  [like.  x2-y2=(x-y)*(x+y)] and prime numbers
(like 47 is prime) will be helpful in solving the equation.{ 

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, jpeg, ExtCtrls;

type
TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    StatusBar1: TStatusBar;
    Image1: TImage;
procedure SolveitBtnClick(Sender: TObject);
end;

var Form1: TForm1;

implementation
{$R *.DFM}

{************** SolveitBtnClick ****************}
procedure TForm1.SolveitBtnClick(Sender: TObject);
var last,this:integer;
begin
memo2.clear;
for last:=1 to 200 do
for this:=last+1 to 201 do
if this*this-last*last=47 then
with memo2, lines do
begin
add(format('MacGregor''s cabbage patch was %d x %d cabbages last year '
+'(%.0n altogether) and %d x %d (%.0n cabbages) this year, 47 more.',
         [last,last,0.0+last*last,this,this,0.0+this*this]));
     add(' ');
     add('Here is the Delphi source code that solves the puzzle');
     add(' ');
     add('for last:=1 to 200 do ');
     add('for this:=last+1 to 200 do');
     add('if this*this-last*last=47 then');
     add('begin ');
     add('  {display answer here}');
     add('  break;');
     add('end;');
end;
end;

end.