-
Notifications
You must be signed in to change notification settings - Fork 34
/
omega_prime_numbers_in_range.pl
74 lines (51 loc) · 1.78 KB
/
omega_prime_numbers_in_range.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 14 March 2021
# https://github.com/trizen
# Generate k-omega primes in range [a,b]. (not in sorted order)
# 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 ntheory qw(:all);
use experimental qw(signatures);
sub omega_prime_numbers ($A, $B, $k, $callback) {
$A = vecmax($A, pn_primorial($k));
sub ($m, $p, $k) {
my $s = rootint(divint($B, $m), $k);
foreach my $q (@{primes($p, $s)}) {
my $r = next_prime($q);
for (my $v = mulint($m, $q); $v <= $B ; $v = mulint($v, $q)) {
if ($k == 1) {
$callback->($v) if ($v >= $A);
}
else {
if (mulint($v, $r) <= $B) {
__SUB__->($v, $r, $k - 1);
}
}
}
}
}->(1, 2, $k);
}
# Generate 5-omega primes in the range [3000, 10000]
my $k = 5;
my $from = 3000;
my $upto = 10000;
my @arr;
omega_prime_numbers($from, $upto, $k, sub ($n) { push @arr, $n });
my @test = grep { prime_omega($_) == $k } $from .. $upto; # just for testing
join(' ', sort { $a <=> $b } @arr) eq join(' ', @test) or die "Error: not equal!";
say join(', ', @arr);
# Run some tests
foreach my $k (1 .. 6) {
my $from = pn_primorial($k) + int(rand(1e4));
my $upto = $from + int(rand(1e5));
say "Testing: $k with $from .. $upto";
my @arr;
omega_prime_numbers($from, $upto, $k, sub ($n) { push @arr, $n });
my @test = grep { prime_omega($_) == $k } $from .. $upto;
join(' ', sort { $a <=> $b } @arr) eq join(' ', @test) or die "Error: not equal!";
}