aboutsummaryrefslogtreecommitdiff
path: root/challenge-015/jo-37/perl/ch-1.pl
blob: cf2b43f61524cfb95fcfd0ac7b5383c7ca164847 (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
27
28
29
30
31
32
33
34
#!/usr/bin/perl -s

use v5.16;
use warnings;
use List::Gen;
use Math::Prime::Util qw(prev_prime next_prime);
use experimental 'signatures';

our $weak;

die <<EOS unless @ARGV == 1;
usage: $0 [-weak] N

-weak
    print "weak" primes instead of "strong" ones

N
    print the first N primes from the selected category

EOS


### Input and Output

gen_sw_primes(!$weak)->say(shift);


### Implementation

sub gen_sw_primes ($strong) {
    iterate(sub {next_prime($_)})
    ->from(3)
    ->filter(sub {!$strong ^ ($_ > (prev_prime($_) + next_prime($_)) / 2)});	
}