aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2022-03-20 18:38:23 -0400
committerAdam Russell <ac.russell@live.com>2022-03-20 18:38:23 -0400
commitec1477b72aaba7a9e6bd27406aedd47ec28e772d (patch)
tree9ac1bd71ec6994cbd9bdcc5fadb0b28e3f226636
parent24e63122ba071239bc964f16df34a87bdfa520e2 (diff)
downloadperlweeklychallenge-club-ec1477b72aaba7a9e6bd27406aedd47ec28e772d.tar.gz
perlweeklychallenge-club-ec1477b72aaba7a9e6bd27406aedd47ec28e772d.tar.bz2
perlweeklychallenge-club-ec1477b72aaba7a9e6bd27406aedd47ec28e772d.zip
initial commit
-rw-r--r--challenge-156/adam-russell/blog.txt1
-rw-r--r--challenge-156/adam-russell/blog1.txt1
-rw-r--r--challenge-156/adam-russell/perl/ch-1.pl33
-rw-r--r--challenge-156/adam-russell/perl/ch-2.pl33
-rw-r--r--challenge-156/adam-russell/prolog/ch-1.p12
-rw-r--r--challenge-156/adam-russell/prolog/ch-2.p36
6 files changed, 116 insertions, 0 deletions
diff --git a/challenge-156/adam-russell/blog.txt b/challenge-156/adam-russell/blog.txt
new file mode 100644
index 0000000000..766992c2ff
--- /dev/null
+++ b/challenge-156/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/03/20
diff --git a/challenge-156/adam-russell/blog1.txt b/challenge-156/adam-russell/blog1.txt
new file mode 100644
index 0000000000..7ee269dddc
--- /dev/null
+++ b/challenge-156/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/03/20
diff --git a/challenge-156/adam-russell/perl/ch-1.pl b/challenge-156/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..f7cb57d15f
--- /dev/null
+++ b/challenge-156/adam-russell/perl/ch-1.pl
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+##
+# Write a script to generate the first 10 Pernicious Numbers.
+##
+use Math::Primality qw/is_prime/;
+
+sub count_bits{
+ my($n) = @_;
+ my $total_count_set_bit = 0;
+ while($n){
+ my $b = $n & 1;
+ $total_count_set_bit++ if $b;
+ $n = $n >> 1;
+ }
+ return $total_count_set_bit;
+}
+
+sub first_n_pernicious{
+ my($n) = @_;
+ my @pernicious;
+ my $x = 1;
+ do{
+ my $set_bits = count_bits($x);
+ push @pernicious, $x if is_prime($set_bits);
+ $x++;
+ }while(@pernicious < $n);
+ return @pernicious;
+}
+
+MAIN:{
+ print join(", ", first_n_pernicious(10)) . "\n";
+} \ No newline at end of file
diff --git a/challenge-156/adam-russell/perl/ch-2.pl b/challenge-156/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..945f1ccaec
--- /dev/null
+++ b/challenge-156/adam-russell/perl/ch-2.pl
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+##
+# You are given number, $n > 0. Write a script to find out if
+# the given number is a Weird Number.
+##
+use boolean;
+use Data::PowerSet q/powerset/;
+
+sub factor{
+ my($n) = @_;
+ my @factors = (1);
+ foreach my $j (2 .. sqrt($n)){
+ push @factors, $j if $n % $j == 0;
+ push @factors, ($n / $j) if $n % $j == 0 && $j ** 2 != $n;
+ }
+ return @factors;
+}
+
+sub is_weird{
+ my($x) = @_;
+ my @factors = factor($x);
+ my $sum = unpack("%32I*", pack("I*", @factors));
+ for my $subset (@{powerset(@factors)}){
+ return false if unpack("%32I*", pack("I*", @{$subset})) == $x;
+ }
+ return boolean($sum > $x);
+}
+
+MAIN:{
+ print is_weird(12) . "\n";
+ print is_weird(70) . "\n";
+} \ No newline at end of file
diff --git a/challenge-156/adam-russell/prolog/ch-1.p b/challenge-156/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..c8dfbeaf9c
--- /dev/null
+++ b/challenge-156/adam-russell/prolog/ch-1.p
@@ -0,0 +1,12 @@
+pernicious(_) --> [].
+pernicious(Seen) --> [X], x(Seen, X), {set_bits(X, Bits), fd_prime(Bits)}, pernicious([X|Seen]).
+x(Seen, X) --> {between(1, 100, X), \+ member(X, Seen)}.
+
+set_bits(N, X):-
+ set_bits(N, 0, X).
+set_bits(0, X, X).
+set_bits(N, X_Acc, X):-
+ B is N /\ 1,
+ X0 is X_Acc + B,
+ N0 is N >> 1,
+ set_bits(N0, X0, X), !. \ No newline at end of file
diff --git a/challenge-156/adam-russell/prolog/ch-2.p b/challenge-156/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..e2f671c56a
--- /dev/null
+++ b/challenge-156/adam-russell/prolog/ch-2.p
@@ -0,0 +1,36 @@
+weird(_) --> [].
+weird(Seen) --> [X], x(Seen, X), {
+ findall(F, factor(X, F), Factors), flatten([1, Factors], FlatFactors),
+ sum_list(FlatFactors, FactorSum),
+ FactorSum > X,
+ powerset(FlatFactors, FactorSets),
+ maplist(sum_list, FactorSets, FactorSetSums),
+ \+ member(X, FactorSetSums)
+ },
+ weird([X|Seen]).
+x(Seen, X) --> {between(1, 1000, X), \+ member(X, Seen)}.
+
+powerset(X,Y):- bagof(S, subseq(S,X), Y).
+subseq([], []).
+subseq([], [_|_]).
+subseq([X|Xs], [X|Ys] ):- subseq(Xs, Ys).
+subseq([X|Xs], [_|Ys] ):- append(_, [X|Zs], Ys), subseq(Xs, Zs).
+
+factor(N, Factors):-
+ S is round(sqrt(N)),
+ fd_domain(X, 2, S),
+ R #= N rem X,
+ R #= 0,
+ Q #= N // X,
+ Q #\= X,
+ fd_labeling([Q, X]),
+ Factors = [Q, X].
+factor(N, Factors):-
+ S is round(sqrt(N)),
+ fd_domain(X, 2, S),
+ R #= N rem X,
+ R #= 0,
+ Q #= N // X,
+ Q #= X,
+ fd_labeling([Q]),
+ Factors = [Q]. \ No newline at end of file