Indian Bakhshali Square Root Algorithm and eTCL demo example calculator, numerical analysis

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 Indian Bakhshali Square Root Algorithm in calculator shell.

The Bakhshali Square Root Algorithm from palm leaves was loaded into an eTCL calculator shell. The Bakhshali Square Root Algorithm is of historical interest, but the rule is not very accurate ( per number/speed of calculations) and dependent on an initial input or guess.

In planning any software, it is advisable to gather a number of testcases to check the results of the program. The results of the testcases are estimated using the hand calculator and then checked in the eTCL slot calculator. Pseudocode and equations are developed from the hand calculations and theory. Small console programs are written to check or proof the alternate subroutines or procedures, rather than keeping the unblessed code and comment lines in the main slot calculator. Finally the improved or alternate subroutines are loaded into the slot calculator. The eTCL slot calculator is effectively a shell program to input entries, host calculation routines, and display results. Additional significant figures are used to check the eTCL calculator, not to infer the accuracy of inputs and product reports.

For the first testcase of sqrt 3, n=2,n*n=4, s=3 was hand calculated in formula. The Bakhshali formula is (n*n*(n*n+6*s)+s*s)/(4.*n*(n*n+s)). n=2,n*n=4, s=3 For first testcase, the numerator evaluates as (n*n*(n*n+6*s)+s*s),(2*2*(2*2+6*3)+3*3), or 97. For first testcase, the denominator evaluates as (4.*n*(n*n+s),(4.*2*(2*2+3)) or 56. formula evaled as num/denom, 97/56, 1.7321428571428572. The sqrt function in TCL gave eval sqrt(3) = 1.7320508075688772. The error was 100*(1.7321428571428572- 1.7320508075688772)/1.7320508075688772, or 0.531 percent error.

For the second testcase of sqrt 216000, n=465.,n*n=216225,s=216225. was hand calculated in formula. 60*60*60=216000.Next up square was 216225.0 .For second testcase, the numerator evaluates as (n*n*(n*n+6*s)+s*s),(465*465*(465*465+6*216225)+216225*216225),374026005000. For second testcase, the denominator evaluates as (4.*n*(n*n+s),(4.*465*(465*465+216225),804357000.0. formula evaled as num/denom, 374026005000.0/804357000.0 .

For the third testcase of sqrt 12960000, n=3601,n*n=12967201,s=12960000 was hand calculated in formula. 60*60*60*60=12960000.Next up square was 3601.*3601=12967201.For third testcase, the numerator evaluates as (n*n*(n*n+6*s)+s*s),(3601*3601*(3601*3601+6*12960000)+12960000*12960000),1344439451534401.0 . For third testcase, the denominator evaluates as (4.*n*(n*n+s),( (4.*3601*(3601*3601+12960000),373455403204.0. formula evaled as num/denom, 1344439451534401.0 /373455403204. or 3600.0000000000027 . Hand calculations seem to agree with eTCL calculator, although the number of digits are approaching the limit of tcl_precision 17.


Pseudocode Section

    # using  pseudocode for  procedure algorithm.
            3 quantities needed
            target number
            trial_square_root,   w.a. guess
            formula factor , usually 2 or 3
            check approx. root from square side rule with sqrt function in TCL
     ref. errorx procedure        
     check_answer   new area =? desired goal , desired goal reached (yes/no)
     set answers and printout with resulting values

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

table 1printed in tcl wiki format
quantity value comment, if any
1:testcase_number
3.0 :target number N
2.0 :trial square root
2.0 :function factor
1.7321428571428572 :answers: intermediate term in formula
4.0 :trial up square
1.7320508075688772 :square root from TCL sqrt function
0.0053144846316133254 :percentage error
1.7321428571428572 :approximate square root from Bakhshali formula

Testcase 2

table 2printed in tcl wiki format
quantity value comment, if any
2:testcase_number
216000.0 :target number N
465.0 :trial square root
2.0 :function factor
464.75800154489428 :answers: intermediate term in formula
216225.0 :trial up square
464.75800154489002 :square root from TCL sqrt function
9.1038288019262836e-13 :percentage error
464.75800154489428 :approximate square root from Bakhshali formula

Testcase 3

table 3printed in tcl wiki format
quantity value comment, if any
3:testcase_number
12960000.0 :target number N
3601.0 :trial square root
2.0 :function factor
3600.0000000000027 :answers: intermediate term in formula
12967201.0 :trial up square
3600.0 :square root from TCL sqrt function
6.6613381477509392e-14 :percentage error
3600.0000000000027 :approximate square root from Bakhshali formula

Screenshots Section

figure 1.

Indian Bakhshali Square Root Algorithm calculator png


References:


Appendix Code

appendix TCL programs and scripts

        # pretty print from autoindent and ased editor
        # Indian Bakhshali Square Root calculator
        # written on Windows XP on eTCL
        # working under TCL version 8.5.6 and eTCL 1.0.1
        # gold on TCL WIKI, 2oct2017
        package require Tk
        package require math::numtheory
        namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory }
        set tcl_precision 17
        frame .frame -relief flat -bg aquamarine4
        pack .frame -side top -fill y -anchor center
        set names {{} {target number N :} }
        lappend names {trial square root:}
        lappend names {formula factor: }
        lappend names {answers: intermediate term in formula }
        lappend names {trial square :}
        lappend names {square root from TCL sqrt function : }
        lappend names {percentage error: }
        lappend names {approximate square root from square side rule :} 
        foreach i {1 2 3 4 5 6 7 8} {
            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 Indian Bakhshali Square Root
            from TCL WIKI,
            written on eTCL "
            tk_messageBox -title "About" -message $msg } 
       proc ::tcl::mathfunc::precision {precision float}  {
            #  tcl:wiki:Floating-point formatting, [AM]
            set x [ format "%#.5g" $float ]
            return $x
           }   
            #proc errorx always returns a positive error. 
            #Normally assume $aa is human estimate,
            #assume $bb is divinely exact.
       proc errorx  {aa bb} {expr { $aa > $bb ?   (($aa*1.)/$bb -1.)*100. : (($bb*1.)/$aa -1.)*100.}}
       proc calculate {     } {
            global answer2
            global side1 side2 side3 side4 side5
            global side6 side7 side8 
            global testcase_number
            incr testcase_number 
            set side1 [* $side1 1. ]
            set side2 [* $side2 1. ]
            set side3 [* $side3 1. ]
            set side4 [* $side4 1. ]
            set side5 [* $side5 1. ]
            set side6 [* $side6 1. ]
            set side7 [* $side7 1. ]
            set side8 [* $side8 1. ]
            set target_number $side1
            set trial_square_root $side2
            set formula_factor $side3
            set s $target_number 
            set n $trial_square_root
       #set approximate_root [+ [* [* $n $n ] [+ [* $n $n ] [* 6. $s]]] [* $s $s]]]
       #set approximate_root [+ [* [* $n $n ] [+ [* $n $n ] [* 6. $s]]] [* $s $s]]
       #set approximate_root [/ [+ [* [* $n $n ] [+ [* $n $n ] [* 6. $s]]] [* $s $s]] [* 4. $n [+  $s [*  $n $n ] ] ]  ]
       set approximate_root [/ [+ [* [* $n $n ] [+ [* $n $n ] [* 6. $s]]] [* $s $s]] [* 4. $n [+  $s [*  $n $n ] ] ]  ]
            set side4  $approximate_root
            set side5 [* $trial_square_root $trial_square_root]
            set side6 [sqrt $target_number ]  
            set side7 [ errorx $approximate_root [sqrt  $target_number ]   ]
            set side8 $approximate_root
                    }
        proc fillup {aa bb cc dd ee ff gg hh} {
            .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"
            .frame.entry8 insert 0 "$hh" 
             }
        proc clearx {} {
            foreach i {1 2 3 4 5 6 7 8 } {
                .frame.entry$i delete 0 end } }
        proc reportx {} {
            global answer2
            global side1 side2 side3 side4 side5
            global side6 side7 side8
            global testcase_number
            console show;
            puts "%|table $testcase_number|printed in| tcl wiki format|% "
            puts "&| quantity| value| comment, if any|& "
            puts "&| $testcase_number:|testcase_number | |&"
            puts "&| $side1 :|target number N  |   |&"
            puts "&| $side2 :|trial square root | |& "  
            puts "&| $side3 :|function factor | |& "
            puts "&| $side4 :|answers: intermediate term in formula| |&"
            puts "&| $side5 :|trial square | |&"
            puts "&| $side6 :|square root from TCL sqrt function |  |&"
            puts "&| $side7 :|percentage error |  |&"
            puts "&| $side8 :|approximate square root from square side rule |  |&" 
            }
        frame .buttons -bg aquamarine4
        ::ttk::button .calculator -text "Solve" -command { set side8 0 ; calculate   }
        ::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 2.  2.0  2.0 3.0  4.0   1.414    6.066  1.5}
        ::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 10.0 3.0 2.0 6.333 9.0   3.162 0.138     3.166}
        ::ttk::button .test4 -text "Testcase3" -command {clearx;fillup 100.0 9.0 2.0  20.111  81.0    10.0  0.5555  10.0555}
        ::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 . "Indian Bakhshali Square Root 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.

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. The current result 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. 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   |&"  


console program for brackets of root

                # pretty print from autoindent and ased editor
                # console program for token multiplication and square root
                # additional verbose  
                # working under TCL version 8.5.6 and eTCL 1.0.1
                # program written on Windows XP on eTCL
                # gold on TCL WIKI, 10Mar2017
                package require Tk
                package require math::numtheory
                namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory }
                set tcl_precision 17
                console show
             global keeper target_number keep_under
             set keeper 0
             proc square_root_functionx { number_for_root  } {  
             global keeper target_number keep_under
             set counter 0
             set epsilon .0001
             while { $counter < 1000.  } {
             set keeper [* $counter $counter 1. ] 
             set target_number $number_for_root
             if { [* $counter $counter 1. ]   > [* $number_for_root 1.] } {break}  
             set keep_under $counter         
             incr counter 
             }   
             return $counter}
            puts " function gives [ square_root_functionx 10. ] ,  root for $target_number between $keep_under and  [ square_root_functionx 10. ] , max square $keeper "
            #returns positive integers under and over root.
            # function gives 4 ,  root for 10. between 3 and  4 , max square 16.0 

gold This page is copyrighted under the TCL/TK license terms, this license .

Comments Section

Please place any comments here, Thanks.