aboutsummaryrefslogtreecommitdiff
path: root/challenge-158/e-choroba/perl/ch-2.pl
blob: b64a65a9ebd2877ae16afb05d53b8627dd651af4 (plain)
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
#!/usr/bin/perl
use warnings;
use strict;

use experimental 'signatures';
use Math::Prime::Util qw{ is_prime };

sub cuban_primes ($n) {
    my @c;
    my $y = 0;
    while (1) {
        # centered hexagonal number
        my $chn = 3 * $y * $y + 3 * $y + 1;
        last if $chn > $n;

        push @c, $chn if is_prime($chn);
        ++$y;
    }
    return \@c
}

use Test::More tests => 1;

is_deeply cuban_primes(1000),
    [7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919],
    'Example';