DEV Community

Simon Green
Simon Green

Posted on

It's all about the numbers

Weekly Challenge 169

Challenge, My solution

After a long break, I'm back. Without further ado, let's go through the two challenges this week.

Task 1: Brilliant Numbers

Task

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.

My solution

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:

  1. Get all primes of l length.
  2. Multiple each of the primes
  3. 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.

Example

$ ./ch-1.py 
4, 6, 9, 10, 14, 15, 21, 25, 35, 49, 121, 143, 169, 187, 209, 221, 247, 253, 289, 299
Enter fullscreen mode Exit fullscreen mode

Task 2: Achilles Numbers

Task

Write a script to generate first 20 Achilles Numbers.

My solution

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

  1. Get the unique prime factors into the factors dict (hash in Perl), where the key is the prime number and the value is the power.
  2. Set the powers set (array in Perl) to the power values. At this point we no longer care about the prime factor.
  3. 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).
  4. If the greatest common divisor of the powers is not 1, return False (undef).
  5. Return True.

Example

$ ./ch-2.py 
72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125, 1152, 1323, 1352, 1372, 1568, 1800
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
ehsan_ahmed07 profile image
Ehsan Ahmed

Hello Simon, i need to get some guide from you