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. For the initial problem, the area was a square of 100 pennies on a side and each circle was diameter of 1 penny. The rectangle surface was 100 penny diameters by 100 penny diameters. A USA penny has approximately 1.9 centimeter diameter (or 2 cm rounded). In theory, the square was * 1.9 100. or 190 cm on a side. The brute force solution, where rows do not alternate, was length 100 pennies and width 100 pennies, *100. 100. or 10E4 pennies. More pennies can be packed with alternating rows of N and N-1 pennies in a similar square, so the calculator was written for the alternating row solution. The solution was 58 rows of 100 pennies and 57 rows of 99 pennies. The calculation with alternate rows was 11443 pennies total. Since the legal 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 a 100-roll 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 eTCL penny packing calculator was exercised over the expected range of use,  5>N<18 and N=even. 

Pseudocode and Equations

  liters = [* volume_in_cubic_cm .001]
  price? = raw materials + labor  +  profit
  price? = raw materials + heat process
  price? = raw materials + labor 
  average price per unit  = revenue  / units sold

Table 1, Prices


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

Testcase 2

Testcase 3

Testcase 4


Screenshots Section

figure 1.

http://s10.postimg.org/o06sn7kex/Image261.gif


References:

  • Ask Dr. Math, 03/15/2001, material left on roll.

Appendix Code

appendix TCL programs and scripts

        # pretty print from autoindent and ased editor        # 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 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 pi {} {
             expr acos(-1)
             } 
        proc about {} {
            set msg "Calculator for Pennies and Rectangles
            from TCL WIKI,
            written on eTCL "
            tk_messageBox -title "About" -message $msg } 
        proc calculate {     } {
            global answer2
            global side1 side2 side3 side4 side5
            global side6 side7 testcase_number 
            incr testcase_number           
            set length   $side1  
            set width  $side2
            set diameter $side3
            set i 2
            #set i [expr  { int ([/ $side1 2.]) }]
            set limit 300
            #set limit [expr {int($limit) } ]
            #console show 
            while {$i <= $limit} {
   set upper [+ 1. [/ [* [- $i 1. ] [sqrt 3. ]] 2. ]]
            set side6 $upper
            #puts are used to check algorithm working
            #puts "upper $upper "
            #puts "i $i"
       if { $upper >= [expr { int($side1)-1. }]} break;  
            incr i
            }
            set side4 $i
            set original_side $side4
            set side6 [int [/ $i 2. ]]
            set side5 [int [+ [/ $i 2. ] 1.]]
            set Nrow $side5
            set N_1row $side6
            set side4 [+ $Nrow $N_1row]
 set coins [+ [* $side5 $side1] [* $side6 [- $side1 1 ]] ]
            set side7 $coins
             }
        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 
            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  :| $side7 ||&"
            }
         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.