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

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Rosetta Stone Challenge: What's Average Anyways?

The goal of a Rosetta Stone Challenge is to write solutions in as many languages as possible. Show off your programming multilingualism!

The Challenge

When people use the term "average," they generally mean the arithmetic mean, which is the sum of the numbers divided by the number of numbers. There are, however, many more meanings to the word "mean," including the harmonic mean, the geometric mean, the arithmetic mean, the quadratic mean, and the contraharmonic mean.

Your challenge is to write a program which inputs a list of numbers and outputs those 5 different means. Additionally, you are trying to write programs in as many languages as possible. You are allowed to use any sort of standard library function that your language has, since this is mostly a language showcase.

Input

Input will be a list of positive numbers.

1,2,3,4,5
1.7,17.3,3.14,24,2.718,1.618
8.6
3,123456
10.1381,29.8481,14.7754,9.3796,44.3052,22.2936,49.5572,4.5940,39.6013,0.9602
3,4,4,6.2,6.2,6.2

Output

Output will be the five means in the order listed above (harmonic, geometric, arithmetic, quadratic, contraharmonic). Conveniently, this is the same as increasing order.

2.18978,2.6052,3,3.31662,3.66667
3.01183,4.62179,8.41267,12.2341,17.7915
8.6,8.6,8.6,8.6,8.6
5.99985,608.579,61729.5,87296.6,123453.
5.95799,14.3041,22.5453,27.9395,34.6243
4.5551,4.74682,4.93333,5.10425,5.28108

There will be some reasonable leniency in I/O format, but I do want several decimal places of accuracy. Since I want floating-point output, you can assume floating-point input.

The Objective Winning Criterion

As for an objective winning criterion, here it is: Each language is a separate competition as to who can write the shortest entry, but the overall winner would be the person who wins the most of these sub-competitions. This means that a person who answers in many uncommon languages can gain an advantage. Code-golf is mostly a tiebreaker for when there is more than one solution in a language: the person with the shortest program gets credit for that language.

If there is a tie, the winner would be the person with the most second-place submissions (and so on).

Rules, Restrictions, and Notes

Your program can be written in any language that existed prior to September 2th, 2014. I will also have to rely on the community to validate some responses written in some of the more uncommon/esoteric languages, since I am unlikely to be able to test them.

Please keep all of your different submissions contained within a single answer.

Also, no shenanigans with basically the same answer in a slightly different language dialects. I will be the judge as to what submissions are different enough.


Current Leaderboard

This section will be periodically updated to show the number of languages and who is leading in each.

  • Algoid (337) - Beta Decay
  • APL (42) - algorithmshark
  • Awk (78) - Dennis
  • BBC BASIC (155) - Beta Decay
  • C (136) - Dennis
  • C++ (195) - Zeta
  • C# (197) - Martin Büttner
  • CJam (43) - Dennis
  • Clojure (324) - Michael Easter
  • Cobra (132) - Ourous
  • CoffeeScript (155) - Martin Büttner
  • Commodore BASIC (104) - Mark
  • Common Lisp (183) - DLosc
  • Erlang (401) - Mark
  • Fortran (242) - Kyle Kanos
  • Fortran 77 (286) - Beta Decay
  • GNU bc (78) - Dennis
  • GolfScript (83) - Dennis
  • Groovy (157) - Michael Easter
  • Haskell (140) - Zeta
  • J (28) - algorithmshark
  • Java (235) - Michael Easter
  • JavaScript (ES6) (112) - Dennis
  • JRuby (538) - Michael Easter
  • Julia (79) - Martin Büttner
  • Lua (113) - AndoDaan
  • Mathematica (65) - Martin Büttner
  • Matlab (63) - Martin Büttner
  • Octave (68) - Dennis
  • Openscript (849?) - COTO
  • Pascal (172) - Mark
  • Perl (76) - Grimy
  • PHP (135) - Dennis
  • POV-Ray 3.7 (304) - Mark
  • Prolog (235) - DLosc
  • Pyth (52) - Dennis
  • Python 2 (96) - Dennis
  • Python 3 (103) - DLosc
  • Q (53) - algorithmshark
  • Q'Nial (68) - algorithmshark
  • QBasic (96) - DLosc
  • R (91) - plannapus
  • Ruby (118) - Martin Büttner
  • Rust (469) - Vi.
  • Scala (230) - Michael Easter
  • T-SQL (122) - MickyT
  • TI-Basic (85) - Ypnypn
  • TypeScript (393) - rink.attendant.6
  • VBA (Excel) (387) - Stretch Maniac
  • wxMaxima (134) - Kyle Kanos

Current User Rankings

  1. Dennis (10)
  2. Martin Büttner (6)
  3. Michael Easter (5)
  4. Mark, DLosc, algorithmshark (4)
  5. Beta Decay (3)
  6. Zeta, Kyle Kanos (2)
  7. Ourous, AndoDaan, COTO, Grimy, plannapus, Vi., MickyT, Ypnypn, rink.attendant.6, Stretch Maniac (1)

(If I made a mistake in the above rankings, let me know and I'll fix it. Also, the tiebreaker has not been applied yet.)

Answer*

Draft saved
Draft discarded
Cancel
2
  • \$\begingroup\$ Can you add the sample output for Java? How many decimal places of accuracy does it give compared to the output in OP? \$\endgroup\$ Commented Sep 3, 2014 at 9:21
  • \$\begingroup\$ Trivially shorter for TI-BASIC is {dim(Ans)/sum(Ansֿ¹),dim(Ans)√(prod(Ans)),mean(Ans),√(mean(Ans²)),mean(Ans²)/mean(Ans \$\endgroup\$ Commented Jun 8, 2015 at 1:56

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