From 5b0ec6d87fe33649117cde1fb7fee1ad2652a88c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 11 Oct 2021 11:09:32 +0200 Subject: First task done --- challenge-133/luca-ferrari/raku/ch-1.p6 | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/challenge-133/luca-ferrari/raku/ch-1.p6 b/challenge-133/luca-ferrari/raku/ch-1.p6 index 0f7a2ce5c4..87d0da2be5 100644 --- a/challenge-133/luca-ferrari/raku/ch-1.p6 +++ b/challenge-133/luca-ferrari/raku/ch-1.p6 @@ -1,18 +1,20 @@ #!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; - +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; + } } - $current-solution.say; + @pandigital[ $_ ].say for 0 ..^ $limit; } -- cgit From 9aada7bcc00f999ced23ebc04397fa51a1709234 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 11 Oct 2021 11:25:24 +0200 Subject: Task 2 done --- challenge-133/luca-ferrari/raku/ch-2.p6 | 60 ++++++++++++--------------------- 1 file 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; + } -- cgit From ed57faca04a582f079ae4baed5b2cf0ac9682a4b Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 11 Oct 2021 11:59:01 +0200 Subject: Allow all pandigit numbers. --- challenge-133/luca-ferrari/raku/ch-1.p6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-133/luca-ferrari/raku/ch-1.p6 b/challenge-133/luca-ferrari/raku/ch-1.p6 index 87d0da2be5..ef7c25b62c 100644 --- a/challenge-133/luca-ferrari/raku/ch-1.p6 +++ b/challenge-133/luca-ferrari/raku/ch-1.p6 @@ -11,7 +11,7 @@ sub MAIN( Int $limit where { $limit > 0 } = 5 ) { next if $start ~~ / ^0+ /; my $found = 0; $found += $current.comb.grep( $_ ).so ?? 1 !! 0 for @digits; - take $current if $found == @digits.elems; + take $current if $found >= @digits.elems; } } -- cgit From bce0b61e62a360898b99e98f1f602e0df558fb2d Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 11 Oct 2021 12:06:03 +0200 Subject: Blog references --- challenge-133/luca-ferrari/blog-1.txt | 2 +- challenge-133/luca-ferrari/blog-2.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-133/luca-ferrari/blog-1.txt b/challenge-133/luca-ferrari/blog-1.txt index 6dc13d6def..bc163279b2 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/09/PerlWeeklyChallegne133.html#task1 +https://fluca1978.github.io/2021/10/11/PerlWeeklyChallegne133.html#task1 diff --git a/challenge-133/luca-ferrari/blog-2.txt b/challenge-133/luca-ferrari/blog-2.txt index a3a9243bdf..65f4a25e54 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/09/PerlWeeklyChallegne133.html#task2 +https://fluca1978.github.io/2021/10/11/PerlWeeklyChallegne133.html#task2 -- cgit From 569133173c96ad5b2d391b58931f0bcdcb5d108b Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 11 Oct 2021 12:38:08 +0100 Subject: - Tidied up contributions by Luca Ferrari. --- challenge-133/luca-ferrari/blog-1.txt | 2 +- challenge-133/luca-ferrari/blog-2.txt | 2 +- challenge-133/luca-ferrari/raku/ch-1.p6 | 24 ++++++------- challenge-133/luca-ferrari/raku/ch-2.p6 | 60 +++++++++++++++++++++------------ challenge-134/luca-ferrari/blog-1.txt | 1 + challenge-134/luca-ferrari/blog-2.txt | 1 + challenge-134/luca-ferrari/raku/ch-1.p6 | 20 +++++++++++ challenge-134/luca-ferrari/raku/ch-2.p6 | 34 +++++++++++++++++++ 8 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 challenge-134/luca-ferrari/blog-1.txt create mode 100644 challenge-134/luca-ferrari/blog-2.txt create mode 100644 challenge-134/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-134/luca-ferrari/raku/ch-2.p6 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; + +} -- cgit