Anonymous user
Function frequency: Difference between revisions
→{{header|REXX}}: added/changed whitespace and comments, add aligned numbers via computation of width, made the frequency counter more idiomatic.
(→{{header|REXX}}: added/changed whitespace and comments, add aligned numbers via computation of width, made the frequency counter more idiomatic.) |
|||
Line 932:
=={{header|REXX}}==
This version doesn't report on the top ten functions (or subroutines), only the functions that are been counted (as implemented below).
<br><br>The use of the '''?.''' stemmed variable is not specific and it can be any (seldom-used, or better yet, unused) REXX variable. ▼
▲
<br>[A question mark was chosen because it most likely won't be used in most REXX programs.]
<br>Also, the counter itself (the array index) should be unique (to avoid REXX variable name collisions).
<lang rexx>/*REXX
if many=='' | many==',' then many=20 /*Not specified? Then use the default.*/
numeric digits 600 /*insure we can handle some big numbers*/
?.=0 /*initialize (et al) the freq counters.*/
do j=1 for many /* [↓] perform this loop MANY times.*/
factorial = !(j)
factorial_R = !
fibonacci = fib(j)
fibonacci_R = fibR(j)
end /*j*/
/*max # of invokes [↓] */
say 'number of invocations for ! (factorial) =
say 'number of invocations for !
say 'number of invocations for Fibonacci =
say 'number of invocations for Fib recursive =
say 'number of invocations for Hofstadter Q =
say 'number of invocations for
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
?: arg _; ?._=?._+1; ?.w=max(?.w, 'LENGTH'(?._)); return
/*────────────────────────────────────────────────────────────────────────────*/
!: procedure expose ?.; ?.!=?.!+1; parse arg x; !=1▼
!: procedure expose ?.; call ? !;
/*────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
▲!r: procedure expose ?.; ?.!r=?.!r+1; parse arg x; if x<2 then return 1
if na<2 then return na /*test for couple special cases.*/
do j=2 to na; s=a+b; a=b; b=s; end▼
fib: procedure expose ?.; ?.fib=?.fib+1; parse arg n; na=abs(n); a=0; b=1▼
/*────────────────────────────────────────────────────────────────────────────*/
▲ do j=2 to na; s=a+b; a=b; b=s; end
▲ if n>0 | na//2==1 then return s /*if positive or odd negative... */
if na<2 then return na;
return (fibR(na-1) + fibR(na-2)) * s▼
/*────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
▲ return (fibR(na-1)+fibR(na-2))*s
<pre>
▲length: procedure expose ?.; ?.length=?.length+1
▲'''output''' when using the input of: <tt> xxx </tt>
▲number of invocations for ! (factorial) = 10
▲number of invocations for ! recursive = 55
▲number of invocations for Fibonacci = 10
▲number of invocations for Fib recursive = 452
▲number of invocations for Hofstadter Q = 1922
▲number of invocations for LENGTH = 30
</pre>
|