-
Notifications
You must be signed in to change notification settings - Fork 34
/
omega_prime_numbers_in_range_mpz.pl
92 lines (64 loc) · 2.16 KB
/
omega_prime_numbers_in_range_mpz.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 14 March 2021
# Edit: 04 April 2024
# https://github.com/trizen
# Generate all the k-omega primes in range [A,B].
# 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.036;
use Math::GMPz;
use ntheory qw(:all);
sub omega_prime_numbers ($A, $B, $k) {
$A = vecmax($A, pn_primorial($k));
$A = Math::GMPz->new("$A");
$B = Math::GMPz->new("$B");
my $u = Math::GMPz::Rmpz_init();
my @values = sub ($m, $lo, $j) {
Math::GMPz::Rmpz_tdiv_q($u, $B, $m);
Math::GMPz::Rmpz_root($u, $u, $j);
my $hi = Math::GMPz::Rmpz_get_ui($u);
if ($lo > $hi) {
return;
}
my @lst;
my $v = Math::GMPz::Rmpz_init();
foreach my $q (@{primes($lo, $hi)}) {
Math::GMPz::Rmpz_mul_ui($v, $m, $q);
while (Math::GMPz::Rmpz_cmp($v, $B) <= 0) {
if ($j == 1) {
if (Math::GMPz::Rmpz_cmp($v, $A) >= 0) {
push @lst, Math::GMPz::Rmpz_init_set($v);
}
}
else {
push @lst, __SUB__->($v, $q + 1, $j - 1);
}
Math::GMPz::Rmpz_mul_ui($v, $v, $q);
}
}
return @lst;
}
->(Math::GMPz->new(1), 2, $k);
sort { Math::GMPz::Rmpz_cmp($a, $b) } @values;
}
# 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);
my @test = grep { prime_omega($_) == $k } $from .. $upto; # just for testing
join(' ', @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);
my @test = grep { prime_omega($_) == $k } $from .. $upto;
join(' ', @arr) eq join(' ', @test) or die "Error: not equal!";
}