/
ch-1.raku
186 lines (138 loc) · 4.46 KB
/
ch-1.raku
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use v6d;
################################################################################
=begin comment
Perl Weekly Challenge 223
=========================
TASK #1
-------
*Count Primes*
Submitted by: Mohammad S Anwar
You are given a positive integer, $n.
Write a script to find the total count of primes less than or equal to the given
integer.
Example 1
Input: $n = 10
Output: 4
Since there are 4 primes (2,3,5,7) less than or equal to 10.
Example 2
Input: $n = 1
Output: 0
Example 3
Input: $n = 20
Output: 8
Since there are 8 primes (2,3,5,7,11,13,17,19) less than or equal to 20.
=end comment
################################################################################
#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=begin comment
Note
----
This is a repeat of Week 198, Task #2: "Prime Count", except that "less than $n"
has been changed to "less than or equal to" $n.
Interface
---------
If no command-line arguments are given, the test suite is run.
=end comment
#===============================================================================
use Test;
subset Pos of Int where * > 0;
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
"\nChallenge 223, Task #1: Count Primes (Raku)\n".put;
}
#===============================================================================
multi sub MAIN
(
Pos:D $n #= A positive integer
)
#===============================================================================
{
"Input: \$n = $n".put;
my UInt $primes = count-primes( $n );
"Output: $primes".put;
}
#===============================================================================
multi sub MAIN() # No input: run the test suite
#===============================================================================
{
run-tests();
}
#------------------------------------------------------------------------------
sub count-primes( UInt:D $n where { $n > 0 } --> UInt:D )
#------------------------------------------------------------------------------
{
my UInt $count = 0;
my Bool @sieve = |(False xx 2), |(True xx ($n - 2));
my UInt $sqrt-n = $n.sqrt.floor;
for 2 .. $sqrt-n -> UInt $i
{
if @sieve[ $i ]
{
++$count;
for 0 .. * -> UInt $j
{
my UInt $k = $i² + $j * $i;
last if $k >= $n;
@sieve[ $k ] = False;
}
}
}
for $sqrt-n + 1 .. $n - 1 -> UInt $m
{
++$count if @sieve[ $m ];
}
return $count;
}
#-------------------------------------------------------------------------------
sub run-tests()
#-------------------------------------------------------------------------------
{
'Running the test suite'.put;
for test-data.lines -> Str $line
{
my Str ($test-name, $n, $expected) = $line.split: / \| /;
for $test-name, $n, $expected # Trim whitespace
{
s/ ^ \s+ //;
s/ \s+ $ //;
}
my UInt $primes = count-primes( $n.Int );
is $primes, $expected.Int, $test-name;
}
done-testing;
}
#-------------------------------------------------------------------------------
sub error( Str:D $message )
#-------------------------------------------------------------------------------
{
"ERROR: $message".put;
USAGE();
exit 0;
}
#-------------------------------------------------------------------------------
sub USAGE()
#-------------------------------------------------------------------------------
{
my Str $usage = $*USAGE;
$usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
$usage.put;
}
#-------------------------------------------------------------------------------
sub test-data( --> Str:D )
#-------------------------------------------------------------------------------
{
return q:to/END/;
Week 223, Example 1| 10| 4
Week 223, Example 2| 1| 0
Week 223, Example 3| 20| 8
Week 198, Example 2| 15| 6
Weel 198, Example 4| 25| 9
Large n |1000000|78498
END
}
################################################################################