Metropoli BBS
VIEWER: sqrt.txt MODE: TEXT (ASCII)
a # 1 60 R ' z#sqrt a;f;n;rem                                           '
a # a,[1]  '"multiprecision integer square root by elementary recursion '
a # a,[1]  '"get an accurate starting value                             '
a # a,[1]  ' z#(n#1+D0.5XRa)Ycan(10000000B(6+2|Ra)Ya#fix a)*0.5         '
a # a,[1]  '"drop trailing zeros from a and double it                   '
a # a,[1]  ' $0XI0=1Y1Ua#can 2X((Ra)+Lio_(`O0^=a)I1)Ya                  '
a # a,[1]  'loop:f#((n+Ra)Yz mul z)div a                                '
a # a,[1]  ' z#nYz mul f#((1+Rf)Y 0 1 5000000)sub f                     '
a # a,[1]  ' $(1<+/nYf)/loop                                            '
a # a,[1]  ' $0                                                         '
a # a,[1]  '"sqrt - multiprecision integer square root                  '
a # a,[1]  '"from ''the apl handbook of techniques'' copyright 1978, ibm. '
a # a,[1]  '"-a may be scalar or multiprecision but will be truncated to'
a # a,[1]  '" an integer.                                               '
a # a,[1]  '"-running time is proportional to (Ra)X@Ra, and is much     '
a # a,[1]  '" faster for numbers with many trailing zeros.              '
a # a,[1]  '"-uses: Gfix Gsub Gmul Gdiv Gcan                            '
Lfx a
[ RETURN TO DIRECTORY ]