Knuth's power tree: Difference between revisions
Content added Content deleted
m (changed a comment in the task's definition section.) |
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations, made other cosmetic changes.) |
||
Line 563: | Line 563: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This REXX version supports results up to 1,000 decimal digits (which can be expanded with the '''numeric digits nnn''' REXX statement |
This REXX version supports results up to 1,000 decimal digits (which can be expanded with the '''numeric digits nnn''' REXX statement. |
||
⚫ | |||
numeric digits 1000 /*be able to handle some large numbers.*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Also, negative powers are supported. |
|||
do e=pL to pH; p=abs(e)/1 /*use a range of powers; use │E│ */ |
|||
⚫ | |||
$=powerTree(p); w=length(pH) /*construct the power tree, (pow list).*/ |
|||
numeric digits 1000 /*be able to handle some large numbers.*/ |
|||
parse arg XP /*get sets: X, low power, high power.*/ |
|||
⚫ | |||
/*────── X LP HP X LP HP X LP ◄── X, low power, high power ··· repeat*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
do e=pL to pH; p=abs(e)/1 /*use a range of powers; use │E│ */ |
|||
$=powerTree(p); w=length(pH) /*construct the power tree, (pow list).*/ |
|||
⚫ | |||
do i=1 for words($); @.i=word($,i) /*build a fast Knuth's power tree array*/ |
|||
⚫ | |||
do |
if p==0 then do; z=1; call show; iterate; end /*handle case of zero power.*/ |
||
!.=.; z=x; !.1=z; prv=z /*define/construct the first power of X*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end /*until nums ···*/ |
|||
⚫ | |||
/*────────────────────────────────POWERTREE subroutine────────────────────────*/ |
|||
⚫ | |||
if y=0 | y=1 then return y /*handle special cases for zero & unity*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end /*while ···*/ |
|||
do k=2 to words($); n=@.k /*obtain the power (number) to be used.*/ |
|||
prev=k-1; diff=n-@.prev /*these are used for the odd powers. */ |
|||
if n//2==0 then z=prv**2 /*Even power? Then square the number.*/ |
|||
else z=z*!.diff /* Odd " " mult. by pow diff.*/ |
|||
!.n=z /*remember for other multiplications. */ |
|||
prv=z /*remember for squaring the numbers. */ |
|||
end /*k*/ |
|||
⚫ | |||
⚫ | |||
end /*e*/ |
|||
end /*until XP ···*/ |
|||
⚫ | |||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
⚫ | |||
#.m=0 |
|||
if y=0 | y=1 then return y /*handle special cases for zero & unity*/ |
|||
⚫ | |||
z=@.y |
|||
/* [↓] add blank "flag" thingy──►list.*/ |
|||
⚫ | |||
return space($) |
|||
⚫ | |||
/*────────────────────────────────SHOW subroutine─────────────────────────────*/ |
|||
⚫ | |||
⚫ | |||
end /*while*/ |
|||
⚫ | |||
⚫ | |||
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=_ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
if q\==0 then do; #.m=q; m=m_; end |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
do while z\==0; $=z $; z=@.z; end /*build power list*/ |
|||
return space($) /*del extra blanks*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
say left('power tree for ' _ " is: " $,60) '═══' x"^"_ ' is: ' z; return</lang> |
|||
'''output''' when using the default inputs: |
'''output''' when using the default inputs: |
||
<pre> |
<pre> |