diff options
| author | Stephen Lynn <bizlsg@localhost.localdomain> | 2022-06-21 18:17:03 +0800 |
|---|---|---|
| committer | Stephen Lynn <bizlsg@localhost.localdomain> | 2022-06-21 18:17:03 +0800 |
| commit | 7771804aa414c7016a6072c5964bcf5e7bbb8cf8 (patch) | |
| tree | 22a7a7abc5d8d999aa3dace09b2bf5ce2e341eb5 | |
| parent | bb6f23fd223e77e35e199fbca2cc224ad21f0f1c (diff) | |
| download | perlweeklychallenge-club-7771804aa414c7016a6072c5964bcf5e7bbb8cf8.tar.gz perlweeklychallenge-club-7771804aa414c7016a6072c5964bcf5e7bbb8cf8.tar.bz2 perlweeklychallenge-club-7771804aa414c7016a6072c5964bcf5e7bbb8cf8.zip | |
perl and raku solutions
l Please enter the commit message for your changes. Lines starting
| -rwxr-xr-x | challenge-170/steve-g-lynn/perl/ch-1.pl | 37 | ||||
| -rwxr-xr-x | challenge-170/steve-g-lynn/perl/ch-2.pl | 107 | ||||
| -rwxr-xr-x | challenge-170/steve-g-lynn/raku/ch-1.p6 | 29 | ||||
| -rwxr-xr-x | challenge-170/steve-g-lynn/raku/ch-2.p6 | 14 |
4 files changed, 187 insertions, 0 deletions
diff --git a/challenge-170/steve-g-lynn/perl/ch-1.pl b/challenge-170/steve-g-lynn/perl/ch-1.pl new file mode 100755 index 0000000000..f5ab6733eb --- /dev/null +++ b/challenge-170/steve-g-lynn/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +use Math::Prime::Util::GMP qw(next_prime); + +for my $i (0 .. 10){ + print &primorial($i)," "; +} + +print "\n"; + + +sub primorial { + #-- old-fashioned perl using dynamic scope and typeglobs + local ($n) = @_; + + #-- nested private sub: recursively find the n'th prime + local *nth_prime = sub { + local ($n)=@_; + + if ($n==0){ + return 1; + } else { + return next_prime( + &nth_prime ( + $n-1 + ) ); + } + }; + + #-- back to primorial sub: recursively compute primorial + if ($n==0){ + return 1; + } else { + return &nth_prime($n)*&primorial($n-1); + } +} + diff --git a/challenge-170/steve-g-lynn/perl/ch-2.pl b/challenge-170/steve-g-lynn/perl/ch-2.pl new file mode 100755 index 0000000000..d089884d24 --- /dev/null +++ b/challenge-170/steve-g-lynn/perl/ch-2.pl @@ -0,0 +1,107 @@ +#!/usr/bin/perl + +# Old-fashioned perl4-style approach modelling a matrix as a hash: +# item at m'th row and n'th column of matrix M represented as $M{'m,n'} +# (sadly the bareword form $M{m,n} is now deprecated.) +# +# This is a convenient representation for computing kronecker products +# when the inputs are sparse matrices. + +# logic: compute the kronecker product C=kron(A,B) in an intermediate form +# first as a hash C{m,n} where the indices m,n are the same as those in A, +# and C{m,n} is a stringified list of the values in the matrix A{m,n} * B. + +# Then flatten C to return it as an old-fashioned hash-style matrix +# like A and B. +# + +use List::Pairwise qw(mapp); + +local %A=('1,1',1,'1,2',2,'2,1',3,'2,2',4); +local %B=('1,1',5,'1,2',6,'2,1',7,'2,2',8); + +local %C= &kron(*A,*B); + +foreach my $i (1 .. 4) { + foreach my $j (1 .. 4) { + print $C{$i.','.$j}," "; + } + print "\n"; +} + +sub kron { + #-- old-fashioned approach using dynamic scope and typeglobs + + local (*A, *B)=@_; #--%A, %B matrices to be kroneckered + local (*C, *C_wip); + #-- %C return value, %C_wip intermediate non-flat object + + #-- nested subs for supporting calculations + #-- get matrix dimensions + local *find_mat_dims = sub { + local (*matrix)=@_; + my ($nrow, $ncol)=(0,0); + + for my $i (keys %matrix){ + my ($row, $col)=split(/,/,$i); + ($row > $nrow) && ($nrow = $row); + ($col > $ncol) && ($ncol = $col); + } + return ($nrow, $ncol); + }; + + local *scalar_times_mat = sub { + #-- nested sub to multiply a scalar into + #-- every element of a matrix + + local ($scalar, *matrix)=@_; + + my %matrix_copy = %matrix; + mapp { $b = $scalar * $b} %matrix_copy; + #-- $a is key, $b is value in List::Pairwise syntax + + return %matrix_copy; + }; + + #-- nested sub to create C_wip + local *assemble_C_wip = sub { + for my $i (keys %A){ + $C_wip{$i} = join(':',&scalar_times_mat( $A{$i}, *B )); + #-- e.g., 1,2 -> '1,2:12:2,1:14:1,1:10:2,2:16' + } + return %C_wip; + }; + + #-- nested sub to flatten C_wip + # e.g., 1,2 -> '1,2:12:2,1:14:1,1:10:2,2:16' + # flattens to ('1,4' -> 12, '2,3'->14, '1,3'->10, '2,4'->16) + # transform 'rowA,colA' -> '..rowB,colB..' + # to (rowB+(rowA-1)*nrow_B),(colB+(colA-1)*ncol_B) + # which is the 'row, col' in the flattened Kronecker product + + local *flatten_C_wip = sub { + local @C=(); + for my $i (keys %C_wip){ + my ($row_A, $col_A)=split(/,/,$i); + my ($entry)=$C_wip{$i}; + $entry =~ s/(\d+),(\d+)/ + ($1+($row_A-1)*$nrow_B) .','. + ($2+($col_A-1)*$nrow_B) + /gex; + push @C, split(/:/,$entry); + } + return @C; + }; + + #-- end nested sub definitions, back to main &kron + + local ($nrow_B, $ncol_B) = &find_mat_dims(*B); + + &assemble_C_wip(); + + %C=&flatten_C_wip(); + + return %C; + +} + diff --git a/challenge-170/steve-g-lynn/raku/ch-1.p6 b/challenge-170/steve-g-lynn/raku/ch-1.p6 new file mode 100755 index 0000000000..cde6b7282b --- /dev/null +++ b/challenge-170/steve-g-lynn/raku/ch-1.p6 @@ -0,0 +1,29 @@ +#!/usr/bin/raku + +use Inline::Perl5; +use Math::Prime::Util::GMP:from<Perl5> <next_prime>; + +for (0 .. 10) -> $n { + print primorial($n)," "; +} + +say ""; + +sub primorial (UInt $n){ + if ($n==0) { + return 1; + } else { + return nth-prime($n) * primorial($n-1); + } +} + +#recursively find the n'th prime +sub nth-prime (UInt $n) { + if ($n==0) { + return 1; + } else { + return next_prime( + nth-prime ($n-1) ); + } +} + diff --git a/challenge-170/steve-g-lynn/raku/ch-2.p6 b/challenge-170/steve-g-lynn/raku/ch-2.p6 new file mode 100755 index 0000000000..83ac5ba027 --- /dev/null +++ b/challenge-170/steve-g-lynn/raku/ch-2.p6 @@ -0,0 +1,14 @@ +#!/usr/bin/raku + +use Math::Matrix :ALL; + +#--Math::Matrix has a built-in operator for kronecker product +#⊗ or X* +# + +my $A = MM [ [1,2], [3,4] ]; +my $B = MM [ [5,6], [7,8] ]; + +say $A ⊗ $B; + + |
