[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]




Search WWW


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 feedback 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.  Thanks


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.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only




Problem Description

The city fathers recently decided that the square pyramidal stack of civil war cannonballs in front of the courthouse was a hazard to the kiddies health.   But it would be OK if the cannonballs were laid in some arrangement flat on the ground.

By a miraculous coincidence, when the stack was disassembled, they could be arranged to form a perfect square!

How many cannonballs were in the stack?

Background & Techniques

Here is beginner's program that can be solved with less than 20 lines of code.  The top layer of square pyramid has one ball, the second layer has 4, the 3rd layer, 9 and the nth layer contains n2 balls.   We'll start adding up  the squares of consecutive integers until we find a sum that is a perfect square.    Testing numbers up to 100 should plenty be far enough to find the solution.

Here's the essential Delphi code:

procedure TForm1.SolveBtnClick(Sender: TObject);
    var i,sum,n:integer;
        for i:= 2 to 100 do
{for a bunch of numbers}
{sum the squares}
{get integer part of square root of the sum}
            if n*n=sum then 
{If sum was a perfect square, we're done!}
{Show "solved!" message here}
{ and stop the loop}

Square pyramids are one type of pentahedron  (five sided figures).      

I couldn't resist adding 3D  graphics of the cannonball-piles as they are generated.    3D graphics are too complicated for inclusion in a beginner's program, so I cheated a little like this:  

bulletI used a 3D graphics program to create a square pyramid, rotated it until I like the looks of it and  then copied down the coordinates of the 5 corners.  (The graphics program used is a freeware Delphi program from Earl Glynn's 3D Lab page.   I modified Earl's cube figure to create a square pyramid. )
bulletI scaled these points to fit nicely in the image area using a test version of the program to actually draw the 8 edge lines.   
bulletI used the coordinates of these scaled points to calculate x and y offset values in three categories:  
bulletbetween layers in a stack (move left 6.46 pixels and  up 10.5 pixels for the lower left corner of each layer); 
bulletbetween rows of balls within a layer, (move left 2.5 pixels and up 6.5 pixels for leftmost ball in each row); and  
bulletbetween balls within a row (move left 11 and down 1.3 pixels for each ball).

You'll find these constant values in the drawing code parts of program version 2 included here.

bulletSince we are looking down slightly at the stack,  balls are drawn layer by layer from bottom to top,  and from back to front within each layer so that higher, closer balls hide lower, more distant ones. 

Running/Exploring the Program 

bulletDownload source (both versions)
bulletDownload  executable  [Version2 (w/graphics)]

Suggestions for Further Explorations

Square pyramidal numbers, the number of cannonballs in each stack, form the series 1,5,14,30, 55, etc.    Here's a link to a Wolfram  mathworld page describing some of the characteristics of this series.  
The  problem presented here was based on an article in Martin Gardner's The Colossal Book of Mathematics.    As unlikely as it seems,  he says that the solution found by this program is the only solution.     It is apparently also true that the proofs are not simple.    I'd like to see the simplest of them  anyway.  
Square pyramidal numbers (square pyramidal stacks) , along with tetrahedral numbers (triangular pyramidal stacks), square numbers (layers of the square pyramids) and triangular numbers (layers of the triangular stacks) have some interesting relationships.    For example, every square number is the sum of two consecutive triangular numbers and every pyramidal number is the sum of two consecutive tetrahedral numbers.     We have just found the only pyramidal number that is square.    There are only four triangular numbers that are also pyramidal (1, 55, 91, and one more big one).    What is it?  A Google search on any of the bold terms above will give you lots of sites to investigate in addition to those referenced here.
 There are four other pairs of series where the existence of common elements  wants investigation  (Triangular-Tetrahedral, Triangular- Square,   Tetrahedral-Square,  Tetrahedral-Pyramidal).   
AT&T's  great integer sequence site!


Original: May 10, 2002

Modified: May 15, 2018

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2018, Gary Darby    All rights reserved.