diff options
| -rw-r--r-- | challenge-141/adam-russell/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-141/adam-russell/perl/ch-2.pl | 24 |
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-141/adam-russell/perl/ch-1.pl b/challenge-141/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..bc9d36902f --- /dev/null +++ b/challenge-141/adam-russell/perl/ch-1.pl @@ -0,0 +1,31 @@ +use strict; +use warnings; +## +# Write a script to find lowest 10 positive integers +# having exactly 8 divisors. +## + +sub factor{ + my($n) = @_; + my @factors = (1, $n); + foreach my $j (2..sqrt($n)){ + push @factors, $j if $n % $j == 0; + push @factors, ($n / $j) if $n % $j == 0 && $j ** 2 != $n; + } + return @factors; +} + +sub first_ten_with_eight{ + my $i = 0; + my @first_ten; + do{ + my @factors = factor($i); + push @first_ten, $i if @factors == 8; + $i++; + }while(@first_ten != 10); + return @first_ten; +} + +MAIN:{ + print join(", ", first_ten_with_eight()) . "\n"; +} diff --git a/challenge-141/adam-russell/perl/ch-2.pl b/challenge-141/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..c5e3a80dd2 --- /dev/null +++ b/challenge-141/adam-russell/perl/ch-2.pl @@ -0,0 +1,24 @@ +use strict; +use warnings; +## +# 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. +## +use Data::PowerSet q/powerset/; + +sub like_numbers{ + my($m, $n) = @_; + my @divisible; + for my $subset (@{powerset(split(//, $m))}){ + my $i = join("", @$subset); + push @divisible, $i if $i && $i != $m && $i % $n == 0; + } + return @divisible; +} + +MAIN:{ + print like_numbers(1234, 2) . "\n"; + print like_numbers(768, 4) . "\n"; +} |
