diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-05 19:14:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-05 19:14:02 +0100 |
| commit | ae31c930d3f8c50b2c09031decb24f5921a460af (patch) | |
| tree | 7fa38a91bbb47ea13ecb6f2407e74fb812778291 | |
| parent | efd1f80fd2548d7dc6569e0a15bef1484526e8db (diff) | |
| parent | 615f7aa7e2613c9daea36b8456c4771a98399b90 (diff) | |
| download | perlweeklychallenge-club-ae31c930d3f8c50b2c09031decb24f5921a460af.tar.gz perlweeklychallenge-club-ae31c930d3f8c50b2c09031decb24f5921a460af.tar.bz2 perlweeklychallenge-club-ae31c930d3f8c50b2c09031decb24f5921a460af.zip | |
Merge pull request #4973 from polettix/polettix/pwc133
Add polettix's solution to challenge-133
| -rw-r--r-- | challenge-133/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-133/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-133/polettix/perl/ch-1.pl | 13 | ||||
| -rw-r--r-- | challenge-133/polettix/perl/ch-2.pl | 33 | ||||
| -rw-r--r-- | challenge-133/polettix/raku/ch-1.raku | 10 | ||||
| -rw-r--r-- | challenge-133/polettix/raku/ch-2.raku | 28 |
6 files changed, 86 insertions, 0 deletions
diff --git a/challenge-133/polettix/blog.txt b/challenge-133/polettix/blog.txt new file mode 100644 index 0000000000..7c61e5cf32 --- /dev/null +++ b/challenge-133/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/10/06/pwc133-integer-square-root/ diff --git a/challenge-133/polettix/blog1.txt b/challenge-133/polettix/blog1.txt new file mode 100644 index 0000000000..2df1e12cfe --- /dev/null +++ b/challenge-133/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/10/07/pwc133-smith-numbers/ diff --git a/challenge-133/polettix/perl/ch-1.pl b/challenge-133/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..8f20ddef96 --- /dev/null +++ b/challenge-133/polettix/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; +sub integer_square_root ($n) { + return $n if $n < 2; + my $x = $n >> 1; # first estimate + my $y = $x + 1; # just to get started with $x < $y + ($x, $y) = (($x + int($n / $x)) >> 1, $x) while $x < $y; + return $y; +} +say integer_square_root(shift // die "$0 <n>\n"); diff --git a/challenge-133/polettix/perl/ch-2.pl b/challenge-133/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..23327d1aa6 --- /dev/null +++ b/challenge-133/polettix/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; +use List::Util 'sum'; +sub is_smith ($x) { + my $sum = sum split m{}mxs, $x; + my $div = 2; + my $ndiv = 0; + while ($x > 1 && $sum > -1) { + if ($x % $div == 0) { + my $subsum = sum split m{}mxs, $div; + while ($x % $div == 0) { + $sum -= $subsum; + $x /= $div; + ++$ndiv; + } + } + $div = $div % 2 ? $div + 2 : 3; + } + return $sum == 0 && $ndiv > 1; +} +sub smith_first ($n) { + my @retval; + my $candidate = 3; # one less of first composite number + while ($n > @retval) { + next unless is_smith(++$candidate); + push @retval, $candidate; + } + return @retval; +} +say for smith_first(shift // 10); diff --git a/challenge-133/polettix/raku/ch-1.raku b/challenge-133/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..0db13b24d8 --- /dev/null +++ b/challenge-133/polettix/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku +use v6; +sub integer-square-root (Int:D $n where * >= 0) { + return $n if $n < 2; + my $x = $n +> 1; # first estimate + my $y = $x + 1; # just to get started with $x < $y + ($x, $y) = (($x + ($n / $x).Int) +> 1, $x) while $x < $y; + return $y; +} +sub MAIN (Int:D $n where * >= 0) { put integer-square-root($n) } diff --git a/challenge-133/polettix/raku/ch-2.raku b/challenge-133/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..bc0639034d --- /dev/null +++ b/challenge-133/polettix/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; +sub is-smith (Int:D() $x is copy where * > 0) { + my $sum = $x.comb(/\d/).sum; + my $div = 2; + my $ndiv = 0; + while $x > 1 && $sum > -1 { + if $x %% $div { + my $subsum = $div.comb(/\d/).sum; + while $x %% $div { + $sum -= $subsum; + $x /= $div; + ++$ndiv; + } + } + $div += $div == 2 ?? 1 !! 2; + } + return $sum == 0 && $ndiv > 1; +} +sub smith-first (Int:D $n is copy where * > 0) { + my $candidate = 3; # one less of first composite number + gather while $n > 0 { + next unless is-smith(++$candidate); + take $candidate; + --$n; + } +} +sub MAIN ($n = 10) { .put for smith-first($n) } |
