aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Lynn <bizlsg@localhost.localdomain>2022-07-08 13:18:52 +0800
committerStephen Lynn <bizlsg@localhost.localdomain>2022-07-08 13:18:52 +0800
commita2d6bb276687ff2c710f7502d45ed0beefe14431 (patch)
tree5eb1d193213747b86f6cac3c078dc07558b97666
parentdbf958883c898c776eac636d6d8e0c17df01df05 (diff)
downloadperlweeklychallenge-club-a2d6bb276687ff2c710f7502d45ed0beefe14431.tar.gz
perlweeklychallenge-club-a2d6bb276687ff2c710f7502d45ed0beefe14431.tar.bz2
perlweeklychallenge-club-a2d6bb276687ff2c710f7502d45ed0beefe14431.zip
pwc 172 solution
-rwxr-xr-xchallenge-172/steve-g-lynn/julia/ch-1.jl50
-rwxr-xr-xchallenge-172/steve-g-lynn/julia/ch-2.jl24
-rwxr-xr-xchallenge-172/steve-g-lynn/perl/ch-1.pl49
-rwxr-xr-xchallenge-172/steve-g-lynn/perl/ch-2.pl48
-rwxr-xr-xchallenge-172/steve-g-lynn/raku/ch-1.p656
-rwxr-xr-xchallenge-172/steve-g-lynn/raku/ch-2.p618
6 files changed, 245 insertions, 0 deletions
diff --git a/challenge-172/steve-g-lynn/julia/ch-1.jl b/challenge-172/steve-g-lynn/julia/ch-1.jl
new file mode 100755
index 0000000000..0c4aba99b8
--- /dev/null
+++ b/challenge-172/steve-g-lynn/julia/ch-1.jl
@@ -0,0 +1,50 @@
+#!/usr/bin/julia
+
+using Primes
+
+function prime_partition(m::Int, n::Int)::Matrix{Int}
+ retval=Array{Int}(undef,0,n)
+ if (n==2)
+ for i in primes(m ÷ 2)
+ if (isprime(m-i))
+ retval=[retval;i m-i]
+ end
+ end
+ retval=sort_partition(retval)
+ retval=delete_equal_rows(retval)
+ return retval
+ end
+ if (n > 2)
+ for i in primes(m ÷ n)
+ p=prime_partition(m-i, n-1)
+ if (size(p)[1] > 0)
+ sizep=size(p)
+ retval=[retval; (i .* ones(Int,sizep[1])) p]
+ end
+ end
+ retval=sort_partition(retval)
+ retval=delete_equal_rows(retval)
+ return retval
+ end
+end
+
+function sort_partition(p::Matrix{Int})::Matrix{Int}
+ return sortslices(sort(p,dims=2),dims=1)
+end
+
+function delete_equal_rows(p::Matrix{Int})::Matrix{Int}
+ #-- matrix must be sorted first by sort_partition
+ list_equal=Vector{Int64}();
+ for i in 1:size(p,1)-1
+ if p[i+1,:]==p[i,:]
+ append!(list_equal,i+1)
+ end
+ end
+ return p[setdiff(1:size(p,1),list_equal),:]
+end
+
+println(prime_partition(18,2))
+println(prime_partition(19,3))
+println(prime_partition(20,4))
+
+
diff --git a/challenge-172/steve-g-lynn/julia/ch-2.jl b/challenge-172/steve-g-lynn/julia/ch-2.jl
new file mode 100755
index 0000000000..d91242f018
--- /dev/null
+++ b/challenge-172/steve-g-lynn/julia/ch-2.jl
@@ -0,0 +1,24 @@
+#!/usr/bin/julia
+
+#real 0m1.088s
+#user 0m1.117s
+#sys 0m0.445s
+
+using Statistics
+
+function fivenum(x::Vector{Float64})
+ retval=zeros(5)
+ ctr=1
+ for i in 0:0.25:1
+ retval[ctr]=quantile(x,i)
+ ctr += 1
+ end
+ return retval
+end
+
+test=rand(925089)
+println(fivenum(test))
+
+#[7.335486141846204e-7, 0.24913072314183482, 0.49950102601650026,
+# 0.749602495312535, 0.9999999579699111]
+
diff --git a/challenge-172/steve-g-lynn/perl/ch-1.pl b/challenge-172/steve-g-lynn/perl/ch-1.pl
new file mode 100755
index 0000000000..3d81c9f72c
--- /dev/null
+++ b/challenge-172/steve-g-lynn/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+use Math::Prime::Util::GMP qw(primes is_prime);
+use PDL;
+use PDL::NiceSlice;
+use PDL::AutoLoader;
+
+#-- global parameters
+local $MAXPRIMES = 100_000;
+local $primes = pdl primes($MAXPRIMES);
+
+print &prime_partition(18,2),"\n";
+print &prime_partition(19,3),"\n";
+print &prime_partition(20,4),"\n";
+
+sub prime_partition {
+ my ($m, $n)=@_;
+
+ if ($m > $MAXPRIMES) {
+ $primes = $primes -> append(primes($MAXPRIMES, $m));
+ $MAXPRIMES = $m;
+ }
+
+ my $retval=zeros($n,0);
+
+ #-- $n == 2
+ if ($n==2) {
+ for my $prime ($primes -> list) {
+ if (is_prime($m - $prime)) {
+ $retval = $retval->glue(1,
+ pdl([$m - $prime, $prime]));
+ }
+ last if ($prime > int($m / 2));
+ }
+ }
+
+ #-- $n > 2: recursion
+ if ($n > 2) {
+ for my $prime ($primes -> list) {
+ my $p1 = &prime_partition($m - $prime, $n - 1);
+ if ( ($p1 -> dim(1)) > 0){
+ $p1=append($prime, $p1);
+ $retval = $retval->glue(1,$p1);
+ }
+ last if ($prime > int($m / $n));
+ }
+ }
+ return qsortvec(qsort($retval))->uniqvec;
+}
diff --git a/challenge-172/steve-g-lynn/perl/ch-2.pl b/challenge-172/steve-g-lynn/perl/ch-2.pl
new file mode 100755
index 0000000000..0585fe0209
--- /dev/null
+++ b/challenge-172/steve-g-lynn/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+#real 0m0.465s
+#user 0m0.418s
+#sys 0m0.041s
+
+use PDL;
+use PDL::NiceSlice;
+use PDL::AutoLoader;
+
+my $test=random(925089);
+print &fivenum($test),"\n";
+#[
+# [4.0794618e-06]
+# [ 0.25007063]
+# [ 0.50035368]
+# [ 0.75020104]
+# [ 0.9999987]
+#]
+
+
+sub fivenum {
+ my ($data) = pdl @_;
+
+ my $len = $data -> dim(0);
+
+ my ($min, $quartile_1, $median, $quartile_3, $max);
+
+ my $indx = qsorti($data);
+
+ #-- Quartiles
+ #-- If len divisible by 4, I take the midpoint of
+ #-- the two observations that embrace the quartile.
+ #-- (Recommended better practice is linear interpolation,
+ #-- but the calculation here is also theoretically okay)
+
+ if ( ($len % 4) == 0 ) {
+ $quartile_1 =
+ 0.5*($data($indx($len/4))+$data($indx($len/4+1)));
+ $quartile_3 =
+ 0.5*($data($indx(3*$len/4))+$data($indx(3*$len/4+1)));
+ } else {
+ $quartile_1 = $data($indx(int($len/4)+1));
+ $quartile_3 = $data($indx(int(3*$len/4)+1));
+ }
+
+ return pdl([$data->min, $quartile_1, $data->median, $quartile_3, $data->max]);
+}
diff --git a/challenge-172/steve-g-lynn/raku/ch-1.p6 b/challenge-172/steve-g-lynn/raku/ch-1.p6
new file mode 100755
index 0000000000..8e1ce34034
--- /dev/null
+++ b/challenge-172/steve-g-lynn/raku/ch-1.p6
@@ -0,0 +1,56 @@
+#!/usr/bin/raku
+
+use Math::Primesieve;
+
+our $MAXPRIMES = 100_000;
+our $p = Math::Primesieve.new;
+our @primes = $p.primes($MAXPRIMES);
+
+say &prime-partition(18,2);
+say &prime-partition(19,3);
+say &prime-partition(20,4);
+
+multi sub prime-partition (Int $m where $m > 0, Int $n where $n==2) {
+ ($n > ($m div 2)) && (return ());
+
+ my @pp=();
+
+ if ($m > $MAXPRIMES) {
+ push @primes, $p.primes($MAXPRIMES,$m);
+ $MAXPRIMES = $m;
+ }
+
+ for (@primes) -> $prime {
+ if ( ($m - $prime).is-prime ) {
+ push @pp, BagHash.new($prime,$m-$prime);
+ }
+ last if ($prime > ($m div 2));
+ }
+ return @pp.unique(as => BagHash, with => &[eqv] );
+}
+
+
+multi sub prime-partition (Int $m where $m > 0, Int $n where $n > 2) {
+
+ ($n > ($m div 2)) && (return ());
+
+ my @pp=();
+
+ if ($m > $MAXPRIMES) {
+ push @primes, $p.primes($MAXPRIMES,$m);
+ $MAXPRIMES = $m;
+ }
+
+ for (@primes) -> $prime {
+ my @p1 = &prime-partition($m - $prime, $n - 1);
+
+ if (@p1.elems > 0) {
+ for (@p1) -> $baghash {
+ $baghash.add($prime);
+ }
+ }
+ @pp.append(@p1);
+ last if ($prime > ($m div 3));
+ }
+ return @pp.unique(as => BagHash, with => &[eqv] );
+}
diff --git a/challenge-172/steve-g-lynn/raku/ch-2.p6 b/challenge-172/steve-g-lynn/raku/ch-2.p6
new file mode 100755
index 0000000000..c4475194f4
--- /dev/null
+++ b/challenge-172/steve-g-lynn/raku/ch-2.p6
@@ -0,0 +1,18 @@
+#!/usr/bin/raku
+
+#real 0m38.303s
+#user 0m37.857s
+#sys 0m0.229s
+
+use Statistics;
+
+my @test=();
+for (1..925_089) {
+ push @test, rand;
+}
+
+say fivenum(@test); #-- built-in
+
+#(1.9337635626115457e-06 0.2505242942758381 0.5005684431380041
+# 0.7503169554857986 0.9999993436204229)
+