From 9a709571310682177b5a30b6ac4fd2d517348a11 Mon Sep 17 00:00:00 2001 From: Joel Crosswhite Date: Tue, 1 Dec 2020 17:45:32 -0700 Subject: Added initial files for challenge 089. Added README for first commit. Created initial script for challenge 1 using binary GCD algorithm. --- challenge-089/jcrosswh/README | 1 + challenge-089/jcrosswh/perl/ch-1.pl | 63 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 challenge-089/jcrosswh/README create mode 100644 challenge-089/jcrosswh/perl/ch-1.pl diff --git a/challenge-089/jcrosswh/README b/challenge-089/jcrosswh/README new file mode 100644 index 0000000000..144afd1a18 --- /dev/null +++ b/challenge-089/jcrosswh/README @@ -0,0 +1 @@ +Solution by Joel Crosswhite. \ No newline at end of file diff --git a/challenge-089/jcrosswh/perl/ch-1.pl b/challenge-089/jcrosswh/perl/ch-1.pl new file mode 100644 index 0000000000..ff0f0979a0 --- /dev/null +++ b/challenge-089/jcrosswh/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +=head1 NAME + +PWC 089 Challenge 1 + +=head1 SYNOPSIS + + $ ch-1.pl 3 + 3 + + $ ch-1.pl 4 + 7 + +=head1 DESCRIPTION + +Given a positive integer, this script will sum the +L of all possible +unique pairs between 1 and the inputed positive integer. + +=head1 AUTHORS + +Joel Crosswhite Ejoel.crosswhite@ix.netcom.comE + +=cut + +my $input = $ARGV[0]; +if (!defined($input) || $input !~ m/^[1-9]\d*$/) { + print "Usage: ch-1.pl \n"; + exit 1; +} + +my $retval = 0; +for (my $i = 1; $i < $input; $i++) { + for (my $k = $i + 1; $k <= $input; $k++) { + $retval += gcd($i, $k); + } +} + +print $retval . "\n"; +exit 0; + +sub gcd { + my ($first, $second, $d) = @_; + $d = 0 if !defined($d); + + if ($first == $second) { + return $first * 2 ** $d; + } elsif ($first % 2 == 0 && $second % 2 == 0) { + return gcd($first / 2, $second / 2, $d + 1); + } elsif ($first % 2 == 0) { + return gcd($first / 2, $second, $d); + } elsif ($second % 2 ==0) { + return gcd($first, $second / 2, $d); + } else { + my $c = abs($first - $second); + my $new_second = $first < $second ? $first : $second; + return gcd($c / 2, $new_second, $d); + } +}# gcd -- cgit From af36ef9593ecd93f7dcd5f4f9dd8323ab77d52c0 Mon Sep 17 00:00:00 2001 From: Joel Crosswhite Date: Sat, 5 Dec 2020 09:31:42 -0700 Subject: Added solution 2 for finding the magic matrix. Updated documentation for solution 1. --- challenge-089/jcrosswh/perl/ch-1.pl | 6 ++++ challenge-089/jcrosswh/perl/ch-2.pl | 72 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 challenge-089/jcrosswh/perl/ch-2.pl diff --git a/challenge-089/jcrosswh/perl/ch-1.pl b/challenge-089/jcrosswh/perl/ch-1.pl index ff0f0979a0..c23100948e 100644 --- a/challenge-089/jcrosswh/perl/ch-1.pl +++ b/challenge-089/jcrosswh/perl/ch-1.pl @@ -21,6 +21,12 @@ Given a positive integer, this script will sum the L of all possible unique pairs between 1 and the inputed positive integer. +=head1 SOLUTION + +This solution uses the +L to +determine the different GCD values. + =head1 AUTHORS Joel Crosswhite Ejoel.crosswhite@ix.netcom.comE diff --git a/challenge-089/jcrosswh/perl/ch-2.pl b/challenge-089/jcrosswh/perl/ch-2.pl new file mode 100644 index 0000000000..349f43d523 --- /dev/null +++ b/challenge-089/jcrosswh/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +=head1 NAME + +PWC 089 Challenge 2 + +=head1 SYNOPSIS + + $ ch-2.pl + [ a b c ] + [ d e f ] + [ g h i ] + +=head1 DESCRIPTION + +A script to display matrix as below with numbers 1 - 9. + + [ a b c ] + [ d e f ] + [ g h i ] + +The matrix will satisfy the following rules: + + a + b + c = 15 + d + e + f = 15 + g + h + i = 15 + a + d + g = 15 + b + e + h = 15 + c + f + i = 15 + a + e + i = 15 + c + e + g = 15 + +The numbers will only be used once. + +=head1 SOLUTION + +This solution uses the +L to populate and +print the array. + +=head1 AUTHORS + +Joel Crosswhite Ejoel.crosswhite@ix.netcom.comE + +=cut + +my @matrix; +my $LENGTH = 3; +my @location = (0, int($LENGTH / 2)); +my $value = 1; +my $max_value = $LENGTH * $LENGTH + 1; + +while ($value < $max_value) { + + if (!defined($matrix[$location[0]][$location[1]])) { + $matrix[$location[0]][$location[1]] = $value++; + $location[0] = ($location[0] - 1) % $LENGTH; + $location[1] = ($location[1] + 1) % $LENGTH; + } else { + $location[0] = ((($location[0] + 1) % $LENGTH) + 1) % $LENGTH; + $location[1] = ($location[1] - 1) % $LENGTH; + } +} + +foreach my $line (@matrix) { + printf("[ %d %d %d ]\n", $line->[0], $line->[1], $line->[2]); +} + +exit 0; -- cgit