forked from trizen/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime_counting_liouville_formula.pl
More file actions
77 lines (54 loc) · 1.44 KB
/
prime_counting_liouville_formula.pl
File metadata and controls
77 lines (54 loc) · 1.44 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
#!/usr/bin/perl
# Author: Trizen
# Date: 17 July 2025
# https://github.com/trizen
# A sublinear algorithm for computing the Prime Counting function `pi(n)`,
# based on the Liouville function and the number of k-almost primes <= n, for `k >= 2`.
# See also:
# https://mathworld.wolfram.com/AlmostPrime.html
use 5.036;
use ntheory qw(:all);
sub k_prime_count ($k, $n) {
if ($k == 1) {
return my_prime_count($n);
}
my $count = 0;
sub ($m, $p, $k, $j = 0) {
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__->($m * $q, $q, $k - 1, $j++);
}
}->(1, 2, $k);
return $count;
}
sub my_prime_count ($n) {
state $pi_table = [0, 0, 1, 2, 2]; # a larger lookup table helps a lot!
if ($n < 0) {
return 0;
}
if (defined($pi_table->[$n])) {
return $pi_table->[$n];
}
my $M = sumliouville($n);
foreach my $k (2 .. logint($n, 2)) {
$M -= (-1)**$k * k_prime_count($k, $n);
}
return ($pi_table->[$n] //= 1 - $M);
}
foreach my $n (1..7) { # takes ~3 seconds
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