diff options
| -rw-r--r-- | challenge-142/duncan-c-white/README | 73 | ||||
| -rwxr-xr-x | challenge-142/duncan-c-white/perl/ch-1.pl | 61 |
2 files changed, 83 insertions, 51 deletions
diff --git a/challenge-142/duncan-c-white/README b/challenge-142/duncan-c-white/README index 40b57c7ac8..5a433917be 100644 --- a/challenge-142/duncan-c-white/README +++ b/challenge-142/duncan-c-white/README @@ -1,70 +1,41 @@ -TASK #1 - Number Divisors +TASK #1 - Divisor Last Digit -Write a script to find lowest 10 positive integers having exactly 8 divisors. +You are given positive integers, $m and $n. -Example - - 24 is the first such number having exactly 8 divisors. - 1, 2, 3, 4, 6, 8, 12 and 24. - -MY NOTES: Very easy. - - -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 (because the digits are in the wrong order). Also for 1234, you can -only have integers having no more than three digits. +Write a script to find total count of divisors of $m having last digit $n. Example 1: - Input: $m = 1234, $n = 2 - Output: 9 + Input: $m = 24, $n = 2 + Output: 2 - Possible integers created using the digits of 1234 are: - 1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134 and 234. + The divisors of 24 are 1, 2, 3, 4, 6, 8 and 12. + There are only 2 divisors having last digit 2 are 2 and 12. - There are 9 integers divisible by 2 such as: - 2, 4, 12, 14, 24, 34, 124, 134 and 234. Example 2: - Input: $m = 768, $n = 4 - Output: 3 + Input: $m = 30, $n = 5 + Output: 2 - Possible integers created using the digits of 768 are: - 7, 6, 8, 76, 78 and 68. + The divisors of 30 are 1, 2, 3, 5, 6, 10 and 15. + There are only 2 divisors having last digit 5 are 5 and 15. - There are 3 integers divisible by 4 such as: - 8, 76 and 68. +MY NOTES: Very easy. -MY NOTES: Sounds pretty easy. However, there's one mistake: -"You are only allowed to use (n-1) digits at the most" -is flatly contradicted by the examples. I assume it meant -"length(m)-1" which is what the examples show. +TASK #2 - Sleep Sort -So each digit can either be present or absent - it's another "subsets" -task. One wrinkle: the length(m)-1 rule means that (all-present) is -not a valid combination. Plus of course (all-absent) isn't either. +Another joke sort similar to JortSort suggested by champion Adam Russell. -Last time we did a "subsets" task was in Challenge 136 Task 2, -Fibonacci Seqence. In that, I wrote: +You are given a list of numbers. -"How do we sum subsets of values? Two obvious ways: +Write a script to implement Sleep Sort. For more information: -1. recursive function, iterating over the values: each value is either in the - subset or not, try both possibilities. -2. binary-counting method, iterate over every combination C from 1 .. - 2**(number values)-1, then sum up only the elements where the corresponding - bit in C is on. -" +for n in array + create a new thread and make it sleep for array[n] milliseconds + print array[n] +end +wait for all processes to finish -That time, I wrote a (2) binary-counting method. So this time round, let's -try a recursive function instead, just for variety. +MY NOTES: threaded sorting? weird. not sure I can be bothered.. diff --git a/challenge-142/duncan-c-white/perl/ch-1.pl b/challenge-142/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..d8abf44adb --- /dev/null +++ b/challenge-142/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl +# +# TASK #1 - Divisor Last Digit +# +# You are given positive integers, $m and $n. +# +# Write a script to find total count of divisors of $m having last digit $n. +# +# Example 1: +# +# Input: $m = 24, $n = 2 +# Output: 2 +# +# The divisors of 24 are 1, 2, 3, 4, 6, 8 and 12. +# There are only 2 divisors having last digit 2 are 2 and 12. +# +# Example 2: +# +# Input: $m = 30, $n = 5 +# Output: 2 +# +# The divisors of 30 are 1, 2, 3, 5, 6, 10 and 15. +# There are only 2 divisors having last digit 5 are 5 and 15. +# +# MY NOTES: Very easy. +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +#use Data::Dumper; + +# +# my @div = divisors( $n ); +# Find and return all the divisors of $n, including 1. +# +sub divisors +{ + my( $n ) = @_; + my @d = (1); + my $halfn = int($n/2); + for( my $i=2; $i<=$halfn; $i++ ) + { + push @d, $i if $n%$i==0; + } + push @d, $n; + return @d; +} + + +my $debug=0; +die "Usage: divisor-last-digit [--debug] M N\n" unless + GetOptions( "debug"=>\$debug ) && @ARGV==2; +my( $m, $n ) = @ARGV; + +my @div = grep { $_ % 10 == $n } divisors( $m ); +my $d = @div; + +say "$m has $d divisors with last digit $n"; +say " they are ", join(', ',@div) if $debug; |
