Weekly Challenge 157
I’m back after the
three week hiatus!
TASK #1 › Pythagorean Means
You are given a set of integers.
Write a script to compute all three Pythagorean Means i.e
Geometric Mean and
Harmonic Mean of the given set of integers.
This is a relatively straight forward task.
The Wikipedia page provides the necessary formulas and we can use the
lambda functions to calculate the required figures. Finally we use the
round function to print the results to one decimal place.
Perl code is similar to the
Python version, except we use the
product() method in
math.product is only available for
One thing to note is if the sum of integers is
division by zero error might occur when calculating the harmonic mean. I’m not catching this and let the error pass to the user.
$ ./ch-1.py 1 3 5 6 9 AM = 4.8, GM = 3.8, HM = 2.8 $ ./ch-1.py 2 4 6 8 10 AM = 6.0, GM = 5.2, HM = 4.4 $ ./ch-1.py 1 2 3 4 5 AM = 3.0, GM = 2.6, HM = 2.2
TASK #2 › Brazilian Number
You are given a number
$n > 3.
Write a script to find out if the given number is a
Brazilian Number. A positive integer number
N has at least one natural number
1 < B < N-1 where the representation of
base B has [the] same digits.
This was an interesting task. We can’t convert numbers into a different base simply by using letters and numbers. For example
base 640, but how would you express
639 in the same base?
For this task I considered a value having the same digits even if the number itself has different digits. For example,
925 is express as
The guts of this task is to convert the number n in
base 10 to a
specified base, which I have called
b. The function is called
same_digits. I take the modulus (remainder) of
b and make this value
d. I then do an integer division and repeat this process comparing that
n % b is
d. I return
False if any digit isn’t the same, or
True if they all are.
Then it’s just a matter of having a loop from
n - 2, and calling the
same_digits function for each base. If there is a match, I print
1 (and the base), or
Perl code is a transliteration of the
$ ./ch-2.py 6 0 $ ./ch-2.py 7 1 (base 2) $ ./ch-2.py 8 1 (base 3)
If you have any suggestion then please do share with us email@example.com.