aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-174/kjetillll/perl/ch-1.pl15
-rw-r--r--challenge-174/kjetillll/perl/ch-2.pl27
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-174/kjetillll/perl/ch-1.pl b/challenge-174/kjetillll/perl/ch-1.pl
new file mode 100644
index 0000000000..0bad1b08f9
--- /dev/null
+++ b/challenge-174/kjetillll/perl/ch-1.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/perl
+use List::Util 'sum'; use v5.10; use strict; use warnings;
+
+my $want = 19;
+my $n = 0;
+
+while( $want > 0 ) {
+
+ my @d = split //, $n; #array d gets digits
+
+ say $n and $want-- if $n == sum( map $d[$_-1]**$_, 1..@d);
+
+ $n++;
+
+}
diff --git a/challenge-174/kjetillll/perl/ch-2.pl b/challenge-174/kjetillll/perl/ch-2.pl
new file mode 100644
index 0000000000..0a2b91a1b7
--- /dev/null
+++ b/challenge-174/kjetillll/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+use List::MoreUtils 'indexes'; use v5.10; use strict; use warnings;
+
+say permutation2rank( [1,0,2] );
+
+say "[@{[@{rank2permutation( [0,1,2], 1 )}]}]" if $"=',';
+
+sub permutation2rank {
+ indexes {"@$_" eq "@{ $_[0] }"} perms( sort {$a<=>$b} @{ $_[0] } )
+}
+
+sub rank2permutation {
+ ( perms( @{ $_[0] } ) )[pop]
+}
+
+sub perms {
+ my(@i,@r) = 0..$#_;
+ @_ || return;
+ while ( push @r, [@_[@i]] ) {
+ my $p = $#i || last;
+ --$p || last while $i[$p-1] > $i[$p];
+ push @i, reverse splice @i, my$q=$p;
+ ++$q while $i[$p-1] > $i[$q];
+ @i[$p-1,$q] = @i[$q,$p-1];
+ }
+ @r
+}