diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-07-20 16:03:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-20 16:03:42 +0100 |
| commit | e826cdb5c7bed81a4518a570044d208d211d0233 (patch) | |
| tree | 0170b3def35475e0a68e18c6b2e58a3a1c539648 | |
| parent | 53235d31a02a98f260b9a2de4c9efc5a78729116 (diff) | |
| parent | 6181fdc30a85c0920f9e6d8bd5c84b0cf96be5ba (diff) | |
| download | perlweeklychallenge-club-e826cdb5c7bed81a4518a570044d208d211d0233.tar.gz perlweeklychallenge-club-e826cdb5c7bed81a4518a570044d208d211d0233.tar.bz2 perlweeklychallenge-club-e826cdb5c7bed81a4518a570044d208d211d0233.zip | |
Merge pull request #6469 from steve-g-lynn/branch-for-challenge-174
pwc-174
| -rwxr-xr-x | challenge-174/steve-g-lynn/julia/ch-1.jl | 19 | ||||
| -rwxr-xr-x | challenge-174/steve-g-lynn/julia/ch-2.jl | 24 | ||||
| -rwxr-xr-x | challenge-174/steve-g-lynn/perl/alt-ch-1.pl | 29 | ||||
| -rwxr-xr-x | challenge-174/steve-g-lynn/perl/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-174/steve-g-lynn/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-174/steve-g-lynn/raku/ch-1.p6 | 23 | ||||
| -rwxr-xr-x | challenge-174/steve-g-lynn/raku/ch-2.p6 | 14 |
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]; +} + |
