# Pythagoras Was a Meanie

Wherein the music of the spheres homes in on a central, perfect note. Or three. I don’t know.

#### THE WEEKLY CHALLENGE – PERL & RAKU #157 Task 1

“Hey, hey, hey, hey-now. Don’t be mean; we don’t have to be mean, cuz, remember, no matter where you go, there you are.”

— Buckaroo Banzai

### Pythagorean Means

You are given a set of integers.

Write a script to compute all three `Pythagorean Means` i.e Arithmetic MeanGeometric Mean and Harmonic Mean of the given set of integers. Please refer to wikipedia page for more informations.

##### Example 1:
``````Input: @n = (1,3,5,6,9)
Output: AM = 4.8, GM = 3.8, HM = 2.8
``````
##### Example 2:
``````Input: @n = (2,4,6,8,10)
Output: AM = 6.0, GM = 5.2, HM = 4.4
``````
##### Example 3:
``````Input: @n = (1,2,3,4,5)
Output: AM = 3.0, GM = 2.6, HM = 2.2
``````

#### ANALYSIS

So before we begin, I have a few observations on the topic of meanness:

• “Pythagorean” means in the manner of Pythagoras.
• Which, because there are no less than three variations on the idea of mean we are discussing today, means apparently that he was mean — a real bastard at that, to require three different ways to properly discuss his meanness.
• I mean, that’s not even controversial: its a sort of middle-of-the-road opinion on the subject. Which sort exactly is a continuing subject for discussion.
• Whatever, exactly, that means.

Ok, I acknowledge that little poetic flight of fancy was probably neither accurate in any absolute sense not particularly enlightening. Let’s try another approach.

Pythagoras was a geometrician, but he was also among his many talents fascinated by music. As such he spent quite a lot of time focused on vibrating strings, and the relationships of those vibrations to the notes sounded, and the relationships those notes had to each other to build musical scales.

A fundamental notion here is that of an interval: the difference in pitch between two notes. As it turns out, a scale is not best thought of as a series of notes places along a line, subdivided at absolute positions, but rather as an assemblage of intervals between notes that in turn correspond to the vibrational harmonics of a string. For example what is known as a perfect fifth, the difference between the first and the fifth note in a scale, or a C and a G, in the C major scale, is a shortening of the string in a 3:2 ratio. It’s all hideously complicated and I’m not going to even start to explain that any further or we’d be here all night. Suffice to say on a fretted instrument if you manually shorten the string by pinching off the vibrating part the note played will be higher. Exactly where to shorten it to becomes a complex issue, and although we’ve generally decided to look at tonality in a standard way today, that remains largely convention and the matter is hardly settled, because ultimately there is no single correct answer to the idea of being “in tune”.

If we want to place a note mid-way between two other notes, what are we really talking about? Midway on a number line? Midway between the ratios above and below?

This is why Pythagoras ended up with so many ideas of what it means to be mean.

Consider a number line between 1 and 9. Half-way along this line we will have the position 5, and 1 to 5 is 4 units, and 5 to 9 is also 4 units. This is the arithmetic mean, and what we commonly think of as being the average.

However what if we think instead of the intervals between the values. 4 is four times 1, but 9 is two point two-five times 4. If we want the intervals to be the same then we arrive at the mean of 3, as 3 is three times 1, and 9 is three times 3.

This is the geometric mean.

The harmonic mean is arrived at by considering the intervals as ratios taken from the values to the mean: the span of 7.2 from 1.8 to 9 viewed as a fraction of 9 is 0.8. The span of 0.8 between 1 and 1.8 is the same fraction, so 1.8 is the harmonic mean.

This is presented by Archytas of Tarentum as:

the mean when they are such that, by whatever part of itself the first
term exceeds the second, by that part of the third the middle term exceeds
the third

The harmonic mean was coined by Archytas as such because of its apparent musicality in arriving at intervals. The connections, as stated previously, are thick and quite complex, but completely fascinating. For interested parties, here’s one introduction to the dark arts:

#### METHOD

##### PERL 5 SOLUTION

The solutions are straightforward: the arithmetic mean is the sum of the values divided by the number of elements. The geometric mean is the n-th root of the product of the values, with n being the number of elements again. The harmonic mean is a little trickier, being the number of elements divided by the sum of the inverses of those elements.

``````use List::Util qw( sum product );

sub am ( @list ) {
return sum( @list ) / @list
}

sub gm ( @list ) {
return product( @list ) ** (1 / @list)
}

sub hm ( @list ) {
return @list / ( sum map { 1/\$_ } @list )
}``````

##### Raku Solution

In Raku we’ll provide a minimal solution and just output a list of the three values as requested: arithmetic, geometric and harmonic.

``````unit sub MAIN ( *@list ) ;

.say for
@list.sum / @list.elems,                            ## arithmetic
@list.reduce({\$^a * \$^b})  ** (1/ @list.elems),     ## geometric
@list.elems / ( sum map { 1/\$_ }, @list );          ## harmonic
``````

The Perl Weekly Challenge, that idyllic glade wherein we stumble upon the holes for these sweet descents, is now known as

The Weekly Challenge – Perl and Raku

It is the creation of the lovely Mohammad Sajid Anwar and a veritable swarm of contributors from all over the world, who gather, as might be expected, weekly online to solve puzzles. Everyone is encouraged to visit, learn and contribute at

https://theweeklychallenge.org