aboutsummaryrefslogtreecommitdiff
path: root/challenge-142
diff options
context:
space:
mode:
authorMatthew Neleigh <matthew.neleigh@gmail.com>2021-12-09 14:50:00 -0500
committerMatthew Neleigh <matthew.neleigh@gmail.com>2021-12-09 14:50:00 -0500
commitcc43fb0a254f63c30b0c2ad5f80a88b80ec8c8f2 (patch)
treea8b1bd2b3b8866394c9fe001751255dd74553aad /challenge-142
parentb5139d12b493bf966de0d155c5ed2aabb3665b29 (diff)
downloadperlweeklychallenge-club-cc43fb0a254f63c30b0c2ad5f80a88b80ec8c8f2.tar.gz
perlweeklychallenge-club-cc43fb0a254f63c30b0c2ad5f80a88b80ec8c8f2.tar.bz2
perlweeklychallenge-club-cc43fb0a254f63c30b0c2ad5f80a88b80ec8c8f2.zip
new file: challenge-142/mattneleigh/perl/ch-1.pl
new file: challenge-142/mattneleigh/perl/ch-2.pl
Diffstat (limited to 'challenge-142')
-rwxr-xr-xchallenge-142/mattneleigh/perl/ch-1.pl76
-rwxr-xr-xchallenge-142/mattneleigh/perl/ch-2.pl105
2 files changed, 181 insertions, 0 deletions
diff --git a/challenge-142/mattneleigh/perl/ch-1.pl b/challenge-142/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..e1fc1ac998
--- /dev/null
+++ b/challenge-142/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,76 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @pairs = (
+ [ 24, 2 ],
+ [ 30, 5 ],
+);
+my $pair;
+
+foreach $pair (@pairs){
+ printf("Input: \$m = %d, \$n = %d\n", $pair->[0], $pair->[1]);
+ printf("Output: %d\n\n", count_divisors_ending_in_n(@{$pair}));
+}
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Count the divisors of a positive integer that end in a particular digit
+# Takes two arguments:
+# * The integer, M, to examine; if this number is not an integer, the
+# fractional portion will be truncated
+# * The digit N to look for; if this number is more than 9, only the FIRST
+# digit will be searched for
+# Returns on success:
+# * The number of divisors of M that end in N
+# Returns on error:
+# * undef if either argument is not valid
+# NOTE: Some definitions of the term "divisor" do not include the number M
+# itself, while others do; the latter definition is adhered to in this
+# function, thus if M ends in N, it will be counted
+# https://en.wikipedia.org/wiki/Divisor#Examples
+################################################################################
+sub count_divisors_ending_in_n{
+ my $m = abs(int(shift()));
+ my $n = substr(abs(shift()), 0, 1);
+
+ return(undef) unless(
+ $m =~ m/^\d+$/ && $n =~ m/^\d+$/
+ );
+
+ my $i;
+ my $factors = 0;
+
+ # Loop until we get to the square root
+ # of $m
+ for($i = 1; $i * $i <= $m; $i++){
+ my $quotient = $m / $i;
+
+ # See if $i appears to divide evenly
+ # into $m
+ if(($quotient - int($quotient)) < 0.000001){
+ # Increment the counter for each
+ # factor that ends in $n
+ $factors++ if($i =~ m/$n$/);
+ $factors++ if(int($quotient) =~ m/$n$/);
+ }
+ }
+
+ return($factors);
+
+}
+
+
+
diff --git a/challenge-142/mattneleigh/perl/ch-2.pl b/challenge-142/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..bde49a7012
--- /dev/null
+++ b/challenge-142/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,105 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+use Config;
+$Config{useithreads}
+ or die(
+ "Threads required; recompile Perl with threads to run this program."
+ );
+use threads;
+use threads::shared;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @numbers = (
+ # No set of test values was specified,
+ # so I created this one
+ 3, 10, 45, 1, 75, 2, 100, 32, 74, 9, 4, 22, 53, 7, 14, 89, 5
+);
+
+print("Input: ", join(", ", @numbers), "\n");
+print("Output: ", join(", ", sleep_sort(@numbers)), "\n");
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+
+################################################################################
+# Perform the Sleep Sort on a list of numerical values
+# Takes one argument:
+# * The list of numerical values, preferably integers
+# Returns:
+# * The numerical values provided as input, sorted in ascending order.
+# Probably, anyway. It's best to run this when the CPU isn't particularly
+# busy if accurate sorting results are desired.
+################################################################################
+sub sleep_sort{
+
+ my @threads;
+ my @sorted :shared;
+
+ # Start a thread for each value in the
+ # number list
+ foreach(@ARG){
+ push(
+ @threads,
+ threads->create(\&_sleep_sort_thread_core, $_, \@sorted)
+ );
+ }
+
+ # Join with all spawned threads; we
+ # don't care in what order they finish,
+ # we just need to know when they're all
+ # done
+ foreach(@threads){
+ $_->join();
+ }
+
+ return(@sorted);
+
+}
+
+
+
+################################################################################
+# Perform one thread's worth of work in the Sleep Sort
+# Takes two arguments:
+# * A numerical value (preferably an integer) to insert into the sorted list at
+# the proper time
+# * A ref to the sorted list, which must have the ':shared' attribute applied
+# (see the documentation for 'threads' and 'threads::shared')
+# Returns:
+# * undef in all cases, which conveys no meaningful information
+# NOTE: This function should ONLY be called by sleep_sort()
+################################################################################
+sub _sleep_sort_thread_core{
+ my $value = shift();
+ my $sorted = shift();
+
+ # Block for a unit of time proportional
+ # to the value we're given; due to the
+ # vagaries of thread scheduling,
+ # reliable sort order could not be
+ # achieved with a granularity of less
+ # than 1/10th of a second- and even
+ # that's iffy if the CPU is busy
+ select(undef, undef, undef, 0.1 * $value);
+
+ # Store the value in the shared list
+ push(@{$sorted}, $value);
+
+ return(undef);
+
+}
+
+
+