diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-03-29 23:47:33 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-03-29 23:47:33 +0100 |
| commit | 5002c9e2bcf431285b74dd9fc51d0f62843abdb1 (patch) | |
| tree | 2fac283bb078e41649b88169686d27d252502019 /challenge-158 | |
| parent | 1ed331bfb0cf6fb4fe4c3023c9837cdee8eb616e (diff) | |
| download | perlweeklychallenge-club-5002c9e2bcf431285b74dd9fc51d0f62843abdb1.tar.gz perlweeklychallenge-club-5002c9e2bcf431285b74dd9fc51d0f62843abdb1.tar.bz2 perlweeklychallenge-club-5002c9e2bcf431285b74dd9fc51d0f62843abdb1.zip | |
- Added solutions to task 1 of week 158.
Diffstat (limited to 'challenge-158')
| -rw-r--r-- | challenge-158/mohammad-anwar/perl/ch-1.pl | 62 | ||||
| -rw-r--r-- | challenge-158/mohammad-anwar/raku/ch-1.raku | 50 |
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-158/mohammad-anwar/perl/ch-1.pl b/challenge-158/mohammad-anwar/perl/ch-1.pl new file mode 100644 index 0000000000..21f2684470 --- /dev/null +++ b/challenge-158/mohammad-anwar/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +=head1 + +Week 158: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-158 + +Task #1: Additive Primes + + Write a script to find out all Additive Primes <= 100. + +=cut + +use strict; +use warnings; +use Test::More; + +is_deeply( + additive_primes(100), + [ 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89 ], + 'Example' +); + +done_testing; + +# +# +# METHODS + +sub additive_primes { + my ($n) = @_; + + my $i = 1; + my $ap = []; + + while ($i <= $n) { + my $s = 0; + + ($i > 10) + && do { $s += $_ for (split //, $i); }; + + (is_prime($i)) + && do { push @$ap, $i if (($s == 0) || is_prime($s)); }; + + $i++; + } + + return $ap; +} + +sub is_prime { + my ($n) = @_; + + return 0 if ($n == 1); + + foreach my $i (2 .. sqrt $n) { + return 0 unless $n % $i + } + + return 1; +} diff --git a/challenge-158/mohammad-anwar/raku/ch-1.raku b/challenge-158/mohammad-anwar/raku/ch-1.raku new file mode 100644 index 0000000000..dd303a3eea --- /dev/null +++ b/challenge-158/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,50 @@ +#!/usr/bin/env raku + +=begin pod + +Week 158: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-158 + +Task #1: Additive Primes + + Write a script to find out all Additive Primes <= 100. + +=end pod + +use Test; + +is-deeply + additive-primes(100), + [ 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89 ], + 'Example'; + +done-testing; + +# +# +# METHOD + +sub additive-primes(Int $n) { + + my $i = 1; + my $ap = []; + + while ($i <= $n) { + my $s = 0; + + if ($i > 10) { + $s += $_ for ($i.split("")); + } + + if ($i.is-prime) { + if (($s == 0) || (($s > 0) && $s.is-prime)) { + $ap.push: $i; + } + } + + $i++; + } + + return $ap; +} |
