aboutsummaryrefslogtreecommitdiff
path: root/challenge-041
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-06 16:36:51 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-06 16:36:51 +0100
commit9b682c1fd7e5aac0580b5e22838696c4f7f4b33d (patch)
tree4c6d7ac09a1a28a249055cd0a1cb7fd89cfc4360 /challenge-041
parent22423b96aef59834ce9f678f5fce1f1b40fd4b66 (diff)
downloadperlweeklychallenge-club-9b682c1fd7e5aac0580b5e22838696c4f7f4b33d.tar.gz
perlweeklychallenge-club-9b682c1fd7e5aac0580b5e22838696c4f7f4b33d.tar.bz2
perlweeklychallenge-club-9b682c1fd7e5aac0580b5e22838696c4f7f4b33d.zip
Add perl solution to challenge 041
Diffstat (limited to 'challenge-041')
-rw-r--r--challenge-041/paulo-custodio/Makefile2
-rw-r--r--challenge-041/paulo-custodio/README1
-rw-r--r--challenge-041/paulo-custodio/perl/ch-1.pl23
-rw-r--r--challenge-041/paulo-custodio/perl/ch-2.pl33
-rw-r--r--challenge-041/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-041/paulo-custodio/t/test-2.yaml5
6 files changed, 69 insertions, 0 deletions
diff --git a/challenge-041/paulo-custodio/Makefile b/challenge-041/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-041/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-041/paulo-custodio/README b/challenge-041/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-041/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-041/paulo-custodio/perl/ch-1.pl b/challenge-041/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..0c8f0c5420
--- /dev/null
+++ b/challenge-041/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+# Challenge 041
+#
+# TASK #1
+# Write a script to display attractive number between 1 and 50.
+# A number is an attractive number if the number of its prime factors is also
+# prime number.
+#
+# The number 20 is an attractive number, whose prime factors are 2, 2 and 5.
+# The total prime factors is 3 which is also a prime number.
+
+use Modern::Perl;
+use ntheory qw( is_prime factor );
+
+my @attractive = grep {is_attractive($_)} 1..50;
+say join(", ", @attractive);
+
+sub is_attractive {
+ my($n) = @_;
+ my @factors = factor($n);
+ return is_prime(scalar(@factors));
+}
diff --git a/challenge-041/paulo-custodio/perl/ch-2.pl b/challenge-041/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..3d4e3e33fe
--- /dev/null
+++ b/challenge-041/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+# Challenge 041
+#
+# TASK #2
+# Write a script to display first 20 Leonardo Numbers. Please checkout wiki
+# page for more information.
+# For example:
+#
+# L(0) = 1
+# L(1) = 1
+# L(2) = L(0) + L(1) + 1 = 3
+# L(3) = L(1) + L(2) + 1 = 5
+# and so on.
+
+use Modern::Perl;
+
+my @out;
+for (0..19) {
+ push @out, leonardo($_);
+}
+say join(", ", @out);
+
+
+sub leonardo {
+ my($n) = @_;
+ if ($n < 2) {
+ return 1;
+ }
+ else {
+ return leonardo($n-1)+leonardo($n-2)+1;
+ }
+}
diff --git a/challenge-041/paulo-custodio/t/test-1.yaml b/challenge-041/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..4d78f36c48
--- /dev/null
+++ b/challenge-041/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 4, 6, 8, 9, 10, 12, 14, 15, 18, 20, 21, 22, 25, 26, 27, 28, 30, 32, 33, 34, 35, 38, 39, 42, 44, 45, 46, 48, 49, 50
diff --git a/challenge-041/paulo-custodio/t/test-2.yaml b/challenge-041/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..a7af00752b
--- /dev/null
+++ b/challenge-041/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529