Problem Description   

Put 3 marbles in a triangle and place a 4th marble on top.   You  have just built a  triangular based pyramid, also called a tetrahedron.  If you started with 6 marbles and placed a second layer of 3 and then one on top, you have another tetrahedron with 3 layers and 10 marbles.  The two  layer tetrahedron contains 4 marbles,  a perfect square(4=22).   Can you find the next tetrahedron whose  total number of marbles is a perfect square?

Background & Techniques   

Only about 10 lines of code in this one. Starting with a one marble pyramid, we'll just generate the total for each pyramid from the previous one.   And stop when the square root of the total is an integer. 

 I put an upper limit of 1000 marbles per side, just in case there weren't any small solutions.  We don't want the program to loop forever.   Notice that each layer contains the number of marbles in the previous layer plus the layer number additional.  And the total is just the previous total plus the number in this layer.

Tetrahedron #  Marbles in bottom layer Total marbles
1 1 1
2 3 4
3 6 10
4 10 20
5 15 35

Running/Exploring the Program     

Suggestions for Further Explorations

It turns out that there are names for each of the numbers we worked with in this program.   The number of marbles in each layer are triangular numbers.  And the total number of marbles in each pyramid is a tetrahedral number.   The solution found here is the largest  one.    And, as you might suspect, there are algorithms to compute the millionth tetrahedral number without computing the first 999,999.  For more information, check this excellent site:  http://mathworld.wolfram.com/TetrahedralNumber.html
Triangular numbers have other interesting properties.  For example
The sum of any two consecutive triangular numbers is the square of an integer!   
The square of the Nth triangular number minus the square of its predecessor is N cubed!  
 The Nth triangular number is also the number of ways we can select  2 objects from N+1, ignoring order (these are called combinations).  For example, the 3rd triangular number from the table above is 6.  and there are exactly 6  combinations for 2 of 4 objects.  See Permutes2 program for more discussion of combinations.