aboutsummaryrefslogtreecommitdiff
path: root/challenge-156
diff options
context:
space:
mode:
author冯昶 <seaker@qq.com>2022-03-22 11:59:57 +0800
committer冯昶 <seaker@qq.com>2022-03-22 11:59:57 +0800
commit95b49b5018a5c11dab33bfe6eeddf439d0de9dcb (patch)
tree07b90a57f3c44980b442eeca35abe5ef48484061 /challenge-156
parentcf12d1ad0715c7481e8a0a4e0ecdfb52146544cb (diff)
parent473825ccd58e668c77aecd33789f13aad6646f01 (diff)
downloadperlweeklychallenge-club-95b49b5018a5c11dab33bfe6eeddf439d0de9dcb.tar.gz
perlweeklychallenge-club-95b49b5018a5c11dab33bfe6eeddf439d0de9dcb.tar.bz2
perlweeklychallenge-club-95b49b5018a5c11dab33bfe6eeddf439d0de9dcb.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-156')
-rw-r--r--challenge-156/0rir/raku/ch-1.raku12
-rw-r--r--challenge-156/0rir/raku/ch-2.raku94
-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
-rw-r--r--challenge-156/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-156/arne-sommer/raku/ch-1.raku7
-rwxr-xr-xchallenge-156/arne-sommer/raku/ch-2.raku23
-rwxr-xr-xchallenge-156/arne-sommer/raku/pernicious-numbers7
-rwxr-xr-xchallenge-156/arne-sommer/raku/unpernicious-numbers7
-rwxr-xr-xchallenge-156/arne-sommer/raku/weird-number23
-rwxr-xr-xchallenge-156/arne-sommer/raku/weird-number-exit23
-rwxr-xr-xchallenge-156/arne-sommer/raku/weird-number-exits38
-rwxr-xr-xchallenge-156/arne-sommer/raku/weird-number-seq32
-rw-r--r--challenge-156/athanasius/perl/ch-1.pl149
-rw-r--r--challenge-156/athanasius/perl/ch-2.pl253
-rw-r--r--challenge-156/athanasius/raku/ch-1.raku122
-rw-r--r--challenge-156/athanasius/raku/ch-2.raku254
-rw-r--r--challenge-156/cheok-yin-fung/blog.txt1
-rw-r--r--challenge-156/cheok-yin-fung/perl/ch-1.pl40
-rw-r--r--challenge-156/cheok-yin-fung/perl/ch-2.pl93
-rw-r--r--challenge-156/colin-crain/blog.txt1
-rw-r--r--challenge-156/colin-crain/blog1.txt1
-rwxr-xr-xchallenge-156/colin-crain/perl/ch-1.pl144
-rwxr-xr-xchallenge-156/colin-crain/perl/ch-2.pl145
-rwxr-xr-xchallenge-156/colin-crain/raku/ch-1.raku18
-rwxr-xr-xchallenge-156/colin-crain/raku/ch-2.raku44
-rw-r--r--challenge-156/duncan-c-white/README78
-rwxr-xr-xchallenge-156/duncan-c-white/perl/ch-1.pl87
-rwxr-xr-xchallenge-156/duncan-c-white/perl/ch-2.pl123
-rw-r--r--challenge-156/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-156/jaldhar-h-vyas/perl/ch-1.pl36
-rwxr-xr-xchallenge-156/jaldhar-h-vyas/perl/ch-2.pl55
-rwxr-xr-xchallenge-156/jaldhar-h-vyas/raku/ch-1.raku15
-rwxr-xr-xchallenge-156/jaldhar-h-vyas/raku/ch-2.raku19
-rwxr-xr-xchallenge-156/jo-37/perl/ch-1.pl71
-rwxr-xr-xchallenge-156/jo-37/perl/ch-2.pl74
-rw-r--r--challenge-156/kueppo-wesley/README1
-rw-r--r--challenge-156/kueppo-wesley/blog.txt1
-rw-r--r--challenge-156/kueppo-wesley/blog1.txt1
-rw-r--r--challenge-156/kueppo-wesley/perl/ch-1.pl54
-rw-r--r--challenge-156/kueppo-wesley/perl/ch-2.pl77
-rw-r--r--challenge-156/laurent-rosenfeld/julia/ch-1.jl16
-rw-r--r--challenge-156/laurent-rosenfeld/python/ch-1.py15
-rw-r--r--challenge-156/lubos-kolouch/perl/ch-1.pl23
-rw-r--r--challenge-156/lubos-kolouch/perl/ch-2.pl30
-rw-r--r--challenge-156/lubos-kolouch/python/ch-1.py22
-rw-r--r--challenge-156/lubos-kolouch/python/ch-2.py27
-rw-r--r--challenge-156/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-156/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-156/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-156/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-156/luca-ferrari/postgresql/ch-1.sql35
-rw-r--r--challenge-156/luca-ferrari/postgresql/ch-2.sql48
-rw-r--r--challenge-156/luca-ferrari/raku/ch-1.p611
-rw-r--r--challenge-156/luca-ferrari/raku/ch-2.p68
-rwxr-xr-xchallenge-156/mattneleigh/perl/ch-1.pl80
-rwxr-xr-xchallenge-156/perlboy1967/perl/ch-1.pl39
-rwxr-xr-xchallenge-156/perlboy1967/perl/ch-2.pl62
-rwxr-xr-xchallenge-156/pete-houston/perl/ch-1.pl57
-rw-r--r--challenge-156/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-156/peter-campbell-smith/perl/ch-1.pl36
-rwxr-xr-xchallenge-156/peter-campbell-smith/perl/ch-2.pl66
-rw-r--r--challenge-156/polettix/blog.txt1
-rw-r--r--challenge-156/polettix/blog1.txt1
-rw-r--r--challenge-156/polettix/perl/ch-1.pl27
-rw-r--r--challenge-156/polettix/perl/ch-2.pl27
-rw-r--r--challenge-156/polettix/perl/cpanfile2
-rw-r--r--challenge-156/polettix/perl/cpanfile.snapshot6
-rw-r--r--challenge-156/polettix/raku/ch-1.raku14
-rw-r--r--challenge-156/polettix/raku/ch-2.raku33
-rw-r--r--challenge-156/roger-bell-west/blog.txt1
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/README.md12
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/bb.edn4
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/deps.edn4
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/src/c156/t1.clj12
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/src/c156/t2.clj25
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/test/c156/t1_test.clj10
-rw-r--r--challenge-156/tyler-wardhaugh/clojure/test/c156/t2_test.clj8
-rw-r--r--challenge-156/wambash/raku/ch-1.raku17
84 files changed, 3087 insertions, 57 deletions
diff --git a/challenge-156/0rir/raku/ch-1.raku b/challenge-156/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..d23a382034
--- /dev/null
+++ b/challenge-156/0rir/raku/ch-1.raku
@@ -0,0 +1,12 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtabs
+use v6.d;
+
+#1 Pernicious Numbers
+
+(gather {
+ for 1…∞ {
+ .take if .base(2).split('').grep( '1' ).chars.is-prime()
+ }
+})[0..9].join( ', ', :skip-empty ).say;
+
diff --git a/challenge-156/0rir/raku/ch-2.raku b/challenge-156/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..d47c7ce64c
--- /dev/null
+++ b/challenge-156/0rir/raku/ch-2.raku
@@ -0,0 +1,94 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtabs
+use v6.d;
+
+# 2 Weird Number
+
+my $n = 5;
+
+my \TEST = False;
+
+if TEST {
+ my @w = 70, 836, 4030, 5830, 7192, 7912, 9272, 10430, 10570,
+ 10792, 10990, 11410, 11690, 12110, 12530, 12670,
+ 13370, 13510, 13790, 13930, 14770, 15610, 15890,
+ 16030, 16310, 16730, 16870, 17272, 17570, 17990,
+ 18410, 18830, 18970, 19390, 19670;
+
+ for 10, 12, 20, 21, 5000, 88,
+ 70, 836, 4030, 5830, 7192, 7912, 9272, 10430, 10570,
+ 10792, 10990, 11410, 11690, 12110, 12530, 12670,
+ 13370, 13510, 13790, 13930, 14770, 15610, 15890,
+ 16030, 16310, 16730, 16870, 17272, 17570, 17990,
+ 18410, 18830, 18970, 19390, 19670
+ -> $x {
+ if is_weird($x) {
+ die unless $x == @w.any;
+ say $x;
+ }else{
+ die unless $x == @w.none;
+ }
+ }
+}
+
+sub MAIN( Int $x = 70 ) {
+ my @state = is_weird( $x);
+ present( | @state);
+ exit;
+}
+
+sub is_weird( Int $n where $n > 1, --> Any ) {
+
+ my @proper = ( 1 … Int($n/2) ).grep: $n %% *;
+ my $sum = [+] @proper;
+
+ if $n ≥ $sum { return ( False, @proper, $n, [], 'gte') but False; }
+
+ my $diff = $sum - $n;
+ my @equalizer = @proper.grep( * ≤ $diff).combinations;
+ sink @equalizer.shift;
+ for @equalizer -> @e {
+ if $diff == [+] @e {
+ return (False, @proper, $n, @e, 'match') but False;
+ }
+ }
+ return ( True, @proper, $n, [], 'fine') but True;
+}
+
+sub present( Bool $flag, @proper, Int $n, @variance, Str $annot ) {
+ say "Input: \$n = $n\nOutput: $flag.Int()\n";
+ _format( $flag, @proper, $n, @variance, $annot );
+}
+
+sub _format( Bool $flag, @p, Int $n, @variance, Str $annot ) {
+ given $annot {
+ when 'gte' {
+ say "Since the proper divisor" ~ _format_list(@p)
+ ~ " sum to " ~ ([+] @p)
+ ~ " which is not greater than $n.";
+ }
+ when 'match' {
+ say "Since the proper divisor", _format_list(@p),
+ " sum to ", ( [+] @p), " but the subset",
+ _format_list((@p (-) @variance).Hash.keys.sort).substr(5),
+ " does sum to $n." ;
+ }
+ when 'fine' {
+ say "Since the proper divisors of $n "
+ ~ _format_list( @p ).substr( 2) ~ " sums to "
+ ~ ([+] @p) ~ " and no subset of divisors sums to $n.";
+ }
+ default { die "bad annot -- unreachable w/ $n";
+ }
+ }
+ return;
+}
+
+sub _format_list( @list --> Str ) {
+ given @list.elems {
+ when 1 { return "is @list[0] " }
+ when 2 { return "s are @list[0] and @list[1] " }
+ default { return "s are @list[0..*-2].join(', ') and @list[*-1]" }
+ }
+ die "unreachable";
+}
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
diff --git a/challenge-156/arne-sommer/blog.txt b/challenge-156/arne-sommer/blog.txt
new file mode 100644
index 0000000000..bc8920923c
--- /dev/null
+++ b/challenge-156/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/weirdly-pernicious.html
diff --git a/challenge-156/arne-sommer/raku/ch-1.raku b/challenge-156/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..727105e251
--- /dev/null
+++ b/challenge-156/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,7 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $length where $length > 0 = 10);
+
+my $pn := (1..Inf).grep({ $_.fmt('%b').comb.sum.is-prime });
+
+$pn.head($length).join(", ").say;
diff --git a/challenge-156/arne-sommer/raku/ch-2.raku b/challenge-156/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..e63cee9fab
--- /dev/null
+++ b/challenge-156/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,23 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $n where $n > 0);
+
+my @proper-divisors = divisors($n, :not-self);
+
+say @proper-divisors.sum <= $n
+ ?? 0
+ !! + ! so any(@proper-divisors.combinations>>.sum) == $n;
+
+sub divisors ($number, :$not-self, :$not-one)
+{
+ my @divisors;
+
+ for ($not-one ?? 2 !! 1) .. $number/2 -> $candidate
+ {
+ @divisors.push: $candidate if $number %% $candidate;
+ }
+
+ @divisors.push: $number unless $not-self;
+
+ return @divisors;
+}
diff --git a/challenge-156/arne-sommer/raku/pernicious-numbers b/challenge-156/arne-sommer/raku/pernicious-numbers
new file mode 100755
index 0000000000..727105e251
--- /dev/null
+++ b/challenge-156/arne-sommer/raku/pernicious-numbers
@@ -0,0 +1,7 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $length where $length > 0 = 10);
+
+my $pn := (1..Inf).grep({ $_.fmt('%b').comb.sum.is-prime });
+
+$pn.head($length).join(", ").say;
diff --git a/challenge-156/arne-sommer/raku/unpernicious-numbers b/challenge-156/arne-sommer/raku/unpernicious-numbers
new file mode 100755
index 0000000000..b7b6510869
--- /dev/null
+++ b/challenge-156/arne-sommer/raku/unpernicious-numbers
@@ -0,0 +1,7 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (Int $length where $length > 0 = 10);
+
+my $pn := (1..Inf).grep({ $_.fmt('%b').comb.sum.is-prime.not });
+
+$pn.head($length).join(", ").say;
diff --git a/challenge-156/arne-sommer/raku/weird-number b/challenge-156/arne-sommer/raku/weird-number