aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-133/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-133/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-133/luca-ferrari/raku/ch-1.p618
-rw-r--r--challenge-133/luca-ferrari/raku/ch-2.p650
4 files changed, 70 insertions, 0 deletions
diff --git a/challenge-133/luca-ferrari/blog-1.txt b/challenge-133/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..6dc13d6def
--- /dev/null
+++ b/challenge-133/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+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
new file mode 100644
index 0000000000..a3a9243bdf
--- /dev/null
+++ b/challenge-133/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+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
new file mode 100644
index 0000000000..0f7a2ce5c4
--- /dev/null
+++ b/challenge-133/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,18 @@
+#!raku
+
+
+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;
+
+ }
+
+ $current-solution.say;
+
+}
diff --git a/challenge-133/luca-ferrari/raku/ch-2.p6 b/challenge-133/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..d2aaf350f4
--- /dev/null
+++ b/challenge-133/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,50 @@
+#!raku
+
+# a lazy list of all prime numbers
+my @PRIMES = grep {.is-prime}, 1..*;
+
+# 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;
+
+ 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;
+
+ # compute the remainder
+ $needle /= $current-factor;
+ }
+
+
+ @factors.sort;
+
+
+}
+
+
+# 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;
+}