Averages/Pythagorean means: Difference between revisions

imported>Regattaguru
 
(10 intermediate revisions by 6 users not shown)
Line 638:
Geometric mean = 4.52872869
Harmonic mean = 3.41417152</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">A ← +´÷≠
G ← ≠√×´
H ← A⌾÷
 
⋈⟜(∧´ ¯1⊸↓ ≥ 1⊸↓) (A∾G∾H) 1+↕10</syntaxhighlight>
{{out}}
<pre>⟨ ⟨ 5.5 4.528728688116765 3.414171521474055 ⟩ 1 ⟩</pre>
 
=={{header|C}}==
Line 806 ⟶ 815:
let sum = sum + i
let bxsum = bxsum * i
let sum1i = sum1i + ( 1 / i )
 
next i
 
let average = sum / 10
let geometric = bxsum ^ ( 1 / 10 )
let harmonic = 10 / sum1i
 
Line 825 ⟶ 834:
endif
 
print "false"</syntaxhighlight>
{{out| Output}}<pre>
end</syntaxhighlight>
arithmetic mean: 5.500000
geometric mean: 4.528729
harmonic mean: 3.414172
true
</pre>
 
=={{header|D}}==
Line 932 ⟶ 946:
? H(1..10)
# value: 3.414171521474055</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
proc mean . v[] a g h .
prod = 1
for v in v[]
sum += v
prod *= v
resum += 1 / v
.
a = sum / len v[]
g = pow prod (1 / len v[])
h = len v[] / resum
.
a[] = [ 1 2 3 4 5 6 7 8 9 10 ]
mean a[] a g h
print a
print g
print h
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,380 ⟶ 1,415:
harmonic_mean as)
</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="FutureBasic>
 
Double ari = 1, geo = 0, har = 0
Short i, n = 10
 
for i = 1 to n
ari += i
geo *= i
har += 1 \ i
next
 
print "ari:", ari \ n
print "geo:", geo^( 1 \ n )
print "har:", n \ har
 
handleevents
</syntaxhighlight>
<pre>
 
ari: 5.5
geo: 4.528728688116765
har: 3.414171521474055
 
</pre>
 
=={{header|GAP}}==
Line 3,182 ⟶ 3,243:
=={{header|RPL}}==
These words can be used either on vectors or lists.
{{works with|HP|28}}
≪ → array op
≪ array 1 GET 2 array SIZE
'''IF''' DUP2 > '''THEN''' DROP2 '''ELSE FOR''' j array GET op EVAL '''NEXT END'''
≫ ≫ '<span style="color:blue">REDUCE</span>' STO
≪ DUP ≪ + ≫ <span style="color:blue">REDUCE</span> SWAP SIZE /
≫ '<span style="color:blue">AMEAN</span>' STO
≪ DUP ≪ * ≫ <span style="color:blue">REDUCE</span> SWAP SIZE INV ^
≫ '<span style="color:blue">GMEAN</span>' STO
DUPSIZE LAST ≪ INV + ≫ <span style="color:blue">REDUCE</span> INV SWAP SIZE */
≫ '<span style="color:blue">HMEAN</span>' STO
 
{ 1 2 3 4 5 6 7 8 9 0 } <span style="color:blue">AMEAN</span>
{ 1 2 3 4 5 6 7 8 9 0 } <span style="color:blue">GMEAN</span>
[ 1 2 3 4 5 6 7 8 9 0 ] <span style="color:blue">HMEAN</span>
{{out}}
<pre>
Line 3,205 ⟶ 3,267:
1: 3.41417152147
</pre>
 
=={{header|Ruby}}==
{{works with|Ruby|1.9+}}
Line 3,472 ⟶ 3,535:
| Harmonic 10 3.414172 2.035664 10.57602
-----------------------------------------------------------------------------</syntaxhighlight>
=={{header|Swift}}==
<syntaxhighlight lang="text">
// Utility for easy creation of Double from any Numeric
extension Double {
init(withNum v: any Numeric) {
switch v {
case let ii as any BinaryInteger: self.init(ii)
case let ff as any BinaryFloatingPoint: self.init(ff)
default: self.init()
}
}
}
// Extension for numeric collections
extension Collection where Element: Numeric {
var arithmeticMean: Double {
self.reduce(0.0, {$0 + Double(withNum: $1)})/Double(self.count)
}
var geometricMean: Double {
pow(self.reduce(1.0, {$0 * Double(withNum: $1)}), 1.0/Double(self.count))
}
var harmonicMean: Double {
Double(self.count) / self.reduce(0.0, {$0 + 1.0/Double(withNum:$1)})
}
}
//Usage:
var c: [Int] = (1...10).map {$0}
 
print(c.arithmeticMean)
print(c.geometricMean)
print(c.harmonicMean)
 
// output:
// 5.5
// 4.528728688116765
// 3.414171521474055
</syntaxhighlight>
 
=={{header|Tcl}}==
Line 3,717 ⟶ 3,816:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var rng = 1..10
var count = rng.count
var A = rng.reduce { |acc, x| acc + x }/count
Anonymous user