summaryrefslogtreecommitdiff
path: root/022/ch1.pl
blob: f6b2de4b1d90ea0455172b6a66c9fb2bdd32a942 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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];
}