# Sum the means of the two integers

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 ($\inline&space;\sqrt{\frac{a^2+b^2}{2}}$).

• The arithmetic mean is their sum, halved ($\inline&space;\frac{a+b}{2}$).

• The geometric mean is the square root of their product ($\inline&space;\sqrt{a\cdot&space;b}$).

• The harmonic mean is 2 divided by the sum of their inverses ($\inline&space;\frac{2}{\frac{1}{a}+\frac{1}{b}}$ = $\inline&space;\frac{2ab}{a+b}$).

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.

• Related. – Martin Ender Oct 15 '17 at 12:18
• Must've asked to output the mean of means. -1 (not). – the default. Oct 15 '17 at 12:21
• At least there is no Mathematica builtin for that. Right? – NieDzejkob Oct 15 '17 at 12:21
• @NieDzejkob I don't think so :-) – Mr. Xcoder Oct 15 '17 at 12:21
• @NieDzejkob Although I suspect there are builtins for each one of the means. – Erik the Outgolfer Oct 15 '17 at 12:25

# 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.

# 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!

¯\_(ツ)_/¯

• An alternative: ((#^2+#2^2)/2)^.5+(#+#2)/2+(#1#2)^.5+2#*#2/(#+#2)& – Mr. Xcoder Oct 15 '17 at 12:50
• 2 bytes off: ((#^2+#2^2)/2)^.5+(+##)/2+(1##)^.5+2/(1/#+1/#2)& – Martin Ender Oct 15 '17 at 12:59
• 42 bytes: (((s=+##)^2-2##)/2)^.5+s/2+(1##)^.5+2##/s& – J42161217 Oct 15 '17 at 18:03
• 37 bytes: (2(s=+##/2)^2-t)^.5+s+(t=1##)^.5+t/s& – J42161217 Oct 15 '17 at 18:42
• 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). – JungHwan Min Oct 15 '17 at 19:26

# 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!

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

# 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!

# 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.

# Octave, 4442 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.

• 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! – Tom Carpenter Oct 15 '17 at 13:08
• Oh oops, that's an artifact from copying it from Octave-Online! Thanks. – Sanchises Oct 15 '17 at 13:10
• TIO loads installed packages by default, it just doesn't have the Signal package installed – Luis Mendo Oct 15 '17 at 13:56
• @LuisMendo Exactly, I think te de facto standard of MATLAB and Octave is to assume that all packages are installed and loaded. – Sanchises Oct 15 '17 at 16:47
• ^.5 saves a byte over sqrt. Also, remove f= from the code part in the link – Luis Mendo Oct 15 '17 at 21:04

# Jelly, 17 bytes

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


Try it online!

• 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 , – Mr. Xcoder Oct 15 '17 at 12:31
• @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. – Erik the Outgolfer Oct 15 '17 at 12:33
• Another possible source of inspiration: PḤ÷S can be replaced by: İSHİ – Mr. Xcoder Oct 15 '17 at 13:03
• @Mr.Xcoder thought of that too – Erik the Outgolfer Oct 15 '17 at 13:04

# 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!

• nO;t¹O;¹Pt2¹zO/O – Erik the Outgolfer Oct 15 '17 at 13:10
• @EriktheOutgolfer I don't think that works. – Okx Oct 15 '17 at 13:11
• Take input as a list [a, b]. – Erik the Outgolfer Oct 15 '17 at 13:12
• @EriktheOutgolfer Of course! Why didn't I think of that. – Okx Oct 15 '17 at 13:15

# Husk, 19 bytes

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


Try it online!

-1 thanks to H.PWiz.

• ö√½Σm□ can be ȯ√½ṁ□ – H.PWiz Oct 15 '17 at 14:49
• @H.PWiz >_> I knew I'd forget something – Erik the Outgolfer Oct 15 '17 at 14:54
• 18 bytes – H.PWiz Oct 16 '17 at 5:52
• @H.PWiz still learning! :p – Erik the Outgolfer Oct 16 '17 at 11:23

# MATL, 2118 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.


# 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]

• 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. – Nick Clifford Oct 16 '17 at 4:10

# 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

• I think you have an extra unmatched closing parenthesis here after the 2 in sum(Ans)/2). – kamoroso94 Oct 16 '17 at 23:34
• @kamoroso94 Fixed, thanks. – pizzapants184 Oct 16 '17 at 23:47
• Save two bytes with the mean( builtin. – Khuldraeseth na'Barya Dec 10 '17 at 21:00

# SOGL V0.12, 22 bytes

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


Try it Here!

# 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.

# 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>

# 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.

# 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

# 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!

# 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).
ßΣ          ~ Push the sum of the input.
ßπτ       ~ Push the product of the input doubled.
/      ~ Divide.
ßµ   ~ Push Root Square Mean.


# 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!

## 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.

• I think you can replace a**2 with a*a and b**2 with b*b – Mr. Xcoder Oct 17 '17 at 18:37

# 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!

# 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!