Square root by hand: Difference between revisions

→‎{{header|REXX}}: cleaned up code so that if the square root is found, it stops spitting out zeroes.
m (→‎{{header|REXX}}: changed a comment, increased the width of the terminal.)
(→‎{{header|REXX}}: cleaned up code so that if the square root is found, it stops spitting out zeroes.)
Line 289:
parse arg xx digs . /*obtain optional arguments from the CL*/
if xx=='' | xx=="," then xx= 2 /*Not specified? Then use the default.*/
if digs=='' | digs=="," then digs= 400500 /* " " " " " " */
numeric digits digs + digs % 2 /*ensure enough decimal digits for calc*/
call sqrtHand xx, digs /*invoke the function for sqrt by hand.*/
Line 299:
return r /*R is the integer square root of Z. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrtHandspit: parse arg x@; call charout ,places @; parse value iSqrt(x)if with#<9 then s= s || @; j 1 k 1 ? /*j, k, ? iSqrt(x) */return
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrtHand: parse arg x 1 ox,##; parse value iSqrt(x) with j 1 k 1 ? /*j, k, ? ≡ iSqrt(x)*/
if ?==0 then ?= /*handle the case of sqrt < 1. */
if j*j=x then do; say j; return; end /*have we found the exact sqrt?*/
L= length(?) /*L: used to place dec. point.*/
if Ls==0; then call charout , . #= 0 /*handleR: dec. pointpartial forsquare Xroot. < 1. .*/
if L==0 then call spit . do spit=1 /*handle dec. point for placesX < 1. */
calldo charout#=1 , for ##; call spit ? /*spit out the first digit. */
if L>0 then do; call charout ,spit .; L= 0; end /*process dec.decimal point.*/
if ?==''#<9 then ?= 0 if datatype(s,'N') then if s*s=ox then leave /*ensure theexact√ ? is a valid dig.*/
if ?=='' then ?= 0 /*ensure ? is a valid digit.*/
x= (x - k*?) * 100; ?= 1
k= j * 20
Line 316 ⟶ 319:
j= ? + j*10
k= ? + k
end /*spit#*/
return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
Line 331 ⟶ 334:
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> 10.89 &nbsp; 80 </tt>}}
<pre>
3.3
3.3000000000000000000000000000000000000000000000000000000000000000000000000000000
</pre>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> 625 </tt>}}