-
Notifications
You must be signed in to change notification settings - Fork 34
/
count_of_k-omega_primes.pl
114 lines (77 loc) · 2.82 KB
/
count_of_k-omega_primes.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
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 14 March 2021
# https://github.com/trizen
# Count the number of k-omega primes <= n.
# Definition:
# k-omega primes are numbers n such that omega(n) = k.
# See also:
# https://en.wikipedia.org/wiki/Almost_prime
# https://en.wikipedia.org/wiki/Prime_omega_function
use 5.020;
use warnings;
use ntheory qw(:all);
use experimental qw(signatures);
sub omega_prime_count_rec ($n, $k = 1) {
if ($k == 1) {
return prime_power_count($n);
}
my $count = 0;
sub ($m, $p, $k, $s = rootint(divint($n, $m), $k), $j = 1) {
if ($k == 2) {
for (; $p <= $s ; ++$j) {
my $r = next_prime($p);
for (my $t = mulint($m, $p) ; $t <= $n ; $t = mulint($t, $p)) {
my $w = divint($n, $t);
if ($r > $w) {
last;
}
$count += prime_count($w) - $j;
for (my $r2 = $r ; $r2 <= $w ; $r2 = next_prime($r2)) {
my $u = vecprod($t, $r2, $r2);
if ($u > $n) {
last;
}
for (; $u <= $n ; $u = mulint($u, $r2)) {
++$count;
}
}
}
$p = $r;
}
return;
}
for (; $p <= $s ; ++$j) {
my $r = next_prime($p);
for (my $t = mulint($m, $p) ; $t <= $n ; $t = mulint($t, $p)) {
my $s = rootint(divint($n, $t), $k - 1);
last if ($r > $s);
__SUB__->($t, $r, $k - 1, $s, $j + 1);
}
$p = $r;
}
}->(1, 2, $k);
return $count;
}
# Run some tests
foreach my $k (1 .. 10) {
my $upto = pn_primorial($k) + int(rand(1e5));
my $x = omega_prime_count_rec($upto, $k);
my $y = omega_prime_count($k, $upto);
say "Testing: $k with n = $upto -> $x";
$x == $y
or die "Error: $x != $y";
}
say '';
foreach my $k (1 .. 8) {
say("Count of $k-omega primes for 10^n: ", join(', ', map { omega_prime_count_rec(10**$_, $k) } 0 .. 8));
}
__END__
Count of 1-omega primes for 10^n: 0, 7, 35, 193, 1280, 9700, 78734, 665134, 5762859
Count of 2-omega primes for 10^n: 0, 2, 56, 508, 4097, 33759, 288726, 2536838, 22724609
Count of 3-omega primes for 10^n: 0, 0, 8, 275, 3695, 38844, 379720, 3642766, 34800362
Count of 4-omega primes for 10^n: 0, 0, 0, 23, 894, 15855, 208034, 2389433, 25789580
Count of 5-omega primes for 10^n: 0, 0, 0, 0, 33, 1816, 42492, 691209, 9351293
Count of 6-omega primes for 10^n: 0, 0, 0, 0, 0, 25, 2285, 72902, 1490458
Count of 7-omega primes for 10^n: 0, 0, 0, 0, 0, 0, 8, 1716, 80119
Count of 8-omega primes for 10^n: 0, 0, 0, 0, 0, 0, 0, 1, 719