Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,43 @@
|
|||
PROGRAM MEANS
|
||||
|
||||
DIM A[9]
|
||||
|
||||
PROCEDURE ARITHMETIC_MEAN(A[]->M)
|
||||
LOCAL S,I%
|
||||
NEL%=UBOUND(A,1)
|
||||
S=0
|
||||
FOR I%=0 TO NEL% DO
|
||||
S+=A[I%]
|
||||
END FOR
|
||||
M=S/(NEL%+1)
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE GEOMETRIC_MEAN(A[]->M)
|
||||
LOCAL S,I%
|
||||
NEL%=UBOUND(A,1)
|
||||
S=1
|
||||
FOR I%=0 TO NEL% DO
|
||||
S*=A[I%]
|
||||
END FOR
|
||||
M=S^(1/(NEL%+1))
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE HARMONIC_MEAN(A[]->M)
|
||||
LOCAL S,I%
|
||||
NEL%=UBOUND(A,1)
|
||||
S=0
|
||||
FOR I%=0 TO NEL% DO
|
||||
S+=1/A[I%]
|
||||
END FOR
|
||||
M=(NEL%+1)/S
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
A[]=(1,2,3,4,5,6,7,8,9,10)
|
||||
ARITHMETIC_MEAN(A[]->M)
|
||||
PRINT("Arithmetic mean = ";M)
|
||||
GEOMETRIC_MEAN(A[]->M)
|
||||
PRINT("Geometric mean = ";M)
|
||||
HARMONIC_MEAN(A[]->M)
|
||||
PRINT("Harmonic mean = ";M)
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
(define (A xs) (// (for/sum ((x xs)) x) (length xs)))
|
||||
|
||||
(define (G xs) (expt (for/product ((x xs)) x) (// (length xs))))
|
||||
|
||||
(define (H xs) (// (length xs) (for/sum ((x xs)) (// x))))
|
||||
|
||||
(define xs (range 1 11))
|
||||
(and (>= (A xs) (G xs)) (>= (G xs) (H xs)))
|
||||
→ #t
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Function ArithmeticMean(array() As Double) As Double
|
||||
Dim length As Integer = Ubound(array) - Lbound(array) + 1
|
||||
Dim As Double sum = 0.0
|
||||
For i As Integer = LBound(array) To UBound(array)
|
||||
sum += array(i)
|
||||
Next
|
||||
Return sum/length
|
||||
End Function
|
||||
|
||||
Function GeometricMean(array() As Double) As Double
|
||||
Dim length As Integer = Ubound(array) - Lbound(array) + 1
|
||||
Dim As Double product = 1.0
|
||||
For i As Integer = LBound(array) To UBound(array)
|
||||
product *= array(i)
|
||||
Next
|
||||
Return product ^ (1.0 / length)
|
||||
End Function
|
||||
|
||||
Function HarmonicMean(array() As Double) As Double
|
||||
Dim length As Integer = Ubound(array) - Lbound(array) + 1
|
||||
Dim As Double sum = 0.0
|
||||
For i As Integer = LBound(array) To UBound(array)
|
||||
sum += 1.0 / array(i)
|
||||
Next
|
||||
Return length / sum
|
||||
End Function
|
||||
|
||||
Dim vector(1 To 10) As Double
|
||||
For i As Integer = 1 To 10
|
||||
vector(i) = i
|
||||
Next
|
||||
|
||||
Print "Arithmetic mean is :"; ArithmeticMean(vector())
|
||||
Print "Geometric mean is :"; GeometricMean(vector())
|
||||
Print "Harmonic mean is :"; HarmonicMean(vector())
|
||||
Print
|
||||
Print "Press any key to quit the program"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
import lists.zip
|
||||
|
||||
def
|
||||
mean( s, 0 ) = product( s )^(1/s.length())
|
||||
mean( s, p ) = (1/s.length() sum( x^p | x <- s ))^(1/p)
|
||||
|
||||
def
|
||||
monotone( [_], _ ) = true
|
||||
monotone( a1:a2:as, p ) = p( a1, a2 ) and monotone( a2:as, p )
|
||||
|
||||
means = [mean( 1..10, m ) | m <- [1, 0, -1]]
|
||||
|
||||
for (m, l) <- zip( means, ['Arithmetic', 'Geometric', 'Harmonic'] )
|
||||
println( "$l: $m" + (if m is Rational then " or ${m.doubleValue()}" else '') )
|
||||
|
||||
println( monotone(means, (>=)) )
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
fun arithmetic_mean(as: [n]f64): f64 =
|
||||
reduce (+) 0.0 (map (/f64(n)) as)
|
||||
|
||||
fun geometric_mean(as: [n]f64): f64 =
|
||||
reduce (*) 1.0 (map (**(1.0/f64(n))) as)
|
||||
|
||||
fun harmonic_mean(as: [n]f64): f64 =
|
||||
f64(n) / reduce (+) 0.0 (map (1.0/) as)
|
||||
|
||||
fun main(as: [n]f64): (f64,f64,f64) =
|
||||
(arithmetic_mean as,
|
||||
geometric_mean as,
|
||||
harmonic_mean as)
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
define arithmetic_mean(a::staticarray)::decimal => {
|
||||
//sum of the list divided by its length
|
||||
return (with e in #a sum #e) / decimal(#a->size)
|
||||
}
|
||||
define geometric_mean(a::staticarray)::decimal => {
|
||||
// The geometric mean is the nth root of the product of the list
|
||||
local(prod = 1)
|
||||
with e in #a do => { #prod *= #e }
|
||||
return math_pow(#prod,1/decimal(#a->size))
|
||||
}
|
||||
define harmonic_mean(a::staticarray)::decimal => {
|
||||
// The harmonic mean is n divided by the sum of the reciprocal of each item in the list
|
||||
return decimal(#a->size)/(with e in #a sum 1/decimal(#e))
|
||||
}
|
||||
|
||||
arithmetic_mean(generateSeries(1,10)->asStaticArray)
|
||||
geometric_mean(generateSeries(1,10)->asStaticArray)
|
||||
harmonic_mean(generateSeries(1,10)->asStaticArray)
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
import math, sequtils, future
|
||||
|
||||
proc amean(num): float =
|
||||
sum(num) / float(len(num))
|
||||
|
||||
proc gmean(num): float =
|
||||
result = 1
|
||||
for n in num: result *= n
|
||||
result = pow(result, 1.0 / float(num.len))
|
||||
|
||||
proc hmean(num): float =
|
||||
for n in num: result += 1.0 / n
|
||||
result = float(num.len) / result
|
||||
|
||||
proc ameanFunctional(num: seq[float]): float =
|
||||
sum(num) / float(num.len)
|
||||
|
||||
proc gmeanFunctional(num: seq[float]): float =
|
||||
num.foldl(a * b).pow(1.0 / float(num.len))
|
||||
|
||||
proc hmeanFunctional(num: seq[float]): float =
|
||||
float(num.len) / sum(num.mapIt(float, 1.0 / it))
|
||||
|
||||
let numbers = toSeq(1..10).map((x: int) => float(x))
|
||||
echo amean(numbers), " ", gmean(numbers), " ", hmean(numbers)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
: A(l) l avg ;
|
||||
: G(l) l prod l size inv powf ;
|
||||
: H(l) l size l map(#inv) sum / ;
|
||||
|
||||
: averages
|
||||
| g |
|
||||
"Geometric mean :" . G(10 seq) dup .cr ->g
|
||||
"Arithmetic mean :" . A(10 seq) dup . g >= ifTrue: [ " ==> A >= G" .cr ]
|
||||
"Harmonic mean :" . H(10 seq) dup . g <= ifTrue: [ " ==> G >= H" .cr ] ;
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
function arithmetic_mean(sequence s)
|
||||
return sum(s)/length(s)
|
||||
end function
|
||||
|
||||
function geometric_mean(sequence s)
|
||||
atom p = 1
|
||||
for i=1 to length(s) do
|
||||
p *= s[i]
|
||||
end for
|
||||
return power(p,1/length(s))
|
||||
end function
|
||||
|
||||
function harmonic_mean(sequence s)
|
||||
atom rsum = 0
|
||||
for i=1 to length(s) do
|
||||
rsum += 1/s[i]
|
||||
end for
|
||||
return length(s)/rsum
|
||||
end function
|
||||
|
||||
function iff(integer condition, object Tval, object Fval)
|
||||
if condition then return Tval else return Fval end if
|
||||
end function
|
||||
|
||||
constant s = {1,2,3,4,5,6,7,8,9,10}
|
||||
constant arithmetic = arithmetic_mean(s),
|
||||
geometric = geometric_mean(s),
|
||||
harmonic = harmonic_mean(s)
|
||||
printf(1,"Arithmetic: %.10g\n", arithmetic)
|
||||
printf(1,"Geometric: %.10g\n", geometric)
|
||||
printf(1,"Harmonic: %.10g\n", harmonic)
|
||||
printf(1,"Arithmetic>=Geometric>=Harmonic: %s\n", {iff((arithmetic>=geometric and geometric>=harmonic),"true","false")})
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
decimals(8)
|
||||
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
see "arithmetic mean = " + arithmeticMean(array) + nl
|
||||
see "geometric mean = " + geometricMean(array) + nl
|
||||
see "harmonic mean = " + harmonicMean(array) + nl
|
||||
|
||||
func arithmeticMean a
|
||||
return summary(a) / len(a)
|
||||
|
||||
func geometricMean a
|
||||
b = 1
|
||||
for i = 1 to len(a)
|
||||
b *= a[i]
|
||||
next
|
||||
return pow(b, (1/len(a)))
|
||||
|
||||
func harmonicMean a
|
||||
b = list(len(a))
|
||||
for nr = 1 to len(a)
|
||||
b[nr] = 1/a[nr]
|
||||
next
|
||||
return len(a) / summary(b)
|
||||
|
||||
func summary s
|
||||
sum = 0
|
||||
for n = 1 to len(s)
|
||||
sum += s[n]
|
||||
next
|
||||
return sum
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
func A(a) { a.sum / a.len }
|
||||
func G(a) { a.prod.root(a.len) }
|
||||
func H(a) { a.len / a.map{1/_}.sum }
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
func A(a) { a«+» / a.len }
|
||||
func G(a) { a«*» ** (1/a.len) }
|
||||
func H(a) { a.len / (a«/«1 «+») }
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
say("A(1,...,10) = ", A(1..10));
|
||||
say("G(1,...,10) = ", G(1..10));
|
||||
say("H(1,...,10) = ", H(1..10));
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
def amean: add/length;
|
||||
|
||||
def logProduct: map(log) | add;
|
||||
|
||||
def gmean: (logProduct / length) | exp;
|
||||
|
||||
def hmean: length / (map(1/.) | add);
|
||||
|
||||
# Tasks:
|
||||
[range(1;11) ] | [amean, gmean, hmean] as $ans
|
||||
| ( $ans[],
|
||||
"amean > gmean > hmean => \($ans[0] > $ans[1] and $ans[1] > $ans[2] )" )
|
||||
Loading…
Add table
Add a link
Reference in a new issue