summaryrefslogtreecommitdiff
path: root/022/ch1.pl
diff options
context:
space:
mode:
authorGuillermo Ramos2019-08-24 22:14:28 +0200
committerGuillermo Ramos2019-08-24 22:14:28 +0200
commit7be27c7356fa782c83b1be2e04fd5586842e4333 (patch)
tree84965894e180380a8f9f0051cabf12b8c8a445c2 /022/ch1.pl
parent5ebb802fbeaf78da370e1026cffc3d129d0be8e2 (diff)
downloadperlweekly-7be27c7356fa782c83b1be2e04fd5586842e4333.tar.gz
[022#1]
Diffstat (limited to '022/ch1.pl')
-rwxr-xr-x022/ch1.pl34
1 files changed, 34 insertions, 0 deletions
diff --git a/022/ch1.pl b/022/ch1.pl
new file mode 100755
index 0000000..f6b2de4
--- /dev/null
+++ b/022/ch1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+#
+# Write a script to print first 10 Sexy Prime Pairs. Sexy primes are prime
+# numbers that differ from each other by 6. For example, the numbers 5 and 11
+# are both sexy primes, because 11 - 5 = 6. The term “sexy prime” is a pun
+# stemming from the Latin word for six: sex. For more information, please
+# checkout wiki page.
+#
+# (https://en.wikipedia.org/wiki/Sexy_prime).
+################################################################################
+
+use strict;
+use warnings;
+
+use List::Util qw<any>;
+
+my $MAX_SEXY = 10;
+
+my @primes = ();
+my @sexy;
+my $p = 2;
+
+while (@sexy < $MAX_SEXY*2) {
+ unless (any { $p % $_ == 0 } @primes) {
+ push @primes, $p;
+ push @sexy, $primes[-2], $primes[-1]
+ if @primes > 1 && $primes[-1] == $primes[-2] + 6;
+ }
+ $p++;
+}
+
+foreach my $i (0 .. @sexy/2-1) {
+ printf "%s-%s\n", $sexy[$i*2], $sexy[$i*2+1];
+}