aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2023-11-05 18:20:05 -0500
committerAdam Russell <ac.russell@live.com>2023-11-05 18:20:05 -0500
commitb1a13353882d5d223e3d6c3ba967d9eb6d7dd5cd (patch)
tree2a4876276e5bbda4c2200884c07c4ac702b3660e
parent06390cf1cc9f59437279b48d8c81f69905a7f3b4 (diff)
downloadperlweeklychallenge-club-b1a13353882d5d223e3d6c3ba967d9eb6d7dd5cd.tar.gz
perlweeklychallenge-club-b1a13353882d5d223e3d6c3ba967d9eb6d7dd5cd.tar.bz2
perlweeklychallenge-club-b1a13353882d5d223e3d6c3ba967d9eb6d7dd5cd.zip
updated solutions
-rw-r--r--challenge-241/adam-russell/blog.txt1
-rw-r--r--challenge-241/adam-russell/blog1.txt1
-rw-r--r--challenge-241/adam-russell/perl/ch-1.pl12
-rw-r--r--challenge-241/adam-russell/perl/ch-2.pl10
-rw-r--r--challenge-241/adam-russell/prolog/ch-1.p24
-rw-r--r--challenge-241/adam-russell/prolog/ch-2.p38
6 files changed, 68 insertions, 18 deletions
diff --git a/challenge-241/adam-russell/blog.txt b/challenge-241/adam-russell/blog.txt
index e69de29bb2..1fe8815a02 100644
--- a/challenge-241/adam-russell/blog.txt
+++ b/challenge-241/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://rabbitfarm.com/cgi-bin/blosxom/perl/2023/11/05 \ No newline at end of file
diff --git a/challenge-241/adam-russell/blog1.txt b/challenge-241/adam-russell/blog1.txt
index e69de29bb2..8c8d81b849 100644
--- a/challenge-241/adam-russell/blog1.txt
+++ b/challenge-241/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://rabbitfarm.com/cgi-bin/blosxom/prolog/2023/11/05 \ No newline at end of file
diff --git a/challenge-241/adam-russell/perl/ch-1.pl b/challenge-241/adam-russell/perl/ch-1.pl
index ac9e406186..25b5b7d4bb 100644
--- a/challenge-241/adam-russell/perl/ch-1.pl
+++ b/challenge-241/adam-russell/perl/ch-1.pl
@@ -1,6 +1,12 @@
-use v5.36;
-use Data::Dump q/pp/;
-
+use v5.38;
+##
+# You are given an array (3 or more members) of integers in increasing order and a
+# positive integer. Write a script to find out the number of unique Arithmetic Triplets
+# satisfying the following rules:
+# a) i < j < k
+# b) nums[j] - nums[i] == diff
+# c) nums[k] - nums[j] == diff
+##
sub arithmetic_triplets{
my $counter = 0;
my $difference = shift;
diff --git a/challenge-241/adam-russell/perl/ch-2.pl b/challenge-241/adam-russell/perl/ch-2.pl
index ea174fcba4..386ad810a5 100644
--- a/challenge-241/adam-russell/perl/ch-2.pl
+++ b/challenge-241/adam-russell/perl/ch-2.pl
@@ -1,5 +1,9 @@
-use v5.36;
-
+use v5.38;
+##
+# You are given an array of unique positive integers greater than 2.
+# Write a script to sort them in ascending order of the count of their prime factors,
+# tie-breaking by ascending value.
+##
sub prime_factor{
my $x = shift(@_);
my @factors;
@@ -23,5 +27,5 @@ sub prime_order{
}
MAIN:{
- say pp prime_order 11, 8, 27, 4;
+ say join q/, /, 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
index 1c9943f890..a5bef464b4 100644
--- a/challenge-241/adam-russell/prolog/ch-1.p
+++ b/challenge-241/adam-russell/prolog/ch-1.p
@@ -1,18 +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,
+arithmetic_triplets([], [], [], _, 0).
+arithmetic_triplets([IX, IY, IZ|I], [JY, JZ|J], [], Difference, TripletCount):-
+ arithmetic_triplets([IX, IY, IZ|I], [JZ|J], J, Difference, TripletCount).
+arithmetic_triplets([_, IY, IZ|I], [], [], Difference, TripletCount):-
+ arithmetic_triplets([IY, IZ|I], [IZ|I], I, Difference, TripletCount).
+arithmetic_triplets([IX, IY, IZ|I], [JY, JZ|J], [KZ|K], Difference, TripletCount):-
+ Difference #= JY - IX,
+ Difference #= KZ - JY,
+ arithmetic_triplets([IX, IY, IZ|I], [JY, JZ|J], K, Difference, TripletCountNext),
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),
+arithmetic_triplets([IX, IY, IZ|I], [JY, JZ|J], [KZ|K], Difference, TripletCount):-
+ (Difference #\= JY - IX; Difference #\= KZ - JY),
+ arithmetic_triplets([IX, IY, IZ|I], [JY, JZ|J], K, Difference, TripletCountNext),
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
index 6535fb913d..1b267a5ccc 100644
--- a/challenge-241/adam-russell/prolog/ch-2.p
+++ b/challenge-241/adam-russell/prolog/ch-2.p
@@ -18,3 +18,41 @@ next_factor(N, F, NF):-
!,
NF is F + 2.
next_factor(N, _, N).
+
+kvf_insert_sort(List,Sorted):-
+ i_sort(List,[],Sorted).
+
+i_sort([],Acc,Acc).
+i_sort([H|T],Acc,Sorted):-
+ kvf_insert(H,Acc,NAcc),
+ i_sort(T,NAcc,Sorted).
+
+kvf_insert(K0-V0,[K1-V1|T],[K1-V1|NT]):-
+ V0 > V1,
+ kvf_insert(K0-V0,T,NT).
+kvf_insert(K0-V0,[K1-V1|T],[K0-V0,K1-V1|T]):-
+ V0 < V1.
+kvf_insert(K0-V0,[K1-V1|T],[K1-V1|NT]):-
+ V0 = V1,
+ K0 > K1,
+ kvf_insert(K0-V0,T,NT).
+kvf_insert(K0-V0,[K1-V1|T],[K0-V0,K1-V1|T]):-
+ V0 = V1,
+ K0 < K1.
+kvf_insert(K0-V0, [], [K0-V0]).
+
+write_factor_sorted([K-_|[]]):-
+ write(K),
+ nl.
+write_factor_sorted([K-_|T]):-
+ write(K),
+ write(', '),
+ write_factor_sorted(T).
+
+factor_counter(Number, Number-FactorCount):-
+ prime_factors(Number, Factors),
+ length(Factors, FactorCount).
+
+factor_sorter(Numbers, FactorsSorted):-
+ maplist(factor_counter, Numbers, Factors),
+ kvf_insert_sort(Factors, FactorsSorted). \ No newline at end of file