After a long break, I'm back. Without further ado, let's go through the two challenges this week.
Write a script to generate first 20 Brilliant Numbers.
Brilliant numbers are numbers with two prime factors of the same length.
The number should have exactly two prime factors, i.e. it’s the product of two primes of the same length.
This is one of those tasks where given we are dealing with such small numbers, it is easier to just brute force things. In the main function, I have the value
l for the length of the primes. I then call call the
get_bril_nums function to get all brilliant numbers.
It does this in three steps:
- Get all primes of
- Multiple each of the primes
- Sort the results numerically
I keep doing this until there are at least 20 numbers calculated (as we know this is when
l == 2). I then print the first 20 numbers.
$ ./ch-1.py 4, 6, 9, 10, 14, 15, 21, 25, 35, 49, 121, 143, 169, 187, 209, 221, 247, 253, 289, 299
Write a script to generate first 20 Achilles Numbers.
This task is more challenging than the first one, and highlighted that at the time of writing, Mohammad's description isn't quite correct. I've tweeted him, so hopefully it will be fixed.
For this task, I keep calling the
is_achilles_number with an incrementing counter until we have found twenty numbers. This function does the following
- Get the unique prime factors into the
factorsdict (hash in Perl), where the key is the prime number and the value is the power.
- Set the
powersset (array in Perl) to the power values. At this point we no longer care about the prime factor.
- If there is only one value (it's a power of a single number) or any of the powers is 1, return
False(undef in Perl).
- If the greatest common divisor of the powers is not 1, return
$ ./ch-2.py 72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125, 1152, 1323, 1352, 1372, 1568, 1800