aboutsummaryrefslogtreecommitdiff
path: root/challenge-198/athanasius/perl/ch-2.pl
blob: 06e53e858e86d549ec3f11cc3bcebe9b9a08d140 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!perl

###############################################################################
=comment

Perl Weekly Challenge 198
=========================

TASK #2
-------
*Prime Count*

Submitted by: Mohammad S Anwar

You are given an integer $n > 0.

Write a script to print the count of primes less than $n.

Example 1

  Input: $n = 10
  Output: 4 as in there are 4 primes less than 10 are 2, 3, 5, 7.

Example 2

  Input: $n = 15
  Output: 6

Example 3

  Input: $n = 1
  Output: 0

Example 4

  Input: $n = 25
  Output: 9

=cut
###############################################################################

#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#

#==============================================================================
=comment

Interface
---------
If no command-line argument is given, the test suite is run.

=cut
#==============================================================================

use strict;
use warnings;
use Const::Fast;
use ntheory        qw( prime_count );
use Regexp::Common qw( number );
use Test::More;

const my $TEST_FIELDS => 3;
const my $USAGE       =>
"Usage:
  perl $0 [<list> ...]
  perl $0

    [<list> ...]    An integer greater than zero\n";

#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 198, Task #2: Prime Count (Perl)\n\n";
}

#==============================================================================
MAIN:
#==============================================================================
{
    my $args = scalar @ARGV;

    if    ($args == 0)
    {
        run_tests();
    }
    elsif ($args == 1)
    {
        my $n = $ARGV[ 0 ];

        $n =~ / ^ $RE{num}{int} $ /x
               or error( qq["$_" is not a valid integer] );

        $n > 0 or error( "$n is not greater than zero" );

        print  "Input:  \$n = $n\n";
        printf "Output: %d\n", count_primes( $n );
    }
    else
    {
        error( "Expected 1 or 0 command line arguments, found $args" );
    }
}

#------------------------------------------------------------------------------
sub count_primes
#------------------------------------------------------------------------------
{
    my ($n) = @_;

    # Note: The count returned by Math::Prime::Util::prime_count( $x ) is
    #       *inclusive* of $x

    return prime_count( $n - 1 );
}

#------------------------------------------------------------------------------
sub error
#------------------------------------------------------------------------------
{
    my ($message) = @_;

    die "ERROR: $message\n$USAGE";
}

#------------------------------------------------------------------------------
sub run_tests
#------------------------------------------------------------------------------
{
    print "Running the test suite\n";

    while (my $line = <DATA>)
    {
        chomp $line;

        my ($test_name, $n, $expected) = split / \| /x, $line, $TEST_FIELDS;

        s/ ^ \s* (.+?) \s* $ /$1/x                            # Trim whitespace
            for $test_name, $n, $expected;

        is count_primes( $n ), $expected, $test_name;
    }

    done_testing;
}

###############################################################################

__DATA__
Example 1|        10|       4
Example 2|        15|       6
Example 3|         1|       0
Example 4|        25|       9
Large n  |   1000000|   78498
Huge n   |1234567890|62106578