Knuth's power tree: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: added/changed whitespace, split a compound statement, added a template for the output section. |
|||
Line 1,589:
/*────── X LP HP X LP HP X LP ◄── X, low power, high power ··· repeat*/
do until XP=''
parse var XP x pL pH XP; x= x /
if pH='' then pH=
do e=pL to pH;
$= powerTree(p);
/* [↑] W≡length for an aligned display*/
if p==0 then do; z= 1; call show; iterate; end
!.= .; z= x; !.1= z;
do k=2 to words($); n= @.k
prev= k - 1; diff= n - @.prev
if n//2==0 then z= prv **
else z= z * !.diff
!.n=
prv=
end /*k*/
call show /*display the expression and its value.*/
end /*e*/
end /*until XP ···*/
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
powerTree: arg y 1 oy; $= /*Z is the result; $ is the power tree.*/
if y=0 | y=1 then return y /*handle special cases for zero & unity*/
#.= 0; @.= 0; #.0=
/* [↓] add blank "flag" thingy──►list.*/
do while \(y//2); $=
if y\==oy then $= y $
y= y %
end /*while*/
if $\=='' then $= y $
$= $ oy
if y>1 then do while @.y==0; n= #.0; m= 0
do while n\==0; q= 0; s= n
do while s\==0; _= n + s
if @._==0 then do; if q==0 then m_=
#._= q; @._= n; q= _
end
s= @.s
end /*while s¬==0*/
if q\==0 then do; #.m= q; m= m_; end
n= #.n
end /*while n¬==0*/
#.m= 0
end /*while @.y==0*/
z= @.y
do while z\==0; $= z $; z= @.z; end
return space($) /*del extra blanks*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: if e<0 then z=format(1/z, , 40)/1; _=right(e, w) /*use reciprocal? */
say left('power tree for ' _ " is: " $,60) '═══' x"^"_ ' is: ' z; return</lang>
<pre>
power tree for -4 is: 1 2 4 ═══ 2^-4 is: 0.0625
|