forked from trizen/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime_counting_from_squarefree_almost_primes.pl
More file actions
89 lines (65 loc) · 1.73 KB
/
prime_counting_from_squarefree_almost_primes.pl
File metadata and controls
89 lines (65 loc) · 1.73 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
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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# Date: 27 August 2025
# https://github.com/trizen
# A sublinear algorithm for computing the Prime Counting function `pi(n)`,
# based on the number of squarefree k-almost primes <= n, for `k >= 2`, which can be computed in sublinear time.
# See also:
# https://mathworld.wolfram.com/AlmostPrime.html
use 5.036;
use ntheory qw(:all);
sub squarefree_almost_prime_count ($k, $n) {
if ($k == 0) {
return (($n <= 0) ? 0 : 1);
}
if ($k == 1) {
return my_prime_count($n);
}
my $count = 0;
sub ($m, $p, $k, $j = 1) {
my $s = rootint(divint($n, $m), $k);
if ($k == 2) {
forprimes {
$count += my_prime_count(divint($n, mulint($m, $_))) - $j++;
}
$p, $s;
return;
}
foreach my $q (@{primes($p, $s)}) {
__SUB__->(mulint($m, $q), $q + 1, $k - 1, ++$j);
}
}
->(1, 2, $k);
return $count;
}
sub my_prime_count ($n) {
state %cache = ( # a larger lookup table helps a lot!
0 => 0,
1 => 0,
2 => 1,
3 => 2,
4 => 2,
);
if ($n < 0) {
return 0;
}
if (exists $cache{$n}) {
return $cache{$n};
}
my $M = powerfree_count($n, 2) - 1;
foreach my $k (2 .. exp(LambertW(log($n))) + 1) {
$M -= squarefree_almost_prime_count($k, $n);
}
$cache{$n} //= $M;
}
foreach my $n (1 .. 7) { # takes ~1 second
say "pi(10^$n) = ", my_prime_count(10**$n);
}
__END__
pi(10^1) = 4
pi(10^2) = 25
pi(10^3) = 168
pi(10^4) = 1229
pi(10^5) = 9592
pi(10^6) = 78498
pi(10^7) = 664579