## DEV Community is a community of 789,160 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Simon Green

Posted on

# Weekly Challenge 141

## TASK #1 › Number Divisors

Write a script to find lowest 10 positive integers having exactly 8 divisors.

## My solutions

The main part of this challenge is calculating the number of divisors a given number has. This is one of those cases where you can over-optimize the code for no real benefit. My function `divisors` assumes that a number has two divisors (1 and itself) and then goes from 2 to half the number to see if there is any other divisors. A special case is made for one, which only has one divisor.

Once that part is done, the main function has an list called `solutions`, and set `number` to 1. It then increments `number` and adds it to the `solutions` list if it has eight divisors, continuing until we have 10 numbers.

The Perl solution is a transliteration of the Python code.

### Examples

``````\$ ./ch-1.py
24, 30, 40, 42, 54, 56, 66, 70, 78, 88

\$ ./ch-1.pl
24, 30, 40, 42, 54, 56, 66, 70, 78, 88
``````

## TASK #2 › Like Numbers

You are given positive integers, `\$m` and `\$n`.

Write a script to find total count of integers created using the digits of `\$m` which is also divisible by `\$n`.

Repeating of digits are not allowed. Order/Sequence of digits can’t be altered. You are only allowed to use (n-1) digits at the most. For example, 432 is not acceptable integer created using the digits of 1234. Also for 1234, you can only have integers having no more than three digits.

### My solutions

In Python I use itertools' combinations function to generate a list of all combinations. I then use a for loop to count the number of combinations that are divisible by `n`.

Perl doesn't have a built in combination function, so I took a slight different tack with this task. I basically used a binary switch to generate a list of combinations using `map { substr( \$m, \$_, 1 ) } grep { \$i & 2**\$_ } ( 0 .. \$l - 1 )` where `\$l` is the length of the first number (`\$m`) and `\$i` is between 1 and 2\$l - 2.

### Examples

``````\$ ./ch-2.py 1234 2
9

\$ ./ch-2.py 768 4
3

\$ ./ch-2.pl 1234 2
9

\$ ./ch-2.pl 768 4
3
``````