-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbitstring_prime_sieve_mpz.pl
More file actions
executable file
·46 lines (34 loc) · 1.01 KB
/
bitstring_prime_sieve_mpz.pl
File metadata and controls
executable file
·46 lines (34 loc) · 1.01 KB
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 09 July 2017
# https://github.com/trizen
# A decently fast bit-string sieve for prime numbers.
# It's asymptotically faster than using Perl's arrays.
# Also useful when memory is very restricted.
use 5.010;
use strict;
use warnings;
use Math::GMPz;
sub bitstring_prime_sieve {
my ($n) = @_;
my $c = Math::GMPz::Rmpz_init_set_ui(1);
Math::GMPz::Rmpz_setbit($c, $n + 1);
my $bound = int(sqrt($n));
for (my $i = 3 ; $i <= $bound ; $i += 2) {
if (!Math::GMPz::Rmpz_tstbit($c, $i)) {
for (my $j = $i * $i ; $j <= $n ; $j += $i << 1) {
Math::GMPz::Rmpz_setbit($c, $j);
}
}
}
my @primes = (2);
foreach my $k (1 .. ($n - 1) >> 1) {
Math::GMPz::Rmpz_tstbit($c, ($k << 1) + 1) || push(@primes, ($k << 1) + 1);
}
return @primes;
}
my $n = shift(@ARGV) // 100;
my @primes = bitstring_prime_sieve($n);
say join(' ', @primes);
say "PI($n) = ", scalar(@primes);