aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-133/luca-ferrari/raku/ch-2.p660
1 files changed, 22 insertions, 38 deletions
diff --git a/challenge-133/luca-ferrari/raku/ch-2.p6 b/challenge-133/luca-ferrari/raku/ch-2.p6
index d2aaf350f4..5538af30b1 100644
--- a/challenge-133/luca-ferrari/raku/ch-2.p6
+++ b/challenge-133/luca-ferrari/raku/ch-2.p6
@@ -1,50 +1,34 @@
#!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;
+sub MAIN( Int $cols where { $cols > 0 } = 5, Int $rows where { $rows > 0 } = 3 ) {
+ my @table;
+ my @distinct;
- for @PRIMES -> $current-factor {
- # stop if we got a bigger number
- last if $current-factor > $needle;
+ # table header
+ " x\t|\t".print;
+ ( 1 .. $cols ).join( "\t" ).say;
+ "--------|--------".print;
+ ( "-" x 8 x $cols ).say;
- # 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;
+ 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 );
+ }
+ }
-}
-
-
-# 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;
+ # print the table
+ for 1 .. $rows {
+ " $_\t|\t".print;
+ @table[ $_ - 1 ].join( "\t" ).say;
}
- @smith-numbers.join( "\n" ).say;
+ "\nDistinct values: ".say;
+ @distinct.join( ', ' ).say;
+
}