diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2023-01-03 13:51:05 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2023-01-03 13:51:05 -0500 |
| commit | 3936477f4d102343c9f21cde1cc99e5e6d113d19 (patch) | |
| tree | 388b3cbad9c6f33e6c5d6555b6982f8e4aa16164 /challenge-198/dave-jacoby/perl/ch-2.pl | |
| parent | ee249218f373166edca2b95144a9b0b59e200e05 (diff) | |
| download | perlweeklychallenge-club-3936477f4d102343c9f21cde1cc99e5e6d113d19.tar.gz perlweeklychallenge-club-3936477f4d102343c9f21cde1cc99e5e6d113d19.tar.bz2 perlweeklychallenge-club-3936477f4d102343c9f21cde1cc99e5e6d113d19.zip | |
DAJ 198
Diffstat (limited to 'challenge-198/dave-jacoby/perl/ch-2.pl')
| -rw-r--r-- | challenge-198/dave-jacoby/perl/ch-2.pl | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-198/dave-jacoby/perl/ch-2.pl b/challenge-198/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..044a2f68a4 --- /dev/null +++ b/challenge-198/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; +use Algorithm::Permute; + +my @examples = ( 10, 15, 1, 25 ); + +for my $e (@examples) { + my $out = prime_count($e); + say <<"END"; + Input: \$n = $e + Output: $out +END +} + +# 1 is not a prime, but 2 is, but the regex in is_prime accepts 1, so +# we start on 2 and go to the given max +# grep ( is_prime($_) ) gives only the list of primes +# scalar gives us the count, which is what we want +sub prime_count ( $e ) { + return scalar grep { is_prime($_) } 2 .. $e; +} + +# https://catonmat.net/perl-regex-that-matches-composite-numbers +sub is_prime ( $n ) { ( '1' x $n ) !~ /\A(11+?)\1+\z/ } |
