12
\$\begingroup\$

There are quite a few means in mathematics, such as the arithmetic mean, the geometric mean, and many others...

Definitions and Task

Note that these are the definitions for two positive integers*:

  • The root mean square is the square root of the sum of their squares halved ().

  • The arithmetic mean is their sum, halved ().

  • The geometric mean is the square root of their product ().

  • The harmonic mean is 2 divided by the sum of their inverses ( = ).

Given two integers a and b such that a, b ∈ [1, +∞), sum the means mentioned above of a and b. Your answers must be accurate to at least 3 decimal places, but you do not have to worry about rounding or floating-point precision errors.

Test Cases

a, b -> Output

7, 6 -> 25.961481565148972
10, 10 -> 40
23, 1 -> 34.99131878607909
2, 4 -> 11.657371451581236
345, 192 -> 1051.7606599443843

You can see the correct results for more test cases using this program. This is , so the shortest valid submissions that follows the standard rules wins.

* There are many other means, but for the purposes of this challenge we'll use the ones mentioned in the "Definitions" section.

\$\endgroup\$
8
  • \$\begingroup\$ Related. \$\endgroup\$ Commented Oct 15, 2017 at 12:18
  • 10
    \$\begingroup\$ Must've asked to output the mean of means. -1 (not). \$\endgroup\$ Commented Oct 15, 2017 at 12:21
  • 9
    \$\begingroup\$ At least there is no Mathematica builtin for that. Right? \$\endgroup\$
    – Maya
    Commented Oct 15, 2017 at 12:21
  • \$\begingroup\$ @NieDzejkob I don't think so :-) \$\endgroup\$
    – Mr. Xcoder
    Commented Oct 15, 2017 at 12:21
  • \$\begingroup\$ @NieDzejkob Although I suspect there are builtins for each one of the means. \$\endgroup\$ Commented Oct 15, 2017 at 12:25

23 Answers 23

13
\$\begingroup\$

Haskell, 48 bytes

a%b=sum[((a**p+b**p)/2)**(1/p)|p<-[2,1,-1,1e-9]]

Try it online!

This uses the fact that the root-square, arithmetic, harmonic, and geometric means are all special cases of the generalized mean ((a**p+b**p)/2)**(1/p) for p=2,1,-1,0. The geometric mean uses the limit p->0+, approximated as p=1e-9 which suffices for precision.

\$\endgroup\$
9
\$\begingroup\$

Mathematica, 37 bytes

-2 bytes thanks to Martin Ender. -6 bytes thanks to Jenny_mathy and function reusability thanks to JungHwan Min.

(t=1##)^.5+(2(s=+##/2)^2-t)^.5+s+t/s&

Try it online!

Mathematica, 55 bytes

RootMeanSquare@#+Mean@#+GeometricMean@#+HarmonicMean@#&

Try it online!

¯\_(ツ)_/¯

\$\endgroup\$
9
  • 1
    \$\begingroup\$ An alternative: ((#^2+#2^2)/2)^.5+(#+#2)/2+(#1#2)^.5+2#*#2/(#+#2)& \$\endgroup\$
    – Mr. Xcoder
    Commented Oct 15, 2017 at 12:50
  • 1
    \$\begingroup\$ 2 bytes off: ((#^2+#2^2)/2)^.5+(+##)/2+(1##)^.5+2/(1/#+1/#2)& \$\endgroup\$ Commented Oct 15, 2017 at 12:59
  • 2
    \$\begingroup\$ 42 bytes: (((s=+##)^2-2##)/2)^.5+s/2+(1##)^.5+2##/s& \$\endgroup\$
    – ZaMoC
    Commented Oct 15, 2017 at 18:03
  • 6
    \$\begingroup\$ 37 bytes: (2(s=+##/2)^2-t)^.5+s+(t=1##)^.5+t/s& \$\endgroup\$
    – ZaMoC
    Commented Oct 15, 2017 at 18:42
  • 2
    \$\begingroup\$ A slight fix to @Jenny_mathy's version (same byte count): (t=1##)^.5+(2(s=+##/2)^2-t)^.5+s+t/s&. Just to make it easier to reuse the function (without having to run Clear@t before each iteration). \$\endgroup\$ Commented Oct 15, 2017 at 19:26
5
\$\begingroup\$

Python 3, 57 bytes

lambda a,b:(a+b+(a*a+b*b<<1)**.5)/2+(a*b)**.5+2*a*b/(a+b)

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ The <<1 is going to incorrectly truncate to an integer when a and b are opposite parities. \$\endgroup\$
    – xnor
    Commented Oct 15, 2017 at 14:35
  • \$\begingroup\$ @xnor No it's not :) You're thinking of >>1. \$\endgroup\$
    – orlp
    Commented Oct 15, 2017 at 14:35
  • 1
    \$\begingroup\$ Oh, my mistake! I see now there's a /2 outside that this is compensating for. Nice trick. \$\endgroup\$
    – xnor
    Commented Oct 15, 2017 at 14:39
4
\$\begingroup\$

R, 52 bytes

function(a,b,m=(a+b)/2,p=a*b)m+p^.5+(m^2*2-p)^.5+p/m

Try it online!

\$\endgroup\$
0
3
\$\begingroup\$

Haskell, 48 bytes

a?b|s<-a+b,p<-a*b=s/2+sqrt(s^2/2-p)+sqrt p+2*p/s

Try it online!

Explanation:

s/2 = (a+b)/2: The arithmetic mean.

sqrt(s^2/2-p) = sqrt((a^2+2*a*b+b^2)/2-a*b) = sqrt((a^2+b^2)/2): The root mean square.

sqrt p = sqrt(a*b). The geometric mean.

2*p/s = 2*a*b/(a+b). The harmonic mean.

\$\endgroup\$
3
\$\begingroup\$

Octave, 44 42 41 bytes

@(n)(q=mean(n))+rms(n)+(z=prod(n))^.5+z/q

Try it online!

Note that TIO does not have the signal package installed, so I defined rms() in the header. On Octave Online, you can try it if you pkg load nan. I'm not sure if there are any online interpreters that load it by default, but most systems would have this package loaded by default.

Thanks to Tom Carpenter for spotting a small mistake of 2 bytes.

This defines an anonymous function, taking the input as a vector n=[a,b]. We then use inline assignment to reduce the calculation of the HM to just z/q.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ You don't need to include the f= in the code, so that makes it 42 bytes. (which of course leads to the "crossed out 44 looks like 44") - Try it online! \$\endgroup\$ Commented Oct 15, 2017 at 13:08
  • \$\begingroup\$ Oh oops, that's an artifact from copying it from Octave-Online! Thanks. \$\endgroup\$
    – Sanchises
    Commented Oct 15, 2017 at 13:10
  • \$\begingroup\$ TIO loads installed packages by default, it just doesn't have the Signal package installed \$\endgroup\$
    – Luis Mendo
    Commented Oct 15, 2017 at 13:56
  • \$\begingroup\$ @LuisMendo Exactly, I think te de facto standard of MATLAB and Octave is to assume that all packages are installed and loaded. \$\endgroup\$
    – Sanchises
    Commented Oct 15, 2017 at 16:47
  • \$\begingroup\$ ^.5 saves a byte over sqrt. Also, remove f= from the code part in the link \$\endgroup\$
    – Luis Mendo
    Commented Oct 15, 2017 at 21:04
2
\$\begingroup\$

Jelly, 17 bytes

²Æm,P½S
PḤ÷S+Ç+Æm

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Nice combination of links. The best I could do in one line is PḤ÷S,µ³²Æm,P½,µÆmFS (19 bytes) - I though it's worth mentioning, maybe it's a source of inspiration. EDIT: >_> I now realise I can just use + instead of , \$\endgroup\$
    – Mr. Xcoder
    Commented Oct 15, 2017 at 12:31
  • \$\begingroup\$ @Mr.Xcoder I had an 18-byte version at first (not in revision history) but then thought to put the ones subject to ½ together, and it saved a byte. \$\endgroup\$ Commented Oct 15, 2017 at 12:33
  • \$\begingroup\$ Another possible source of inspiration: PḤ÷S can be replaced by: İSHİ \$\endgroup\$
    – Mr. Xcoder
    Commented Oct 15, 2017 at 13:03
  • \$\begingroup\$ @Mr.Xcoder thought of that too \$\endgroup\$ Commented Oct 15, 2017 at 13:04
2
\$\begingroup\$

05AB1E, 18 16 bytes

-2 bytes thanks to Erik the Outgolfer

nO;t¹O;¹Pt2¹zO/O

Explanation:

nO;t                Root mean square
n                    Raise [a, b] to [a ** 2, b ** 2]
 O                   Sum
  ;                  Half
   t                 Square root
    ¹O;             Arithmetic mean
    ¹                Retrieve stored [a, b]
     O               Sum
      ;              Half
       ¹Pt          Geometric mean
       ¹             Retrieve stored [a, b]
        P            Product
         t           Square root
          2¹zO/     Harmonic mean
           ¹         Retrieved stored [a, b]
            z        Vectorised inverse to [1 / a, 1 / b]
             O       Sum
          2   /      Get 2 divided by the sum
               O    Sum of all elements in stack

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ nO;t¹O;¹Pt2¹zO/O \$\endgroup\$ Commented Oct 15, 2017 at 13:10
  • \$\begingroup\$ @EriktheOutgolfer I don't think that works. \$\endgroup\$
    – Okx
    Commented Oct 15, 2017 at 13:11
  • \$\begingroup\$ Take input as a list [a, b]. \$\endgroup\$ Commented Oct 15, 2017 at 13:12
  • \$\begingroup\$ @EriktheOutgolfer Of course! Why didn't I think of that. \$\endgroup\$
    – Okx
    Commented Oct 15, 2017 at 13:15
2
\$\begingroup\$

Husk, 19 bytes

ṁëȯ√½ṁ□o½Σo√Π§/ΣoDΠ

Try it online!

-1 thanks to H.PWiz.

\$\endgroup\$
5
  • \$\begingroup\$ ö√½Σm□ can be ȯ√½ṁ□ \$\endgroup\$
    – H.PWiz
    Commented Oct 15, 2017 at 14:49
  • \$\begingroup\$ @H.PWiz >_> I knew I'd forget something \$\endgroup\$ Commented Oct 15, 2017 at 14:54
  • \$\begingroup\$ 18 bytes \$\endgroup\$
    – H.PWiz
    Commented Oct 16, 2017 at 5:52
  • \$\begingroup\$ @H.PWiz still learning! :p \$\endgroup\$ Commented Oct 16, 2017 at 11:23
  • \$\begingroup\$ you can change o½ΣY. \$\endgroup\$
    – Razetime
    Commented Jan 28, 2021 at 3:28
2
\$\begingroup\$

MATL, 21 18 17 bytes

UYmGphX^GYmGpy/vs

Try it online!

-3 bytes thanks to Luis Mendo.

Explanation

UYm               % Mean of squares, 
                  % Stack: { (a^2+b^2)/2 }
   Gp             % Product of input, a*b
                  % Stack: { (a^2+b^2)/2, a*b }
     hX^          % Concatenate into array, take square root of each element.
                  % Stack: { [RMS, HM] } 
        GYm       % Arithmetic mean of input.
                  % Stack: { [RMS,GM], AM }
           Gpy    % Product of input, duplicate AM from below.
                  % Stack: { [RMS,GM], AM, a*b, AM
              /   % Divide to get HM
                  % Stack { [RMS,GM], AM, HM}
               vs % Concatenate all to get [RMS,GM,AM,HM], sum.
\$\endgroup\$
0
2
\$\begingroup\$

Ohm v2, 16 bytes

²Σ½¬³Π¬³Σ½D³Πs/Σ

Try it online!

Explanation

square sum halve sqrt input product sqrt input sum halve dupe input product swap div sum

...if Ohm had a verbose mode of sorts. :P

²Σ½¬³Π¬³Σ½D³Πs/Σ

                  implicit input       [[7, 6]]
²Σ½¬              root mean square
²                  square              [[49, 36]]
 Σ                 sum                 [85]
  ½                halve               [42.5]
   ¬               square root         [6.519]
    ³Π¬           geometric mean
    ³              push first input    [6.519, [7, 6]]
     Π             product             [6.519, 42]
      ¬            square root         [6.519, 6.481]
       ³Σ½        arithmetic mean
       ³           push first input    [6.519, 6.481, [7, 6]]
        Σ          sum                 [6.519, 6.481, 13]
         ½         halve               [6.519, 6.481, 6.500]
          D³Πs/   harmonic mean
          D        duplicate           [6.519, 6.481, 6.500, 6.500]
           ³       push first input    [6.519, 6.481, 6.500, 6.500, [7, 6]]
            Π      product             [6.519, 6.481, 6.500, 6.500, 42]
             s     swap                [6.519, 6.481, 6.500, 42, 6.500]
              /    divide              [6.519, 6.481, 6.500, 6.461]
               Σ  sum                  [25.961]
                  implicit output      [25.961]
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm pretty sure I added a built-in for arithmetic mean a little while ago, but it wouldn't save you any bytes here. \$\endgroup\$ Commented Oct 16, 2017 at 4:10
2
\$\begingroup\$

TI-Basic (TI-84 Plus CE), 27 25 bytes

√(sum(Ans2)/2)+mean(Ans)+2prod(Ans)/sum(Ans)+√(prod(Ans

-2 bytes from Scrooble

Takes a list of two numbers in Ans, and implicitly returns the sum of the four means; e.g. run with {7,6}:prgmNAME to get 25.96148157.

Explanation:

√(sum(Ans2)/2): 8 bytes: root mean square

mean(Ans): 5 3 bytes: arithmetic mean (old: sum(Ans)/2)

2prod(Ans)/sum(Ans): 8 bytes: harmonic mean

√(prod(Ans: 3 bytes: geometric mean

+3 bytes for 3 +es

\$\endgroup\$
3
  • \$\begingroup\$ I think you have an extra unmatched closing parenthesis here after the 2 in sum(Ans)/2). \$\endgroup\$
    – kamoroso94
    Commented Oct 16, 2017 at 23:34
  • \$\begingroup\$ @kamoroso94 Fixed, thanks. \$\endgroup\$ Commented Oct 16, 2017 at 23:47
  • \$\begingroup\$ Save two bytes with the mean( builtin. \$\endgroup\$ Commented Dec 10, 2017 at 21:00
1
\$\begingroup\$

SOGL V0.12, 22 bytes

+:A½.².²+½√..*:√;«a/¹∑

Try it Here!

\$\endgroup\$
1
\$\begingroup\$

Dyalog APL, 44 bytes

{+/(2×o÷k),(.5×k←⍺+⍵),.5*⍨(o←⍺×⍵),.5×+/⍺⍵*2}

Try it online!

Dyadic dfns with a on the left and b on the right.

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 47 bytes

a=>b=>(c=a+b)/2+(c*c/2-(d=a*b))**.5+d**.5+2*d/c

quite trivial

f =

a=>b=>(c=a+b)/2+(c*c/2-(d=a*b))**.5+d**.5+2*d/c
<div oninput="r.value = f(+a.value)(+b.value)">
<p><label>a = <input id="a" type="number" step="any" value="1" /></label></p>
<p><label>b = <input id="b" type="number" step="any" value="1" /></label></p>
</div>
<p>result = <output id="r">4</output></label></p>

\$\endgroup\$
1
\$\begingroup\$

Java 8, 63 bytes

a->b->Math.sqrt((a*a+b*b)/2)+(a+b)/2+Math.sqrt(a*b)+2/(1/a+1/b)

Takes both parameters as Double and outputs as Double.
Try it here.

Or (also 63 bytes):

a->b->(a+b+Math.sqrt(a*a+b*b<<1))/2+Math.sqrt(a*b)+2d*a*b/(a+b)

Takes both parameters as Integer and outputs as Double.
Try it here.

\$\endgroup\$
0
1
\$\begingroup\$

Python 2, 58 bytes

lambda a,b:((a*a+b*b)/2)**.5+(a+b)/2+(a*b)**.5+2*a*b/(a+b)

Try it online!

Takes input as floats

\$\endgroup\$
1
\$\begingroup\$

ARBLE, 49 45 bytes

-4 bytes thanks to Mr. Xcoder

((a^2+b^2)/2)^.5+(a+b)/2+(a*b)^.5+2*a*b/(a+b)

Try it online!

\$\endgroup\$
1
1
\$\begingroup\$

Actually, 15 bytes

æßπ√+ßΣßπτ/+ßµ+

Try it online!

Yay Actually has a built-in for Root Square Mean!

æßπ√+ßΣßπτ/+ßµ+  ~ Full program.

æ                ~ Arithmetic mean.
 ßπ√             ~ Product, Square root (computes geometric mean).
    +            ~ Addition.
     ßΣ          ~ Push the sum of the input.
       ßπτ       ~ Push the product of the input doubled.
          /      ~ Divide.
           +     ~ Addition.
            ßµ   ~ Push Root Square Mean.
              +  ~ Addition.
\$\endgroup\$
1
\$\begingroup\$

Julia, 49 47 bytes

a$b=(x=a+b)/2+((a^2+b^2)/2)^.5+(y=a*b)^.5+2*y/x

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Groovy, 54 bytes

{a,b->c=a+b;((a*a+b*b)/2)**0.5+c/2+(a*b)**0.5+2*a*b/c}

-2 thanks to Mr. Xcoder for an edit that made me feel dumb.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you can replace a**2 with a*a and b**2 with b*b \$\endgroup\$
    – Mr. Xcoder
    Commented Oct 17, 2017 at 18:37
1
\$\begingroup\$

C# (.NET Core), 76 bytes

+13 bytes for using System;

a=>b=>Math.Sqrt((a*a+b*b)/2)+(a+b)/2+Math.Sqrt(a*b)+2/(1/a+1/b)

Try it online!

\$\endgroup\$
1
0
\$\begingroup\$

Jq 1.5, 76 bytes

[pow((map(pow(.;2))|add)/2;.5),add/2,pow(.[0]*.[1];.5),2/(map(1/.)|add)]|add

Expanded

[
  pow((map(pow(.;2))|add)/2;.5)  # root mean square
, add/2                          # arithmetic mean
, pow(.[0]*.[1];.5)              # geometric mean
, 2/(map(1/.)|add)               # harmonic mean
]
| add                            # that just about sums it up for mean

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.