aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-11 12:38:08 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-11 12:38:08 +0100
commit569133173c96ad5b2d391b58931f0bcdcb5d108b (patch)
tree01a85a677b70d4d8cc68f6b9973660c08d0598f2
parentbce0b61e62a360898b99e98f1f602e0df558fb2d (diff)
downloadperlweeklychallenge-club-569133173c96ad5b2d391b58931f0bcdcb5d108b.tar.gz
perlweeklychallenge-club-569133173c96ad5b2d391b58931f0bcdcb5d108b.tar.bz2
perlweeklychallenge-club-569133173c96ad5b2d391b58931f0bcdcb5d108b.zip
- Tidied up contributions by Luca Ferrari.
-rw-r--r--challenge-133/luca-ferrari/blog-1.txt2
-rw-r--r--challenge-133/luca-ferrari/blog-2.txt2
-rw-r--r--challenge-133/luca-ferrari/raku/ch-1.p624
-rw-r--r--challenge-133/luca-ferrari/raku/ch-2.p660
-rw-r--r--challenge-134/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-134/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-134/luca-ferrari/raku/ch-1.p620
-rw-r--r--challenge-134/luca-ferrari/raku/ch-2.p634
8 files changed, 107 insertions, 37 deletions
diff --git a/challenge-133/luca-ferrari/blog-1.txt b/challenge-133/luca-ferrari/blog-1.txt
index bc163279b2..6dc13d6def 100644
--- a/challenge-133/luca-ferrari/blog-1.txt
+++ b/challenge-133/luca-ferrari/blog-1.txt
@@ -1 +1 @@
-https://fluca1978.github.io/2021/10/11/PerlWeeklyChallegne133.html#task1
+https://fluca1978.github.io/2021/10/09/PerlWeeklyChallegne133.html#task1
diff --git a/challenge-133/luca-ferrari/blog-2.txt b/challenge-133/luca-ferrari/blog-2.txt
index 65f4a25e54..a3a9243bdf 100644
--- a/challenge-133/luca-ferrari/blog-2.txt
+++ b/challenge-133/luca-ferrari/blog-2.txt
@@ -1 +1 @@
-https://fluca1978.github.io/2021/10/11/PerlWeeklyChallegne133.html#task2
+https://fluca1978.github.io/2021/10/09/PerlWeeklyChallegne133.html#task2
diff --git a/challenge-133/luca-ferrari/raku/ch-1.p6 b/challenge-133/luca-ferrari/raku/ch-1.p6
index ef7c25b62c..0f7a2ce5c4 100644
--- a/challenge-133/luca-ferrari/raku/ch-1.p6
+++ b/challenge-133/luca-ferrari/raku/ch-1.p6
@@ -1,20 +1,18 @@
#!raku
-sub MAIN( Int $limit where { $limit > 0 } = 5 ) {
- my @digits = 1 .. 9;
- @digits.push: 0;
- my $start = @digits.join;
-
- my @pandigital = lazy gather {
- for $start ..^ Inf -> $current {
- next if $start ~~ / ^0+ /;
- my $found = 0;
- $found += $current.comb.grep( $_ ).so ?? 1 !! 0 for @digits;
- take $current if $found >= @digits.elems;
- }
+sub MAIN( Int $n where { $n > 0 } ) {
+ $n.say and exit if $n == 1;
+
+ my Int $current-solution = $n +> 1; # divide by two
+ my Int $next-solution = 0;
+ while ( $next-solution < $current-solution ) {
+ $next-solution = ( $current-solution + $n / $current-solution ) +> 1 if ! $next-solution;
+ ( $current-solution, $next-solution ) = $next-solution,
+ ( $next-solution + $n / $next-solution ) +> 1;
+
}
- @pandigital[ $_ ].say for 0 ..^ $limit;
+ $current-solution.say;
}
diff --git a/challenge-133/luca-ferrari/raku/ch-2.p6 b/challenge-133/luca-ferrari/raku/ch-2.p6
index 5538af30b1..d2aaf350f4 100644
--- a/challenge-133/luca-ferrari/raku/ch-2.p6
+++ b/challenge-133/luca-ferrari/raku/ch-2.p6
@@ -1,34 +1,50 @@
#!raku
+# a lazy list of all prime numbers
+my @PRIMES = grep {.is-prime}, 1..*;
-sub MAIN( Int $cols where { $cols > 0 } = 5, Int $rows where { $rows > 0 } = 3 ) {
- my @table;
- my @distinct;
+# divide a number into a list of its own factors
+multi do-factor( 1 ) { (1) }
+multi do-factor( Int $n where { $n > 1 } ) {
+ my $needle = $n;
+ my @factors;
- # table header
- " x\t|\t".print;
- ( 1 .. $cols ).join( "\t" ).say;
- "--------|--------".print;
- ( "-" x 8 x $cols ).say;
+ for @PRIMES -> $current-factor {
+ # stop if we got a bigger number
+ last if $current-factor > $needle;
+ # skip if the number is not a divisor of what we are searching for
+ next unless $needle %% $current-factor;
+ # if here, it is a good factor
+ @factors.push: $current-factor;
-
- for 1 .. $rows -> $current-row {
- for 1 .. $cols -> $current-col {
- my $value = $current-row * $current-col;
- @table[ $current-row - 1 ].push: $value;
- @distinct.push: $value if ! @distinct.grep( $value );
- }
+ # compute the remainder
+ $needle /= $current-factor;
}
- # print the table
- for 1 .. $rows {
- " $_\t|\t".print;
- @table[ $_ - 1 ].join( "\t" ).say;
- }
+
+ @factors.sort;
+
+
+}
- "\nDistinct values: ".say;
- @distinct.join( ', ' ).say;
+# It is a smith number if the sum of the digits
+# is the sum of the factors
+sub is-smith-number( Int $n where { $n > 0 } ) {
+ return $n.comb.sum == do-factor( $n ).sum;
+}
+
+
+sub MAIN( Int $limit where { $limit > 0 } = 10 ) {
+
+ my @smith-numbers;
+ for 1 .. Inf {
+ next if ! is-smith-number( $_ );
+ @smith-numbers.push: $_;
+ last if @smith-numbers.elems == $limit;
+ }
+
+ @smith-numbers.join( "\n" ).say;
}
diff --git a/challenge-134/luca-ferrari/blog-1.txt b/challenge-134/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..bc163279b2
--- /dev/null
+++ b/challenge-134/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/10/11/PerlWeeklyChallegne133.html#task1
diff --git a/challenge-134/luca-ferrari/blog-2.txt b/challenge-134/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..65f4a25e54
--- /dev/null
+++ b/challenge-134/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/10/11/PerlWeeklyChallegne133.html#task2
diff --git a/challenge-134/luca-ferrari/raku/ch-1.p6 b/challenge-134/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..ef7c25b62c
--- /dev/null
+++ b/challenge-134/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,20 @@
+#!raku
+
+
+sub MAIN( Int $limit where { $limit > 0 } = 5 ) {
+ my @digits = 1 .. 9;
+ @digits.push: 0;
+ my $start = @digits.join;
+
+ my @pandigital = lazy gather {
+ for $start ..^ Inf -> $current {
+ next if $start ~~ / ^0+ /;
+ my $found = 0;
+ $found += $current.comb.grep( $_ ).so ?? 1 !! 0 for @digits;
+ take $current if $found >= @digits.elems;
+ }
+ }
+
+ @pandigital[ $_ ].say for 0 ..^ $limit;
+
+}
diff --git a/challenge-134/luca-ferrari/raku/ch-2.p6 b/challenge-134/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..5538af30b1
--- /dev/null
+++ b/challenge-134/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,34 @@
+#!raku
+
+
+sub MAIN( Int $cols where { $cols > 0 } = 5, Int $rows where { $rows > 0 } = 3 ) {
+ my @table;
+ my @distinct;
+
+ # table header
+ " x\t|\t".print;
+ ( 1 .. $cols ).join( "\t" ).say;
+ "--------|--------".print;
+ ( "-" x 8 x $cols ).say;
+
+
+
+
+ for 1 .. $rows -> $current-row {
+ for 1 .. $cols -> $current-col {
+ my $value = $current-row * $current-col;
+ @table[ $current-row - 1 ].push: $value;
+ @distinct.push: $value if ! @distinct.grep( $value );
+ }
+ }
+
+ # print the table
+ for 1 .. $rows {
+ " $_\t|\t".print;
+ @table[ $_ - 1 ].join( "\t" ).say;
+ }
+
+ "\nDistinct values: ".say;
+ @distinct.join( ', ' ).say;
+
+}