Penny Packing Calculator and eTCL Slot Calculator Demo Example

This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER in your comment with the same courtesy that I will give you. Its very hard to reply intelligibly without some background of the correspondent. Thanks,gold


gold Here is some eTCL starter code for packing pennies in a rectangular surface area. The impetus for these calculations was round barrels in a rectangular storage area like a ship deck or storage room. This area of mathematics is called circle packing. Most of the testcases involve replicas or models, using assumptions and rules of thumb.

For a rectangle, the maximum packing of pennies is considered to be alternate rows of N and N-1 pennies, where N > 5 and N= even. The diameter of a USA penny is 1.905 centimeters. Since the diameter of pennies and other coins differ as well as rectangles, we will try to make the algorithm as generic as possible.

In planning any software, it is advisable to gather a number of testcases to check the results of the program. For testcases, the following is recommended. Get three 50-rolls of clean mint pennies, a metric ruler, and clean typing paper. Mark perpendicular lines on the left and bottom of the typing paper. An old textbook usually gives as good a penciled perpendicular line as a drafting triangle. Lay pennies out touching the penciled lines and each other. Mark out a square of N pennies.

A square of * 6 6 or 36 pennies will do for a start, meaning 6 rows of 6 pennies. Use a ruler to herd the pennies to overlay the former penciled lines and mark out the boundaries for the top and right side of the square. Pin the ruler rigidly on the paper and mark the remaining sides. Some pennies may have to be knocked out of the pencil track to get a true line. Measure all four sides with the ruler and average the sides. For example, my four sides averaged / [+ 11.4 11.3 11.5 11.3 4.] or 11.375 cm.

For the first testcase, there should be N*N, 6*6, or 36 pennies in the square field marked by the pencil. In the field measured by the pencil marks, set out alternating rows of 6/5/6/5/6/5/6 pennies touching each other, with the outer rows of 6 in a line touching the pencil lines. 4 rows of 6 pennies and 3 rows of 5 pennies should total + [* 4 6 [* 3 5 ] or 39 pennies. Alternating rows allowed [- 39 [* 6 6] or three extra pennies in the old 6*6 space. The eTCL calculator returns 4 rows of N (6) and 3 rows of N-1 (5), also returning total 39 coins. While the algorithm is not perfectly certain at this point, this testcase seems agree that 3 extra coins can be packed into the old 6*6 (coin diameter) space.

The second testcase was N*N, 8*8, or 64 pennies in a square. Set up by 64 pennies, the four sides averaged / [+ 15.3 15.3 15.3 15.1 4.] or 15.25 cm. After laying out the alternating coin rows on the old 8*8 square, there were 5 rows of 8 and 4 rows of 7 giving + [* 5 8 * 4 7 ] or 68 coins. The alternating rows gave [- [+ [* 5 8 * 4 7 ] [* 8 8] or 4 more coins. The eTCL calculator returns 5 rows of N (8) and 4 rows of N-1(7), giving total coins as 68.

The third testcase was N*N, 10*10, or 100 pennies in a square. Laying 10 touching coins on each of four sides averaged / [+ 19.3 19.2 19.4 19.2 4.] or 19.275 cm. For the alternate rows, an estimate was tried. A first layer of 10 coins and a second layer of 9 coins had a width of 19.3 and a height of 3.6 cm. There appears room for 5 layers of 19 coins (2 alternating rows) and a top layer of 10 coins. The total estimate would be + [* 5 19 10] or 105 coins. The eTCL calculator returns 6 rows of N (10) and 4 rows of N-1(9), giving total coins as 105. From the eTCL calculator, the alternating rows gave - 105 100 or 5 extra coins over the old 10*10 square.

The fourth testcase was N1*N2, 6*12, or 72 pennies in a rectangle. On the two perpendicular lines, lay 6 touching coins on the left side and 12 touching coins for bottom. Mark the end coins from 6 pennies high and 12 pennies wide on the perpendicular lines as definding the sides of a rectangle. As an estimate, there would be 6*12 or 72 pennies in the completed rectangle of nonoverlapping rows. For a rectangle of alternating pennies, leave the 12 pennies marking the first row and fill the marked figure as 12/11/12/11/12/11/12 rows. There appears room for 4 layers of 12 coins and 3 layers of 11 coins. The total estimate for alternating rows would be + [* 4 12 * 3 11] or 81 coins. Internally for the report, the eTCL calculator calculates 4 rows of N (12) and 3 rows of N-1(11), giving total coins as 81. From the eTCL calculator, the optimized alternating rows gave [- 81 72 or 9 extra coins over the old 6*12 rectangle of nonoverlapping rows.

The interior space between touching coins produce geometric figures. Non-alternating layers of touching coins create four pointed stars called concave squares. Alternating layers of touching coins produce three pointed stars called concave triangles. The clay tablets, charts, palace decorations, and coefficient lists show the Babylonian mathematicians were studying concave squares and triangles. In some instances, the Babylonians were computing the remaining area from touching circles within squares. In some measure, the interior spaces between touching coins is a waste of space and it might be conjectured that a concave triangle has less wasted space than a concave square. Possibly, the saved space of a concave triangle over the concave squares would allow the placing of extra coins in the customary squares. The calculations of the space at the square edges might be tricky, but with very large squares, the accumulated space in the concave figures should be greater than the space at the edges.

The eTCL penny packing calculator was exercised over the expected range of use, 5>N<18 and N=even. The total coins for the optimised square or rectangle is shown in the calculator window. The coins for the nonoverlapping squares and rectangles are calculated and included in the report. First, the internal calulations estimate the alternating rows for an optimised square of the entered length ($side1). Second, then the number of alternating rows from $side1 are used the calculate the fill of an optimised rectangle. For now, use the longest dimension of the rectangle as the width.

Pseudocode Section

    pseudocode can be developed from rules of thumb.
    pseudocode: enter rectangle length , width , penny or coin diameter
    pseudocode: output rows of N and N-1 coins, total number of coins 
    pseudocode: rules of thumb can be 3 to 15 percent off, partly since g..in g..out.
    pseudocode: packing pennies in square.
    pseudocode: base of square 6 pennies wide.
    pseudocode: height of square 6 pennies tall
    pseudocode: pennies will be paced in layers equal to width of diameters, non optimal spacing
    find width of every stack  for each  stack layers
    length of touching coins equals  N*diameters of coin
    foreach layer {1 2 3 ... N coins high} {add to total coins} 
    foreach alternating layer {1 2 3 ... N coins high} {add to total coins} 
    pack number of circles in each layer,  short of sides,
    pseudocode: need test cases > small,medium, giant
    testcases within range of expected operation.
    pseudocode: are there any cases too small or large to be solved? 
    pseudocode: Could this be problem similar to placing grains on chessboard?
    pseudocode: Could this be problem similar to putting oil barrels on deck of ship? 

Testcases Section

In planning any software, it is advisable to gather a number of testcases to check the results of the program. The math for the testcases can be checked by pasting statements in the TCL console. Aside from the TCL calculator display, when one presses the report button on the calculator, one will have console show access to the capacity functions (subroutines).

Testcase 1

6*6 pennies table printed in tcl wiki format
quantity value comment, if any
testcase number 1
length centimeters: 6.
width centimeters: 6.
diameter coins : 1.0
answers: rows of alternating coins : 7
number of N rows: 4
number of N-1 rows: 3
total number of coins : 39.0

Testcase 2

8*8 pennies table printed in tcl wiki format
quantity value comment, if any
testcase number 2
length centimeters: 8.
width centimeters: 8.
diameter coins : 1.0
answers: rows of alternating coins : 9
number of N rows: 5
number of N-1 rows: 4
total number of coins : 68.0

Testcase 3

10*10 pennies table printed in tcl wiki format
quantity value comment, if any
testcase number 3
length centimeters: 10.
width centimeters: 10.0
diameter coins : 1.0
answers: rows of alternating coins : 11
number of N rows: 6
number of N-1 rows: 5
total number of coins : 105.0

Testcase 4

6*12 coins rectangle table printed in tcl wiki format
quantity value comment, if any
testcase number 4
length centimeters: 6.
width centimeters: 12.
diameter coins : 1.0
answers: rows of alternating coins : 7
number of N rows: 4
number of N-1 rows: 3
total number of coins,alt. rows : 81
total nonoptimum coins, nonoverlapping : 72
difference coins 9
merged rectangle, alt. rows 81

Testcase5

12*12 pennies table printed in tcl wiki format
quantity value comment, if any
testcase number 4
length centimeters: 12.
width centimeters: 12.0
diameter coins : 1.0
answers: rows of alternating coins : 13
number of N rows: 7
number of N-1 rows: 6
total number of coins : 150.0

Testcase 6

18*18 pennies table printed in tcl wiki format
quantity value comment, if any
testcase number 5
length centimeters: 18.
width centimeters: 18.0
diameter coins : 1.0
answers: rows of alternating coins : 21
number of N rows: 11
number of N-1 rows: 10
total number of coins : 368.0

Screenshots Section

figure 1.

http://s26.postimg.org/l3lrwk3o9/Image263.gif

figure 2.

http://s26.postimg.org/59yfx4n89/COINSINSQUARE_TCL_WIKI.png

figure 3.

http://s26.postimg.org/5b8dqjp21/COINSAVER_TCL_WIKI.png

figure 4.

http://s26.postimg.org/b06pb9imx/alternating_rows_coins_rectangle_TCL_WIKI.png

figure 5.

http://s26.postimg.org/ljom00xw9/nonoverlappingrows_TCL_WIKI.png

figure 6.

http://s26.postimg.org/n2oliqtuh/alternating_rows_concepts_TCL_WIKI.png


References:

  • Ask Doctor Math, Dr. Greenie, 01/23/2001, Circle packing
  • Ask Doctor Math, Doctor Peterson, 08/16/99, Spheres and spaces

Appendix Code

appendix TCL programs and scripts

        # pretty print from autoindent and ased editor  
        # penny packing calculator
        # written on Windows XP on eTCL
        # working under TCL version 8.5.6 and eTCL 1.0.1
        # gold on TCL WIKI , 2may2014
        package require Tk
        namespace path {::tcl::mathop ::tcl::mathfunc}
        frame .frame -relief flat -bg aquamarine4
        pack .frame -side top -fill y -anchor center
        set names {{} { length centimeters:} }
        lappend names { width (longest dimension) centimeters::}
        lappend names { diameter coin centimeters: }
        lappend names { answers:rows of alternating coins:}
        lappend names { number of N  rows:}
        lappend names { number of N-1 rows:}
        lappend names { total number of coins :}
        foreach i {1 2 3 4 5 6 7} {
    label .frame.label$i -text [lindex $names $i] -anchor e
    entry .frame.entry$i -width 35 -textvariable side$i
    grid .frame.label$i .frame.entry$i -sticky ew -pady 2 -padx 1 }
        proc about {} {
            set msg "Calculator for Pennies and Rectangles
            from TCL WIKI,
            written on eTCL "
            tk_messageBox -title "About" -message $msg }
        proc alternaterows {i limit xside} {    
            #console show 
            while {$i <= $limit} {
            set upper [+ 1. [/ [* [- $i 1. ] [sqrt 3. ]] 2. ]]           
            set upper [expr int($upper)]
            if { $upper == [expr { int($xside)-1 }]} break;  
            #puts are used to check algorithm working
            #puts "upper $upper "
            #puts "i $i"
            incr i
            }  
            set alternaterows $i         
            return $alternaterows
            } 
            proc nonoptimum {l w d } {
            set numrows [int [/ $l $d ] ]
            set coinwidth [int [/ $w $d ] ] 
            set totalcoin [* $numrows $coinwidth  ]        
            return $totalcoin
            }
            proc merged {l w d N N_1} {
            set numrows [int [/ $l $d ] ]
            set coinwidth [int [/ $w $d ] ]
            set totalcoin [* $numrows $coinwidth ]  
            set Nrows [* $N $coinwidth ]
            set N_1rows [* $N_1 [- $coinwidth 1]]
            set totalcoin [+ $Nrows $N_1rows  ]        
            return $totalcoin
            }
        proc calculate { } {
            global answer2
            global side1 side2 side3 side4 side5
            global side6 side7 testcase_number 
            global nonopttotal mergedx
            incr testcase_number           
            set length   $side1  
            set width  $side2
            set diameter $side3
            set initial 2
            set limit 300
            set original_side $side4
            set xside $side1 
   set side4 [ alternaterows $initial $limit $xside]
            set altrows [* $side4 1.]
            set original_side $side4
            set side6 [int [/ $altrows 2. ]]
            set side5 [int [+ [/ $altrows 2. ] 1.]]
            set Nrow $side5
            set N_1row $side6
            set side4 [+ $Nrow $N_1row]
            set coins [+ [* $side5 $side1] [* $side6 [- $side1 1 ]] ]
   set nonopttotal [nonoptimum $length $width $diameter]
 set mergedx [merged $length $width $diameter $Nrow $N_1row]
            set side7 $mergedx
             }
        proc fillup {aa bb cc dd ee ff gg} {
            .frame.entry1 insert 0 "$aa"
            .frame.entry2 insert 0 "$bb"
            .frame.entry3 insert 0 "$cc"
            .frame.entry4 insert 0 "$dd"
            .frame.entry5 insert 0 "$ee"
            .frame.entry6 insert 0 "$ff"
            .frame.entry7 insert 0 "$gg"}
        proc clearx {} {
            foreach i {1 2 3 4 5 6 7} {
                .frame.entry$i delete 0 end } }
        proc reportx {} {
            global side1 side2 side3 side4 side5
            global side6 side7 testcase_number
            global nonopttotal mergedx
            console show;
            puts "%| table |printed in| tcl wiki format|% "
            puts "&| quantity| value| comment, if any|& "
            puts "&| testcase number| $testcase_number||& "
            puts "&| length centimeters:| $side1 ||&"
            puts "&| width centimeters:| $side2||& "
            puts "&| diameter coins :| $side3||& "
            puts "&| answers: rows of alternating coins :| $side4 ||&"
            puts "&| number of N rows:| $side5||& "
            puts "&| number of N-1 rows:| $side6 ||&"
    puts "&| total number of coins,alt. rows  :| $side7 ||&"
    puts "&| total nonoptimum coins, nonoverlapping  :| $nonopttotal||&"
            set rem [- $side7 $nonopttotal ]
            puts "&| difference coins   | $rem ||&" 
            puts "&| merged rectangle, alt. rows   | $mergedx ||&" 
           }
         frame .buttons -bg aquamarine4
        ::ttk::button .calculator -text "Solve" -command { calculate   }
        ::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 6. 6. 1.0  7.1 4.01 3.01  39.0 }
        ::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 8. 8. 1.0  9.1 5.1 4.1 68.0 }
        ::ttk::button .test4 -text "Testcase3" -command {clearx;fillup 10. 10.0 1.0 11.1 6.01 5.01  105.}
        ::ttk::button .clearallx -text clear -command {clearx }
        ::ttk::button .about -text about -command about
        ::ttk::button .cons -text report -command { reportx }
        ::ttk::button .exit -text exit -command {exit}
        pack .calculator  -in .buttons -side top -padx 10 -pady 5
        pack  .clearallx .cons .about .exit .test4 .test3 .test2   -side bottom -in .buttons
        grid .frame .buttons -sticky ns -pady {0 10}
        . configure -background aquamarine4 -highlightcolor brown -relief raised -border 30
        wm title . "Penny Packing Calculator"       

Pushbutton Operation


For the push buttons, the recommended procedure is push testcase and fill frame, change first three entries etc, push solve, and then push report. Report allows copy and paste from console, but takes away from computer "efficiency".

For testcases in a computer session, the eTCL calculator increments a new testcase number internally, eg. TC(1), TC(2) , TC(3) , TC(N). The testcase number is internal to the calculator and will not be printed until the report button is pushed for the current result numbers (which numbers will be cleared on the next solve button.) The command { calculate; reportx } or { calculate ; reportx; clearx } can be added or changed to report automatically, but is not recommended as computer efficiency is impaired. Another wrinkle would be to print out the current text, delimiters, and numbers in a TCL wiki style table as

  puts " %| testcase $testcase_number | value| units |comment |%"
  puts " &| volume| $volume| cubic meters |based on length $side1 and width $side2   |&"  


Comments Section

Please place any comments here, Thanks.