Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

7 of 7
Commonmark migration

Languages: 5

POV-Ray 3.7 Scene Description Language: 304 bytes

#fopen I"i"read#declare S=0;#declare N=0;#declare Q=0;#declare P=1;#declare R=0;#while(defined(I))#read(I,V)#declare S=S+V;#declare N=N+1;#declare Q = Q+V*V;#declare P=P*V;#declare R=R+1/V;#end#warning concat(str(N/R,0,5),",",str(pow(P,1/N),0,5),",",str(S/N,0,5),",",str(sqrt(Q/N),0,5),",",str(Q/S,0,5))

(POV-Ray SDL doesn't have console input functions, so I've substituted file input. Output is to the console, but is surrounded by a good deal of program status output.)

Commodore BASIC: (削除) 111 (削除ここまで) 104 bytes

1 P=1:O┐1,0
2 I/#1 V:IF V=0T|G┌4
3 S=S+V:N=N+1:Q=Q+V*V:P=P*V:R=R+1/V:G┌2
4 ?N/R,P↑(1/N),S/N,(Q/N)↑.5,Q/S

(Not all of the characters in this program can be represented in Unicode. | is used to represent SHIFT+H, represents SHIFT+O, represents SHIFT+P, / represents SHIFT+N. Because of the limitations in Commodore Basic I/O, the input is entered one number at a time, with an input of -1 to indicate the end of input. Output is tab-delimited.)

QBasic: 96 bytes

P=1:INPUT V:WHILE V:S=S+V:N=N+1:Q=Q+V*V:P=P*V:R=R+1/V:INPUT V:WEND:?N/R;P^(1/N);S/N;(Q/N)^.5;Q/S

Uses the same I/O scheme as DLosc's entry; I golfed 15 bytes off by using the fact that INPUT V returns 0 (which evaluates to false) when an empty line is input (at least in MS-DOS QBasic 1.1 -- I don't know if it also works in QB64).

Pascal (FPC compiler): 172 bytes

program M;uses math;var v,p,s,n,q,r:real; begin p:=1;while not eoln do begin read(v);s:=s+v;n:=n+1;q:=q+v*v;p:=p*v;r:=r+1/v end;write(n/r,p**(1/n),s/n,(q/n)**0.5,q/s);end.

The input is separated by spaces, not commas, and is newline terminated. Output is space-separated.

Erlang: 401 bytes

-module(means).
-import(io).
-import(math).
-import(string).
-import(lists).
-export([means/6]).
means(S,N,Q,P,R,[]) -> io:fwrite("~f,~f,~f,~f,~f~n", [N/R,math:pow(P,(1/N)),S/N,math:sqrt(Q/N),Q/S]);
means(S,N,Q,P,R,[V|T]) -> means(S+V,N+1,Q+V*V,P*V,R+1/V,T).
means:means(0,0,0,1,0,lists:map(fun({F,R}) -> F end, lists:map(fun(X) -> string:to_float(X) end, string:tokens(io:get_line(""), ",\n")))).

String handling in Erlang is a royal pain. Consequently, all floating-point numbers must be entered with at least one digit after the decimal point -- string:to_float/1 will not convert 1, but will convert 1.0.

(More to come, especially if I figure out how to do this in RoboTalk, a language with neither floating-point operations nor I/O)

Mark
  • 2.4k
  • 17
  • 16

AltStyle によって変換されたページ (->オリジナル) /