aboutsummaryrefslogtreecommitdiff
path: root/challenge-158
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-01 12:36:34 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-01 12:36:34 +0100
commit9dd5c70e46bc2d018e505e7f9b09f47d2f31754e (patch)
treeda7dbef28267cdf8785670754cf6638e1cd59053 /challenge-158
parent94c76f89337d76da7cb27601d2b02a5553462bf9 (diff)
downloadperlweeklychallenge-club-9dd5c70e46bc2d018e505e7f9b09f47d2f31754e.tar.gz
perlweeklychallenge-club-9dd5c70e46bc2d018e505e7f9b09f47d2f31754e.tar.bz2
perlweeklychallenge-club-9dd5c70e46bc2d018e505e7f9b09f47d2f31754e.zip
Add Perl solution to challenge 158
Diffstat (limited to 'challenge-158')
-rw-r--r--challenge-158/paulo-custodio/perl/ch-1.pl33
-rw-r--r--challenge-158/paulo-custodio/perl/ch-2.pl34
-rw-r--r--challenge-158/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-158/paulo-custodio/t/test-2.yaml5
4 files changed, 77 insertions, 0 deletions
diff --git a/challenge-158/paulo-custodio/perl/ch-1.pl b/challenge-158/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..1c23f8c0ff
--- /dev/null
+++ b/challenge-158/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+# Challenge 158
+#
+# TASK #1 › Additive Primes
+# Submitted by: Mohammad S Anwar
+# Write a script to find out all Additive Primes <= 100.
+#
+# Additive primes are prime numbers for which the sum of their decimal digits
+# are also primes.
+#
+#
+# Output
+# 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89
+
+use Modern::Perl;
+use List::Util qw( sum );
+use ntheory qw( is_prime next_prime );
+
+say join(", ", additive_primes(100));
+
+sub additive_primes {
+ my($limit) = @_;
+ my @out;
+ my $prime = 2;
+ while ($prime < $limit) {
+ if (is_prime(sum(split(//, $prime)))) {
+ push @out, $prime;
+ }
+ $prime = next_prime($prime);
+ }
+ return @out;
+}
diff --git a/challenge-158/paulo-custodio/perl/ch-2.pl b/challenge-158/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..8f28f63531
--- /dev/null
+++ b/challenge-158/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+# Challenge 158
+#
+# TASK #2 › First Series Cuban Primes
+# Submitted by: Mohammad S Anwar
+# Write a script to compute first series Cuban Primes <= 1000. Please refer
+# wikipedia page for more informations.
+#
+# Output
+# 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919.
+#
+# p=(x^3-y^3)/(x-y), x=y+1, y>0
+# <=> p=3y^2+3y+1, y>0
+
+use Modern::Perl;
+use ntheory qw( is_prime );
+
+say join(", ", cuban_primes(1000));
+
+sub cuban_primes {
+ my($limit) = @_;
+ my @out;
+ my $y = 1;
+ my $p;
+ do {
+ $p = 3*$y*$y+3*$y+1;
+ if (is_prime($p)) {
+ push @out, $p;
+ }
+ $y++;
+ } while ($p < $limit);
+ return @out;
+}
diff --git a/challenge-158/paulo-custodio/t/test-1.yaml b/challenge-158/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..245c490895
--- /dev/null
+++ b/challenge-158/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89
diff --git a/challenge-158/paulo-custodio/t/test-2.yaml b/challenge-158/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..19b7f5e476
--- /dev/null
+++ b/challenge-158/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919