diff options
author | Guillermo Ramos | 2019-08-24 22:14:28 +0200 |
---|---|---|
committer | Guillermo Ramos | 2019-08-24 22:14:28 +0200 |
commit | 7be27c7356fa782c83b1be2e04fd5586842e4333 (patch) | |
tree | 84965894e180380a8f9f0051cabf12b8c8a445c2 /022 | |
parent | 5ebb802fbeaf78da370e1026cffc3d129d0be8e2 (diff) | |
download | perlweekly-7be27c7356fa782c83b1be2e04fd5586842e4333.tar.gz |
[022#1]
Diffstat (limited to '022')
-rwxr-xr-x | 022/ch1.pl | 34 |
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]; +} |