Averages/Pythagorean means: Difference between revisions
Content added Content deleted
(→{{header|Kotlin}}: kotlin/js compatability, harmonic mean: reduce to fold since first element is not divided) |
m (→{{header|REXX}}: added/changed whitespace and comments, used a template for the output sections.) |
||
Line 2,817: | Line 2,817: | ||
numeric digits 20 /*use a little extra for the precision.*/ |
numeric digits 20 /*use a little extra for the precision.*/ |
||
parse arg n . /*obtain the optional argument from CL.*/ |
parse arg n . /*obtain the optional argument from CL.*/ |
||
if n=='' | n=="," then n= |
if n=='' | n=="," then n= 10 /*None specified? Then use the default*/ |
||
sum=0; prod=1; rSum= |
sum= 0; prod= 1; rSum= 0 /*initialize sum/product/reciprocal sum*/ |
||
$=; do #=1 for n; |
$=; do #=1 for n; $= $ # /*generate list by appending # to list.*/ |
||
sum = sum + |
sum = sum + # /*compute the sum of all the elements. */ |
||
prod= prod * |
prod= prod * # /*compute the product of all elements. */ |
||
rSum= rSum + |
rSum= rSum + 1/# /*compute the sum of the reciprocals. */ |
||
end /*#*/ |
end /*#*/ |
||
say ' list ='$ /*display the list of numbers used. */ |
say ' list ='$ /*display the list of numbers used. */ |
||
say 'Amean =' sum / n /*calculate & display arithmetic mean.*/ |
say 'Amean =' sum / n /*calculate & display arithmetic mean.*/ |
||
say 'Gmean =' Iroot(prod, n) /* " " " geometric " */ |
say 'Gmean =' Iroot(prod, n) /* " " " geometric " */ |
||
⚫ | |||
say 'Hmean =' n / rSum /* " " " harmonic " */ |
say 'Hmean =' n / rSum /* " " " harmonic " */ |
||
exit |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Iroot: procedure; arg x 1 ox, y 1 oy |
Iroot: procedure; parse arg x 1 ox, y 1 oy /*get both args, and also a copy of X&Y*/ |
||
if x=0 | x=1 | y=1 then return x |
if x=0 | x=1 | y=1 then return x /*handle special case of zero and unity*/ |
||
if y=0 then return 1 /* " " " " a zero root.*/ |
|||
if x<0 & y//2==0 then return |
if x<0 & y//2==0 then return '[n/a]' /*indicate result is "not applicable". */ |
||
x=abs(x); y=abs(y); |
x= abs(x); y= abs(y); m= y - 1 /*use the absolute value for X and Y. */ |
||
oDigs=digits(); a=oDigs + 5 |
oDigs= digits(); a= oDigs + 5 /*save original digits; add five digs.*/ |
||
g=(x+1) / y* |
g= (x+1) / y*2 /*use this as the first guesstimate. */ |
||
d= 5 /*start with 5 dec digs, saves CPU time*/ |
|||
do until d==a; d= min(d + d, a) /*keep going as digits are increased. */ |
|||
numeric digits d; f= d - 2 /*limit digits to original digits + 5.*/ |
|||
og= /*use a non─guess for the old G (guess)*/ |
|||
do forever; gm= g**m /*keep computing at the Yth root. */ |
|||
_=format( (m*g*gm + x) |
_= format( (m*g*gm + x)/(y*gm),,f) /*this is the nitty─gritty calculation.*/ |
||
if _=g | _=og then leave |
if _=g | _=og then leave /*are we close enough yet? */ |
||
og= g; g= _ /*save guess ──► OG; set the new guess.*/ |
|||
end /*forever*/ |
end /*forever*/ |
||
end /*until */ |
|||
g= g * sign(ox); if oy<0 then g= 1 / g /*adjust for original X sign; neg. root*/ |
|||
numeric digits oDigs; return |
numeric digits oDigs; return g / 1 /*normalize to original decimal digits.*/</lang> |
||
⚫ | |||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
⚫ | |||
return '[n/a]' /*return a "not applicable" string. */</lang> |
|||
⚫ | |||
<pre> |
<pre> |
||
list = 1 2 3 4 5 6 7 8 9 10 |
list = 1 2 3 4 5 6 7 8 9 10 |