Integer roots: Difference between revisions

→‎{{header|REXX}}: added an integer-only example.
mNo edit summary
(→‎{{header|REXX}}: added an integer-only example.)
Line 43:
=={{header|REXX}}==
No error checking is performed to ensure the root is a non-zero integer.
 
<br>Negative and complex roots are supported.
This version incorporates some optimization when computing square roots &nbsp; (because &nbsp; '''M''' &nbsp; is
unity, &nbsp; there is no need to
<br>multiply the guess ['''G'''] by unity, &nbsp; and no need to compute the guess to the 1<sup>st</sup> power, &nbsp; bypassing some trivial arithmetic).
===integer result only===
<lang rexx>/*REXX program calculates the Nth root of a number to a specified number of decimal digs*/
parse arg num root digs . /*obtain the optional arguments from CL*/
if num=='' | num=="," then num= 2 /*Not specified? Then use the default.*/
if root=='' | root=="," then root= 2 /* " " " " " " */
if digs=='' | digs=="," then digs=2001 /* " " " " " " */
numeric digits digs /*utilize this number of decimal digits*/
say 'number=' num /*display the number that will be used.*/
say ' root=' root /* " " root " " " " */
say 'digits=' digs /* " dec. digits " " " " */
say /* " a blank line. */
say 'result:'; say rootI(num, root, digs) /* " what it is; display the root.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
rootI: procedure; parse arg x,root,p /*obtain the numbers, Y is the root #.*/
numeric digits p*root+length(x) /*double the number of digits + guard.*/
if x<2 then return x /*B is one or zero? Return that value.*/
z=x*(10**root)**p /*calculate the number with appended 0s*/
m=root - 1 /*utilize a diminished (by one) power. */
g=(1 + z) % root /*take a stab at the first root guess. */
old=. /* [↓] When M=1, a fast path for sqrt.*/
if m==1 then do until old==g; old=g; g=(g + z % g ) % root; end
else do until old==g; old=g; g=(g*m + z % (g**m) ) % root; end
return left(g,p) /*return the Nth root of Z to invoker.*/</lang>
'''output''' &nbsp; when the defaults are being used:
<pre>
number= 2
root= 2
digits= 2001
 
result:
result:
14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714
70109559971605970274534596862014728517418640889198609552329230484308714321450839762603627995251407989687253396546331808829640620615258352395054745750287759961729835575220337531857011354374603408498847
16038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016
20758474922657226002085584466521458398893944370926591800311388246468157082630100594858704003186480342194897278290641045072636881313739855256117322040245091227700226941127573627280495738108967504018369
86836845072579936472906076299694138047565482372899718032680247442062926912485905218100445984215059112024944134172853147810580360337107730918286931471017111168391658172688941975871658215212822951848847
20896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558
69568685964595155501644724509836896036887323114389415576651040883914292338113206052433629485317049915771756228549741438999188021762430965206564211827316726257539594717255934637238632261482742622208671
15583959992652117625269891754098815934864008345708518147223181420407042650905653233339843645786579679651926729239987536661721598257886026336361782749599421940377775368142621773879919455139723127406689
83299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685
40575867999670121372239475821426306585132217408832382947287617393647467837431960001592188807347857617252211867490424977366929207311096369721608933708661156734585334833295254675851644710757848602463600
8
</pre>
 
===true results===
<br>Negative and complex roots are supported. &nbsp; The expressed root may have a decimal point.
<lang rexx>/*REXX program calculates the Nth root of a number to a specified number of decimal digs*/
parse arg num root digs . /*obtain the optional arguments from CL*/