diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-21 22:05:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-21 22:05:27 +0100 |
| commit | b8131ad692a193e3d336bc2f1f50368e51d0a15f (patch) | |
| tree | f2b6e30298efb4dfff5c7833cf254cd4dcebe410 | |
| parent | 4b908564f5bfed80f327bcbd8ee9808ca147c355 (diff) | |
| parent | f9851bfb09572b751cec03210fca8d797b47d846 (diff) | |
| download | perlweeklychallenge-club-b8131ad692a193e3d336bc2f1f50368e51d0a15f.tar.gz perlweeklychallenge-club-b8131ad692a193e3d336bc2f1f50368e51d0a15f.tar.bz2 perlweeklychallenge-club-b8131ad692a193e3d336bc2f1f50368e51d0a15f.zip | |
Merge pull request #6312 from mattneleigh/pwc170
new file: challenge-170/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-170/mattneleigh/perl/ch-1.pl | 126 | ||||
| -rwxr-xr-x | challenge-170/mattneleigh/perl/ch-2.pl | 152 |
2 files changed, 278 insertions, 0 deletions
diff --git a/challenge-170/mattneleigh/perl/ch-1.pl b/challenge-170/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..87d12a5a0c --- /dev/null +++ b/challenge-170/mattneleigh/perl/ch-1.pl @@ -0,0 +1,126 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +use POSIX; + +################################################################################ +# Begin main execution +################################################################################ + +my $n = 10; +my @primorials = calculate_primorial_numbers($n); +my $output_format = + "P(%d) = %" + . + ceil(log($primorials[$#primorials][0]) / log(10)) + . + "d (%s)\n"; + +print("\n"); +for my $i (0 .. $#primorials){ + printf( + $output_format, + $i, + $primorials[$i][0], + join("*", map($primorials[$_][1], (0 .. $i))) + ); +} +print("\n"); + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Calculate a specified quantity of the first primorial numbers +# Takes one argument: +# * The number N of primorial numbers to calculate +# Returns on success: +# * A list of primorial numbers and the largest prime factor used to calculate +# each, e.g.: +# +# @output = ( +# [ 1, 1 ], +# [ 2, 2 ], +# [ 5, 3 ], +# [ 30, 5 ], +# ... +# ); +# +# Returns on error: +# * undef if N is not at least one (1) +################################################################################ +sub calculate_primorial_numbers{ + my $n = int(shift()); + + return(undef) + unless($n > 0); + + my @primorials = ( [ 1, 1 ] ); + my $next_prime = 2; + + # Loop until we've found enough primorial + # numbers + while(scalar(@primorials) < $n){ + # Calculate and store the next primorial + # and its factor list + push( + @primorials, + [ + $primorials[$#primorials][0] * $next_prime, + $next_prime + ] + ); + + # Calculate the prime for the next + # iteration + until(is_prime(++$next_prime)){ + ;; + } + } + + return(@primorials); + +} + + + +################################################################################ +# Determine whether a given integer N is prime +# Takes one argument: +# * The integer N +# Returns on success: +# * 1 if N is prime +# * 0 if N is not prime +# NOTE: If N is less than zero, it will always be considered nonprime +################################################################################ +sub is_prime{ + my $n = int(shift()); + + my $i; + + # Take care of a few easy cases + return(1) + if(($n == 2) || ($n == 3)); + return(0) + if(($n <= 1) || !($n % 2) || !($n % 3)); + + # See if certain factors divide evenly + for($i = 5; $i * $i <= $n; $i += 6){ + if(!($n % $i) || !($n % ($i + 2))){ + return(0); + } + } + + return(1); + +} + + + diff --git a/challenge-170/mattneleigh/perl/ch-2.pl b/challenge-170/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..ac26f973d0 --- /dev/null +++ b/challenge-170/mattneleigh/perl/ch-2.pl @@ -0,0 +1,152 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @matrix_pairs = ( + # Given case + [ + [ + [ 1, 2 ], + [ 3, 4 ] + ], + [ + [ 5, 6 ], + [ 7, 8 ] + ] + ], + + # Additional test case(s) + [ + [ + [ 1, 2, 3, 4, 5, 6, 7, 8 ] + ], + [ + [ 9 ], + [ 10 ], + [ 11 ], + [ 12 ], + [ 13 ], + [ 14 ], + [ 15 ], + [ 16 ] + ] + ] +); + +print("\n"); +foreach my $pair (@matrix_pairs){ + my @k_prod = @{kronecker_product($pair->[0], $pair->[1])}; + + print_matrix(\@k_prod, " "); + print("\n"); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Calculate the Kronecker product of two matrices A and B +# Takes two arguments: +# * Matrix A, which must consist of a ref to an array of arrays whose contents +# form a rectangular grid of scalar numerical values, e.g.: +# +# $A = [ +# [ 1, 2, 3 ], +# [ 4, 5, 6 ] +# ]; +# +# * Matrix B, subject to the same requirements as matrix A +# Returns: +# * A ref to a matrix that contains the Kronecker product of A and B; this +# will conform to the format described above for A +# NOTE: There are no constraints on the relative dimensions of A and B so long +# as both are rectangular +################################################################################ +sub kronecker_product{ + my $a = shift(); + my $b = shift(); + + my @c = (); + + for my $aj (0 .. $#$a){ + for my $ai (0 .. $#{$a->[0]}){ + for my $bj (0 .. $#$b){ + for my $bi (0 .. $#{$b->[0]}){ + $c[$aj*scalar(@{$b})+$bj][$ai*scalar(@{$b->[0]})+$bi] + = $a->[$aj][$ai] * $b->[$bj][$bi]; + } + } + } + } + + return(\@c); + +} + + + +################################################################################ +# Print the contents of a matrix to STDOUT +# Takes two arguments: +# * A matrix, which must consist of a ref to an array of arrays whose contents +# are to be printed to STDOUT, e.g.: +# +# $M = [ +# [ 1, 2, 3, 4 ], +# [ 5, 6, 7, 8 ] +# ]; +# +# * An optional string to prepend to each line of the output written to STDOUT, +# which can be used to establish an indent if desired +# Returns no meaningful value +################################################################################ +sub print_matrix{ + my $matrix = shift(); + my $indent = shift(); + + my $max = 0; + my $element_format; + + $indent = "" unless(defined($indent)); + + # Determine the size, in characters, of the + # largest item in the matrix- done this way + # instead of counting digits with log() so + # non-numerical values can be accommodated + foreach(@{$matrix}){ + foreach(@{$_}){ + $max = length($_) + if(length($_) > $max); + } + } + + # Produce the output format for each + # element + $element_format = "%".$max."s"; + + # Loop and print + foreach(@{$matrix}){ + printf( + "%s[ %s ]\n", + $indent, + join( + " ", + map(sprintf($element_format, $_), @{$_}) + ) + ); + } + +} + + + |
