diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-09 18:41:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-09 18:41:19 +0100 |
| commit | 8cc54f2af227f67ca297dab232bb632dc717f289 (patch) | |
| tree | e63ea06713f1a90ddb42cb772087c99c19c3d9ba | |
| parent | b02a1506628b2fdc21088a12fa0dbfb6b25e3b58 (diff) | |
| parent | ce1cb4aee69363f214b710a1fe6d631971e9bf9a (diff) | |
| download | perlweeklychallenge-club-8cc54f2af227f67ca297dab232bb632dc717f289.tar.gz perlweeklychallenge-club-8cc54f2af227f67ca297dab232bb632dc717f289.tar.bz2 perlweeklychallenge-club-8cc54f2af227f67ca297dab232bb632dc717f289.zip | |
Merge pull request #4991 from fluca1978/pwc133
Pwc133
| -rw-r--r-- | challenge-133/luca-ferrari/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-133/luca-ferrari/blog-2.txt | 1 | ||||
| -rw-r--r-- | challenge-133/luca-ferrari/raku/ch-1.p6 | 18 | ||||
| -rw-r--r-- | challenge-133/luca-ferrari/raku/ch-2.p6 | 50 |
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; +} |
