aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-174/adam-russell/blog.txt1
-rw-r--r--challenge-174/adam-russell/blog1.txt1
-rw-r--r--challenge-174/adam-russell/perl/ch-1.pl24
-rw-r--r--challenge-174/adam-russell/perl/ch-2.pl53
-rw-r--r--challenge-174/adam-russell/prolog/ch-1.p22
5 files changed, 101 insertions, 0 deletions
diff --git a/challenge-174/adam-russell/blog.txt b/challenge-174/adam-russell/blog.txt
new file mode 100644
index 0000000000..a4df9d46c5
--- /dev/null
+++ b/challenge-174/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/07/24 \ No newline at end of file
diff --git a/challenge-174/adam-russell/blog1.txt b/challenge-174/adam-russell/blog1.txt
new file mode 100644
index 0000000000..b9bcf60a36
--- /dev/null
+++ b/challenge-174/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/07/24 \ No newline at end of file
diff --git a/challenge-174/adam-russell/perl/ch-1.pl b/challenge-174/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..5419bea060
--- /dev/null
+++ b/challenge-174/adam-russell/perl/ch-1.pl
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+##
+# Write a script to generate the first 19 Disarium Numbers.
+##
+use POSIX;
+
+sub disarium_n{
+ my($n) = @_;
+ my @disariums;
+ map{
+ return @disariums if @disariums == $n;
+ my @digits = split(//, $_);
+ my $digit_sum = 0;
+ map{
+ $digit_sum += $digits[$_] ** ($_ + 1);
+ } 0 .. @digits - 1;
+ push @disariums, $digit_sum if $digit_sum == $_;
+ } 0 .. INT_MAX / 100;
+}
+
+MAIN:{
+ print join(", ", disarium_n(19)) . "\n";
+} \ No newline at end of file
diff --git a/challenge-174/adam-russell/perl/ch-2.pl b/challenge-174/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..44f70abadf
--- /dev/null
+++ b/challenge-174/adam-russell/perl/ch-2.pl
@@ -0,0 +1,53 @@
+use strict;
+use warnings;
+##
+# You are given a list of integers with no duplicates, e.g. [0, 1, 2].
+# Write two functions, permutation2rank() which will take the list and
+# determine its rank (starting at 0) in the set of possible permutations
+# arranged in lexicographic order, and rank2permutation() which will take
+# the list and a rank number and produce just that permutation.
+##
+package PermutationRanking{
+ use Mars::Class;
+ use List::Permutor;
+
+ attr q/list/;
+ attr q/permutations/;
+ attr q/permutations_sorted/;
+ attr q/permutations_ranked/;
+
+ sub BUILD{
+ my $self = shift;
+ my @permutations;
+ my %permutations_ranked;
+ my $permutor = new List::Permutor(@{$self->list()});
+ while(my @set = $permutor->next()) {
+ push @permutations, join(":", @set);
+ }
+ my @permutations_sorted = sort @permutations;
+ my $rank = 0;
+ for my $p (@permutations_sorted){
+ $permutations_ranked{$p} = $rank;
+ $rank++;
+ }
+ @permutations_sorted = map {[split(/:/, $_)]} @permutations_sorted;
+ $self->permutations_sorted(\@permutations_sorted);
+ $self->permutations_ranked(\%permutations_ranked);
+ }
+
+ sub permutation2rank{
+ my($self, $list) = @_;
+ return $self->permutations_ranked()->{join(":", @{$list})};
+ }
+
+ sub rank2permutation{
+ my($self, $n) = @_;
+ return "[" . join(", ", @{$self->permutations_sorted()->[$n]}) . "]";
+ }
+}
+
+package main{
+ my $ranker = new PermutationRanking(list => [0, 1, 2]);
+ print "[1, 0, 2] has rank " . $ranker->permutation2rank([1, 0, 2]) . "\n";
+ print "[" . join(", ", @{$ranker->list()}) . "]" . " has permutation at rank 1 --> " . $ranker->rank2permutation(1) . "\n";
+} \ No newline at end of file
diff --git a/challenge-174/adam-russell/prolog/ch-1.p b/challenge-174/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..f7f5f9ea4a
--- /dev/null
+++ b/challenge-174/adam-russell/prolog/ch-1.p
@@ -0,0 +1,22 @@
+disariums(_) --> [].
+disariums(Seen) --> [X], {disarium(X), \+ member(X, Seen)}, disariums([X|Seen]).
+
+sum_power(Digits, Sum):-
+ sum_power(Digits, 0, 0, Sum).
+sum_power([], _, Sum, Sum).
+sum_power([H|T], I, PartialSum, Sum):-
+ succ(I, N),
+ number_chars(X, [H]),
+ Partial is PartialSum + round(X ** N),
+ sum_power(T, N, Partial, Sum).
+
+disarium(X):-
+ current_prolog_flag(max_integer, MAX_INTEGER),
+ between(0, MAX_INTEGER, X),
+ number_chars(X, Chars),
+ sum_power(Chars, Sum),
+ Sum == X.
+
+n_disariums(N, Disariums):-
+ length(Disariums, N),
+ phrase(disariums([]), Disariums). \ No newline at end of file