aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-241/adam-russell/blog.txt0
-rw-r--r--challenge-241/adam-russell/blog1.txt0
-rw-r--r--challenge-241/adam-russell/perl/ch-1.pl35
-rw-r--r--challenge-241/adam-russell/perl/ch-2.pl27
-rw-r--r--challenge-241/adam-russell/prolog/ch-1.p18
-rw-r--r--challenge-241/adam-russell/prolog/ch-2.p20
6 files changed, 100 insertions, 0 deletions
diff --git a/challenge-241/adam-russell/blog.txt b/challenge-241/adam-russell/blog.txt
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-241/adam-russell/blog.txt
diff --git a/challenge-241/adam-russell/blog1.txt b/challenge-241/adam-russell/blog1.txt
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-241/adam-russell/blog1.txt
diff --git a/challenge-241/adam-russell/perl/ch-1.pl b/challenge-241/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..ac9e406186
--- /dev/null
+++ b/challenge-241/adam-russell/perl/ch-1.pl
@@ -0,0 +1,35 @@
+use v5.36;
+use Data::Dump q/pp/;
+
+sub arithmetic_triplets{
+ my $counter = 0;
+ my $difference = shift;
+ arithmetic_triplets_r($difference, \$counter, [@_[0 .. @_ -1]], [@_[1 .. @_ -1]], [@_[2 .. @_ -1]]);
+ return $counter;
+}
+
+sub arithmetic_triplets_r{
+ my $difference = $_[0];
+ my $counter = $_[1];
+ my @i = @{$_[2]};
+ my @j = @{$_[3]};
+ my @k = @{$_[4]};
+ if(@i > 0 && @j > 0 && @k > 0){
+ $$counter++ if $j[0] - $i[0] == $difference && $k[0] - $j[0] == $difference;
+ arithmetic_triplets_r($difference, $counter, [@i], [@j], [@k[1 .. @k - 1]]);
+ }
+ elsif(@i > 0 && @k == 0 && @j > 0){
+ arithmetic_triplets_r($difference, $counter, [@i], [@j[1 .. @j - 1]], [@j[2 .. @j - 1]]);
+ }
+ elsif(@i > 0 && @k == 0 && @j == 0){
+ arithmetic_triplets_r($difference, $counter, [@i[1 .. @i - 1]], [@i[2 .. @i - 1]], [@i[3 .. @i - 1]]);
+ }
+}
+
+MAIN:{
+ my $difference;
+ $difference = 3;
+ say arithmetic_triplets $difference, 0, 1, 4, 6, 7, 10;
+ $difference = 2;
+ say arithmetic_triplets $difference, 4, 5, 6, 7, 8, 9;
+}
diff --git a/challenge-241/adam-russell/perl/ch-2.pl b/challenge-241/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..ea174fcba4
--- /dev/null
+++ b/challenge-241/adam-russell/perl/ch-2.pl
@@ -0,0 +1,27 @@
+use v5.36;
+
+sub prime_factor{
+ my $x = shift(@_);
+ my @factors;
+ for (my $y = 2; $y <= $x; $y++){
+ next if $x % $y;
+ $x /= $y;
+ push @factors, $y;
+ redo;
+ }
+ return @factors;
+}
+
+sub prime_order{
+ my %factor_i = map{($_, 0 + prime_factor($_))} @_;
+ my $factor_sorter = sub{
+ my $c = $factor_i{$a} <=> $factor_i{$b};
+ return $c unless !$c;
+ return $a <=> $b;
+ };
+ return sort $factor_sorter @_;
+}
+
+MAIN:{
+ say pp prime_order 11, 8, 27, 4;
+}
diff --git a/challenge-241/adam-russell/prolog/ch-1.p b/challenge-241/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..1c9943f890
--- /dev/null
+++ b/challenge-241/adam-russell/prolog/ch-1.p
@@ -0,0 +1,18 @@
+arithmetic_triplets(Numbers, Difference, TripletCount):-
+ [X, Y, Z|T] = Numbers,
+ arithmetic_triplets([X, Y, Z|T], [Y, Z|T], [Z|T], Difference, TripletCount).
+arithmetic_triplets([], [], [], _, 0).
+arithmetic_triplets([X, Y, Z|T], [Y, Z|T], [Z|T], Difference, TripletCount):-
+ arithmetic_triplets([X, Y, Z|T], [Z|T], [T], Difference, TripletCountNext),
+ Difference #= Y - X,
+ Difference #= Z - Y,
+ succ(TripletCountNext, TripletCount).
+arithmetic_triplets([X, Y, Z|T], [Y, Z|T], [Z|T], Difference, TripletCount):-
+ arithmetic_triplets([X, Y, Z|T], [Z|T], [T], Difference, TripletCountNext),
+ (Difference #\= Y - X; Difference #\= Z - Y),
+ TripletCount is TripletCountNext + 0.
+arithmetic_triplets([X, Y, Z|T], [Y, Z|T], [], Difference, TripletCount):-
+ arithmetic_triplets([X, Y, Z|T], [Z|T], [T], Difference, TripletCount).
+arithmetic_triplets([X, Y, Z|T], [], [], Difference, TripletCount):-
+ arithmetic_triplets([Y, Z|T], [Z|T], [T], Difference, TripletCount).
+
diff --git a/challenge-241/adam-russell/prolog/ch-2.p b/challenge-241/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..6535fb913d
--- /dev/null
+++ b/challenge-241/adam-russell/prolog/ch-2.p
@@ -0,0 +1,20 @@
+prime_factors(N, L):-
+ N > 0,
+ prime_factors(N, L, 2).
+prime_factors(1, [], _):-
+ !.
+prime_factors(N, [F|L], F):-
+ R is N // F,
+ N =:= R * F,
+ !,
+ prime_factors(R, L, F).
+prime_factors(N, L, F):-
+ next_factor(N, F, NF),
+ prime_factors(N, L, NF).
+next_factor(_, 2, 3):-
+ !.
+next_factor(N, F, NF):-
+ F * F < N,
+ !,
+ NF is F + 2.
+next_factor(N, _, N).