Revision c38d2beb-a17c-4f87-ac68-c514900eaafb - Code Golf Stack Exchange
#Languages - 3
I always love an excuse to pull out good old
##QBasic, <s>112</s> 96 bytes
g=1:INPUT x:WHILE x:h=h+1/x:g=g*x:a=a+x:q=q+x^2:n=n+1:INPUT x:WEND:?n/h;g^(1/n);a/n;SQR(q/n);q/a
QBasic isn't good with variable numbers of inputs, so the program requires one number per line, terminated with 0 or an empty line. Output is separated with spaces.
(Shortened once I realized that 0 isn't a valid number and can be used for input termination.)
Tested using [QB64](http://en.wikipedia.org/wiki/QB64):
![Testing the QBasic means program][1]
## Common Lisp, 183 bytes
(defun m(l)(let((a(apply #'+ l))(q(apply #'+(map'list #'(lambda(x)(* x x))l)))(n(length l)))(list(/ n(apply #'+(map'list #'/ l)))(expt(apply #'* l)(/ n))(/ a n)(sqrt(/ q n))(/ q a))))
For some reason I expected this to be shorter. I'm not any kind of Lisp expert, so tips are appreciated. Ungolfed version:
(defun means (l)
(let ((a (apply #'+ l)) ; sum of numbers
(q (apply #'+ (map 'list #'(lambda (x) (* x x)) l))) ; sum of squares
(n (length l)))
(list ; Return a list containing:
(/ n (apply #'+ (map 'list #'/ l))) ; n over sum of inverses
(expt (apply #'* l) (/ n)) ; product to the power of 1/n
(/ a n) ; a/n
(sqrt (/ q n)) ; square root of q/n
(/ q a) ; q/a
)
)
)
Probably the best way to test is by pasting the function into the `clisp` REPL, like so:
$ clisp -q
[1]> (defun m(l)(let((a(apply #'+ l))(q(apply #'+(map'list #'(lambda(x)(* x x))l)))(n(length l)))(list(/ n(apply #'+(map'list #'/ l)))(expt(apply #'* l)(/ n))(/ a n)(sqrt(/ q n))(/ q a))))
M
[2]> (m '(1 2 3 4 5))
(300/137 2.6051712 3 3.3166249 11/3)
[3]> (m '(8.6))
(8.6 8.6 8.6 8.6 8.6)
[4]> (m '(3 123456))
(246912/41153 608.5787 123459/2 87296.58 5080461315/41153)
I love how Lisp uses exact fractions instead of floats when dividing two integers.
##Prolog, 235 bytes
Prolog isn't great at math, but we're gonna use it anyway. Tested with SWI-Prolog. I think the `sumlist` predicate may not be standard Prolog, but whatever, I'm using it.
m(L,H,G,A,Q,C):-length(L,N),h(L,I),H is N/I,p(L,P),G is P^(1/N),sumlist(L,S),A is S/N,q(L,R),Q is sqrt(R/N),C is R/S.
p([H|T],R):-p(T,P),R is H*P.
p([],1).
q([H|T],R):-q(T,P),R is H*H+P.
q([],0).
h([H|T],R):-h(T,P),R is 1/H+P.
h([],0).
Ungolfed:
m(L, H, G, A, Q, C) :-
length(L, N), % stores the length into N
h(L, I), % stores the sum of inverses into I
H is N/I,
p(L, P), % stores the product into P
G is P^(1/N),
sumlist(L, S), % stores the sum into S
A is S/N,
q(L, R), % stores the sum of squares into R
Q is sqrt(R/N),
C is R/S.
% Helper predicates:
% p calculates the product of a list
p([H|T], R) :-
p(T, P), % recursively get the product of the tail
R is H*P. % multiply that by the head
p([], 1). % product of empty list is 1
% q calculates the sum of squares of a list
q([H|T], R) :-
q(T, P), % recursively get the sum of squares of the tail
R is H*H+P. % add that to the square of the head
q([], 0). % sum of empty list is 0
% h calculates the sum of inverses of a list
h([H|T], R) :-
h(T, P), % recursively get the sum of inverses of the tail
R is 1/H+P. % add that to the inverse of the head
h([], 0). % sum of empty list is 0
On Linux, with the code in a file called `means.pro`, test like this:
$ swipl -qs means.pro
?- m([1,2,3,4,5],H,G,A,Q,C).
H = 2.18978102189781,
G = 2.605171084697352,
A = 3,
Q = 3.3166247903554,
C = 3.6666666666666665.
Gives a correct but rather amusing result when there's only one number:
?- m([8.6],H,G,A,Q,C).
H = G, G = A, A = Q, Q = C, C = 8.6.
[1]: https://i.sstatic.net/GEDUy.png