How Many Cubes?

[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

A puzzle to find the total number of embedded cubes of any size that can be found in this 4x4x4 cubical structure.

Background & Techniques

The "Ask Marilyn" column in today's edition of Parade Magazine (Dec. 15, 2013) offers this puzzle:

"Consider a cubical structure composed of unit cubes (like a child's building blocks), four on an edge. Without drawing the structure or actually assembling it, how many cubical shapes can you envision within it?"

My first "envisioning" was incorrect but since computer programs were not forbidden, here's a small program with 25 lines of source code that verifies Marilyn's answer.  Try your hand at it, then click the button to see how the program solves it. Oh, and the solution of course.

Programmer's Notes:

I kept the analysis of the algorithm hidden until user clicks the  "Show me" button, but here are the 10 lines of code which does the trick:

for i:=1 to 4 do C[i]:=0; {initialize the cube counts}
for i:=1 to 4 do {lwidth: left to right}
for j:=1 to 4 do {depth: front to back} 
for k:=1 to 4 do {height: bottom to top}
    inc(C[1]); {count 1x1 cubes - should be 64 of these}
    if (i<4) and (j<4) and (k<4) then inc(C[2]); {generate all 2x2x2 cubes by counting  left, front, bottom corner cubes}
    if (i<3) and (j<3) and (k<3) then inc(C[3]); {count 3x3x3 cudes the same way}
    if (i<2) and (j<2) and (k<2) then inc(C[4]); {count the only 4x4x4 cube (I hope)}


Running/Exploring the Program 

bulletDownload  executable
bulletDownload source 

Suggestions for Further Explorations



Original:  December 15, 2013

Modified:  May 15, 2018

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