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

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 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 was 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 was 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 was 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. Square roots below 1000 seemed to have unacceptable error. Possibly because of selecting ((tcl_sqrt(N))+1) as the trial square root, meaning the simple error measure would see relatively greater percentage distance in the lower numbers.

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

Testcase 4

table 4printed in tcl wiki format
quantity value comment, if any
4:testcase_number
100.0 :target number N
11.0 :trial square root
2.0 :function factor
10.000102838338133 :answers: intermediate term in formula
121.0 :trial square
10.0 :square root from TCL sqrt function
0.0010283833813407028 :percentage error
10.000102838338133 :approximate square root from Bakhshali formula

Testcase 5

table 5printed in tcl wiki format
quantity value comment, if any
5:testcase_number
400.0 :target number N
21.0 :trial square root
2.0 :function factor
20.000014155483836 :answers: intermediate term in formula
441.0 :trial square
20.0 :square root from TCL sqrt function
7.0777419169942846e-05 :percentage error
20.000014155483836 :approximate square root from Bakhshali formula

Testcase 6

table 6printed in tcl wiki format
quantity value comment, if any
6:testcase_number
2.0 :target number N
1.0 :trial square root
2.0 :function factor
1.4166666666666667 :answers: intermediate term in formula
1.0 :trial square
1.4142135623730951 :square root from TCL sqrt function
0.17346066809422744 :percentage error
1.4166666666666667 :approximate square root from Bakhshali formula

Screenshots Section

figure 1. 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
# comment follows from gold, 12Dec2018
# pretty print from autoindent and ased editor
# Indian Bakhshali Square Root Calculator V2
# written on Windows XP on TCL
# working under TCL version 8.6
# Revamping older program from 2017.
# Indian Bakhshali Square Root is of
# historical interest, but not particularly
# accurate.
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
set msg "Calculator for Indian Bakhshali Square Root
from TCL ,
# gold on  TCL Club, 12Dec2018 "
tk_messageBox -title "About" -message \$msg }
proc self_help {} {
set msg " Indian Bakhshali Square Root V2
from TCL Club ,
# self help listing
# problem, Indian Bakhshali Square Root V2
# 3 givens follow.
1) target number N:
2) trial square root:
3) formula factor:
# 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 to conventional texteditor.
# For testcases, testcase number is internal
# to the calculator and will not be printed
# until the report button is pushed
# for the current result numbers.
# This posting, screenshots, and TCL source code is
# Editorial rights and disclaimers
# retained under the TCL/TK license terms
# and will be defended as necessary in court.
Conventional text editor formulas
or  formulas grabbed from internet
screens can be pasted into green console.
# gold on  TCL Club, 12Dec2018 "
tk_messageBox -title "Self_Help" -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 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]] [* 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 side1 side2 side3 side4 side5
global side6 side7 side8
global testcase_number
console eval {.console config -bg palegreen}
console eval {.console config -font {fixed 20 bold}}
console eval {wm geometry . 40x20}
console eval {wm title . " Indian Bakhshali Square Root V2 Report, screen grab and paste from console 2 to texteditor"}
console eval {. configure -background orange -highlightcolor brown -relief raised -border 30}
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 from Bakhshali formula |  |&"
}
frame .buttons -bg aquamarine4
::ttk::button .calculator -text "Solve" -command { set side8 0 ; calculate   }
::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 3.  2.0  2.0 1.72  4.0   1.732    0.005366  1.732}
::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 216.E3 465.0 2.0 464.758 216225.0   464.758 9.E-13  464.758}
::ttk::button .test4 -text "Testcase3" -command {clearx;fillup 1296.E4 3601.0 2.0  3600.0  12967201.0    3600.0  6.E-14  3600.}
::ttk::button .clearallx -text clear -command {clearx }
::ttk::button .self_help -text self_help -command { self_help }
::ttk::button .cons -text report -command { reportx }
::ttk::button .exit -text exit -command {exit}
pack  .clearallx .cons .self_help .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 V2"

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
# 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