Sum the means of the two integers

12

1

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.

Mr. Xcoder

Posted 2017-10-15T12:13:10.343

Reputation: 39 774

Related. – Martin Ender – 2017-10-15T12:18:19.557

10Must've asked to output the mean of means. -1 (not). – my pronoun is monicareinstate – 2017-10-15T12:21:01.417

9At least there is no Mathematica builtin for that. Right? – NieDzejkob – 2017-10-15T12:21:05.263

@NieDzejkob I don't think so :-) – Mr. Xcoder – 2017-10-15T12:21:24.317

@NieDzejkob Although I suspect there are builtins for each one of the means. – Erik the Outgolfer – 2017-10-15T12:25:00.900

@EriktheOutgolfer It's not the same – NieDzejkob – 2017-10-15T12:39:46.687

Can we take inputs as 7.0, 6.0? – totallyhuman – 2017-10-15T13:58:53.887

@icrieverytim yes, you can. – Mr. Xcoder – 2017-10-15T13:59:28.430

Answers

13

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.

xnor

Posted 2017-10-15T12:13:10.343

Reputation: 115 687

9

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!

¯\_(ツ)_/¯

totallyhuman

Posted 2017-10-15T12:13:10.343

Reputation: 15 378

I am pretty sure the chain of built-ins is way more verbose than applying the formulas (for some). – Mr. Xcoder – 2017-10-15T12:35:32.087

@Mr.Xcoder Oh maybe but I thought it'd be more fun to ridicule Mathematica's built-ins! Anyways, here you go, a much less readable golfy version. :3 – totallyhuman – 2017-10-15T12:41:25.193

1An alternative: ((#^2+#2^2)/2)^.5+(#+#2)/2+(#1#2)^.5+2#*#2/(#+#2)& – Mr. Xcoder – 2017-10-15T12:50:44.913

12 bytes off: ((#^2+#2^2)/2)^.5+(+##)/2+(1##)^.5+2/(1/#+1/#2)& – Martin Ender – 2017-10-15T12:59:07.443

@MartinEnder Does... Does Mathematica turn prefix when you want it to? Oh wait, it changes the head of the list or something..? Thanks! (Also, do you happen to know if I can remove the N@ from the second solution?) – totallyhuman – 2017-10-15T13:02:05.750

1

I've explained how those ## tricks work in this tip. And no you can't remove the N@ unless the OP allows exact symbolic solutions, or lets you take the input as a floating-point type.

– Martin Ender – 2017-10-15T13:47:40.253

242 bytes: (((s=+##)^2-2##)/2)^.5+s/2+(1##)^.5+2##/s& – J42161217 – 2017-10-15T18:03:26.643

637 bytes: (2(s=+##/2)^2-t)^.5+s+(t=1##)^.5+t/s& – J42161217 – 2017-10-15T18:42:45.283

2A 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 – 2017-10-15T19:26:58.993

5

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!

orlp

Posted 2017-10-15T12:13:10.343

Reputation: 37 067

The <<1 is going to incorrectly truncate to an integer when a and b are opposite parities. – xnor – 2017-10-15T14:35:17.293

@xnor No it's not :) You're thinking of >>1. – orlp – 2017-10-15T14:35:40.550

1Oh, my mistake! I see now there's a /2 outside that this is compensating for. Nice trick. – xnor – 2017-10-15T14:39:20.970

4

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!

Giuseppe

Posted 2017-10-15T12:13:10.343

Reputation: 21 077

3

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.

Sanchises

Posted 2017-10-15T12:13:10.343

Reputation: 8 530

1

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 – 2017-10-15T13:08:44.647

Oh oops, that's an artifact from copying it from Octave-Online! Thanks. – Sanchises – 2017-10-15T13:10:13.037

TIO loads installed packages by default, it just doesn't have the Signal package installed – Luis Mendo – 2017-10-15T13:56:45.550

@LuisMendo Exactly, I think te de facto standard of MATLAB and Octave is to assume that all packages are installed and loaded. – Sanchises – 2017-10-15T16:47:09.777

^.5 saves a byte over sqrt. Also, remove f= from the code part in the link – Luis Mendo – 2017-10-15T21:04:12.490

3

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.

H.PWiz

Posted 2017-10-15T12:13:10.343

Reputation: 10 962

2

Jelly, 17 bytes

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

Try it online!

Erik the Outgolfer

Posted 2017-10-15T12:13:10.343

Reputation: 38 134

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 – 2017-10-15T12:31:59.480

@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 – 2017-10-15T12:33:14.017

Another possible source of inspiration: PḤ÷S can be replaced by: İSHİ – Mr. Xcoder – 2017-10-15T13:03:49.340

@Mr.Xcoder thought of that too – Erik the Outgolfer – 2017-10-15T13:04:02.460

2

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.

Sanchises

Posted 2017-10-15T12:13:10.343

Reputation: 8 530

2

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!

Okx

Posted 2017-10-15T12:13:10.343

Reputation: 15 025

nO;t¹O;¹Pt2¹zO/O – Erik the Outgolfer – 2017-10-15T13:10:31.137

@EriktheOutgolfer I don't think that works. – Okx – 2017-10-15T13:11:33.447

Take input as a list [a, b]. – Erik the Outgolfer – 2017-10-15T13:12:21.487

@EriktheOutgolfer Of course! Why didn't I think of that. – Okx – 2017-10-15T13:15:14.260

2

Husk, 19 bytes

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

Try it online!

-1 thanks to H.PWiz.

Erik the Outgolfer

Posted 2017-10-15T12:13:10.343

Reputation: 38 134

ö√½Σm□ can be ȯ√½ṁ□ – H.PWiz – 2017-10-15T14:49:02.213

@H.PWiz >_> I knew I'd forget something – Erik the Outgolfer – 2017-10-15T14:54:17.793

18 bytes – H.PWiz – 2017-10-16T05:52:45.300

@H.PWiz still learning! :p – Erik the Outgolfer – 2017-10-16T11:23:39.343

2

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]

totallyhuman

Posted 2017-10-15T12:13:10.343

Reputation: 15 378

1I'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 – 2017-10-16T04:10:03.257

2

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

pizzapants184

Posted 2017-10-15T12:13:10.343

Reputation: 3 174

I think you have an extra unmatched closing parenthesis here after the 2 in sum(Ans)/2). – kamoroso94 – 2017-10-16T23:34:08.910

@kamoroso94 Fixed, thanks. – pizzapants184 – 2017-10-16T23:47:57.503

Save two bytes with the mean( builtin. – Khuldraeseth na'Barya – 2017-12-10T21:00:05.283

1

SOGL V0.12, 22 bytes

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

Try it Here!

dzaima

Posted 2017-10-15T12:13:10.343

Reputation: 19 048

1

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.

Uriel

Posted 2017-10-15T12:13:10.343

Reputation: 11 708

1

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!

my pronoun is monicareinstate

Posted 2017-10-15T12:13:10.343

Reputation: 3 111

You can save a byte by using System; and removing both System.. PS: If you want a less boring way (with the exact same byte-count of 76): using System;a=>b=>(a+b+Math.Sqrt(a*a+b*b<<1))/2+Math.Sqrt(a*b)+2d*a*b/(a+b). ;)

– Kevin Cruijssen – 2017-10-19T11:46:48.027

1

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!

Uriel

Posted 2017-10-15T12:13:10.343

Reputation: 11 708

1

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>

tsh

Posted 2017-10-15T12:13:10.343

Reputation: 13 072

1

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!

ATaco

Posted 2017-10-15T12:13:10.343

Reputation: 7 898

45 bytes – Mr. Xcoder – 2017-10-15T20:38:36.683

1

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.

Kevin Cruijssen

Posted 2017-10-15T12:13:10.343

Reputation: 67 575

1

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

TFeld

Posted 2017-10-15T12:13:10.343

Reputation: 19 246

1

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.

Mr. Xcoder

Posted 2017-10-15T12:13:10.343

Reputation: 39 774

1

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.

Magic Octopus Urn

Posted 2017-10-15T12:13:10.343

Reputation: 19 422

1I think you can replace a**2 with a*a and b**2 with b*b – Mr. Xcoder – 2017-10-17T18:37:13.797

0

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!

jq170727

Posted 2017-10-15T12:13:10.343

Reputation: 411