/
sieve.pl
48 lines (37 loc) · 945 Bytes
/
sieve.pl
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
#!/usr/bin/perl -w
# 25 January 2011 Lenna Xiao Ping Peterson
#
# sample runtimes
# 100000: 0.066562
# 1000000: 0.84392
# 10000000: 9.443371
# 100000000: 104.505623
#
use Time::HiRes ("gettimeofday", "tv_interval");
$start = [gettimeofday];
#print "start: $start\n";
$maximum = 100000000;
for $i ( 0 .. $maximum ) {
$numbers[$i] = 0; # fill array with 0 (prime until proven composite)
}
$numbers[0] = -1;
$numbers[1] = -1;
use POSIX;
$stop = ceil(sqrt($maximum));
for $n ( 2 .. $stop ) {
if ( $numbers[$n] == 0 ) { # first loop: 2 is prime
$composite = 2*$n; # thus 4 cannot be prime
while ($composite <= $maximum) { # set all multiples of 2 composite
$numbers[$composite] = -1;
$composite += $n;
}
}
}
for $j ( 2 .. $#numbers ) {
if ( $numbers[$j] == 0 ) { # if the element is still 0, it's prime
push @primes, $j;
}
}
#print "@primes\n";
$clock = tv_interval($start);
print "$maximum: $clock\n";