aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Lynn <bizlsg@localhost.localdomain>2022-07-20 20:08:51 +0800
committerStephen Lynn <bizlsg@localhost.localdomain>2022-07-20 20:08:51 +0800
commit6181fdc30a85c0920f9e6d8bd5c84b0cf96be5ba (patch)
tree9bcaf3eefc228e24bb4e6c1571246654ab37d6e6
parentbd35cd539d3229f9442ce41a4001abe3ad1c1a4b (diff)
downloadperlweeklychallenge-club-6181fdc30a85c0920f9e6d8bd5c84b0cf96be5ba.tar.gz
perlweeklychallenge-club-6181fdc30a85c0920f9e6d8bd5c84b0cf96be5ba.tar.bz2
perlweeklychallenge-club-6181fdc30a85c0920f9e6d8bd5c84b0cf96be5ba.zip
pwc-174
-rwxr-xr-xchallenge-174/steve-g-lynn/julia/ch-1.jl19
-rwxr-xr-xchallenge-174/steve-g-lynn/julia/ch-2.jl24
-rwxr-xr-xchallenge-174/steve-g-lynn/perl/alt-ch-1.pl29
-rwxr-xr-xchallenge-174/steve-g-lynn/perl/ch-1.pl32
-rwxr-xr-xchallenge-174/steve-g-lynn/perl/ch-2.pl31
-rwxr-xr-xchallenge-174/steve-g-lynn/raku/ch-1.p623
-rwxr-xr-xchallenge-174/steve-g-lynn/raku/ch-2.p614
7 files changed, 172 insertions, 0 deletions
diff --git a/challenge-174/steve-g-lynn/julia/ch-1.jl b/challenge-174/steve-g-lynn/julia/ch-1.jl
new file mode 100755
index 0000000000..096223a757
--- /dev/null
+++ b/challenge-174/steve-g-lynn/julia/ch-1.jl
@@ -0,0 +1,19 @@
+#!/usr/bin/env julia
+
+#real 0m1.126s
+#user 0m1.196s
+#sys 0m0.417s
+
+#-- for integer abc.. return a^1+b^2+c^3...
+function get_poly(n::Int) ::Int
+ narry = digits(n); #-- array of digits in reverse order
+ indx=(length(narry)):-1:1
+ return sum(narry .^ indx)
+end
+
+for i in 0:3000000
+ if (get_poly(i)==i)
+ println(i)
+ end
+end
+
diff --git a/challenge-174/steve-g-lynn/julia/ch-2.jl b/challenge-174/steve-g-lynn/julia/ch-2.jl
new file mode 100755
index 0000000000..2757d03089
--- /dev/null
+++ b/challenge-174/steve-g-lynn/julia/ch-2.jl
@@ -0,0 +1,24 @@
+#!/usr/bin/env julia
+
+using Combinatorics;
+
+function rank2permutation(a::Vector, n::Int)
+ return nthperm(sort(a),n+1)
+end
+
+
+function permutation2rank(a::Vector)
+ perms=permutations(sort(a))
+ ctr=0
+ for i in perms
+ if (a==i)
+ break
+ end
+ ctr = ctr+1
+ end
+ return ctr
+end
+
+println(rank2permutation([0,1,2],1)) # [0,2,1]
+println(permutation2rank([1,0,2])) #2
+
diff --git a/challenge-174/steve-g-lynn/perl/alt-ch-1.pl b/challenge-174/steve-g-lynn/perl/alt-ch-1.pl
new file mode 100755
index 0000000000..af7b3bd86e
--- /dev/null
+++ b/challenge-174/steve-g-lynn/perl/alt-ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+
+#real 0m7.760s
+#user 0m7.744s
+#sys 0m0.004s
+
+
+
+for my $i (0 .. 3_000_000) {
+ ($i == &get_poly($i)) && print "$i\n";
+}
+
+#print &get_poly(518);
+
+sub get_poly {
+#-- for base-10 integer abc.. return a^1+b^2+c^3...
+ my ($n)=@_;
+ my (@nstr)=split(//,$n);
+ my @indx = 1..@nstr;
+ my $retval=0;
+ for my $i (0..(@nstr-1)) {
+ $retval += ($nstr[$i] ** $indx[$i]);
+ }
+ return $retval;
+}
+
+
+
+
diff --git a/challenge-174/steve-g-lynn/perl/ch-1.pl b/challenge-174/steve-g-lynn/perl/ch-1.pl
new file mode 100755
index 0000000000..20dcf208d7
--- /dev/null
+++ b/challenge-174/steve-g-lynn/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+#real 3m31.183s
+#user 3m29.832s
+#sys 0m0.179s
+
+#-- this PDL solution is slow. The alternative
+#-- plain Perl solution in alt-ch-1,pl runs in ~8 seconds.
+
+use PDL;
+use PDL::NiceSlice;
+use PDL::AutoLoader;
+
+#--make PDL aware of the user-defined fn signature so it broadcasts correctly
+
+broadcast_define('get_poly(a();[o]b())', over { $_[1] .= _get_poly($_[0]); });
+
+#-- now find the sequence by checking ints up to 3_000_000
+
+my $pdl=sequence(long, 3_000_000);
+&get_poly($pdl, (my $out=null) );
+print $pdl->where($pdl==$out),"\n";
+
+#[0 1 2 3 4 5 6 7 8 9 89 135 175 518 598 1306 1676 2427 2646798]
+
+#-- subroutine
+#-- given base-10 integer abc.. return a^1+b^2+c^3...
+sub _get_poly {
+ my $a=pdl split(//,$_[0]);
+ return sum($a ** ( ($a->xvals) + 1) );
+}
+
diff --git a/challenge-174/steve-g-lynn/perl/ch-2.pl b/challenge-174/steve-g-lynn/perl/ch-2.pl
new file mode 100755
index 0000000000..6386bc2f9f
--- /dev/null
+++ b/challenge-174/steve-g-lynn/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use List::Permutor;
+use Array::Compare;
+
+print &permutation2rank([1,0,2]),"\n"; # 2
+print &rank2permutation([0,1,2],1),"\n"; # 021
+
+sub permutation2rank {
+ my ($rarry) = @_;
+ my $comp = Array::Compare -> new;
+ my $ctr=0;
+ my $p = new List::Permutor (sort @$rarry);
+ while (my @set = $p->next) {
+ last if $comp->compare($rarry,\@set);
+ $ctr++;
+ }
+ return $ctr;
+}
+
+sub rank2permutation {
+ my ($rarry, $rank) = @_; my $ctr=0;
+ my $p = new List::Permutor (sort @$rarry);
+ my @set=();
+ while (@set = $p->next) {
+ last if $ctr==$rank;
+ $ctr++;
+ }
+ return @set;
+}
+
diff --git a/challenge-174/steve-g-lynn/raku/ch-1.p6 b/challenge-174/steve-g-lynn/raku/ch-1.p6
new file mode 100755
index 0000000000..d52579a196
--- /dev/null
+++ b/challenge-174/steve-g-lynn/raku/ch-1.p6
@@ -0,0 +1,23 @@
+#!/usr/bin/env raku
+
+#real 1m40.771s
+#user 1m40.149s
+#sys 0m0.150s
+
+for (0..3_000_000) { say $_ if $_ == &get_poly($_) }
+
+sub get_poly (Int $n) {
+#-- for base-10 integer abc.. return a^1+b^2+c^3...
+ my (@nstr)=$n.Str.split('');
+ shift @nstr; pop @nstr;
+ my @indx = 1..@nstr.elems;
+ my $retval=0;
+ for (0..(@nstr.elems-1)) -> $i {
+ $retval += (@nstr[$i] ** @indx[$i]);
+ }
+ return $retval;
+}
+
+
+
+
diff --git a/challenge-174/steve-g-lynn/raku/ch-2.p6 b/challenge-174/steve-g-lynn/raku/ch-2.p6
new file mode 100755
index 0000000000..f560cdbeed
--- /dev/null
+++ b/challenge-174/steve-g-lynn/raku/ch-2.p6
@@ -0,0 +1,14 @@
+#!/usr/bin/env raku
+
+say &permutation2rank( (1,0,2) ); # 2
+say &rank2permutation( (0,1,2), 1); # (0 2 1)
+
+sub permutation2rank (@arry) {
+ my @perms = @arry.sort.permutations;
+ return @perms.keys.grep( {@perms[$_] eqv @arry} );
+}
+
+sub rank2permutation (@arry, $rank) {
+ return @arry.sort.permutations[$rank];
+}
+