diff options
Diffstat (limited to '022/ch1.pl')
-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]; +} |