-
Notifications
You must be signed in to change notification settings - Fork 34
/
lucas-carmichael_numbers_from_multiple_mpz.pl
51 lines (34 loc) · 1.31 KB
/
lucas-carmichael_numbers_from_multiple_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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 08 March 2023
# https://github.com/trizen
# Generate Lucas-Carmichael numbers from a given multiple.
# See also:
# https://trizenx.blogspot.com/2020/08/pseudoprimes-construction-methods-and.html
use 5.036;
use Math::GMPz;
use ntheory qw(:all);
sub lucas_carmichael_from_multiple ($m, $callback) {
my $t = Math::GMPz::Rmpz_init();
my $u = Math::GMPz::Rmpz_init();
my $v = Math::GMPz::Rmpz_init();
is_square_free($m) || return;
my $L = lcm(map { addint($_, 1) } factor($m));
$m = Math::GMPz->new("$m");
$L = Math::GMPz->new("$L");
Math::GMPz::Rmpz_invert($v, $m, $L) || return;
Math::GMPz::Rmpz_sub($v, $L, $v);
for (my $p = Math::GMPz::Rmpz_init_set($v) ; ; Math::GMPz::Rmpz_add($p, $p, $L)) {
Math::GMPz::Rmpz_gcd($t, $m, $p);
Math::GMPz::Rmpz_cmp_ui($t, 1) == 0 or next;
my @factors = factor_exp($p);
(vecall { $_->[1] == 1 } @factors) || next;
Math::GMPz::Rmpz_mul($v, $m, $p);
Math::GMPz::Rmpz_add_ui($u, $v, 1);
Math::GMPz::Rmpz_set_str($t, lcm(map { addint($_->[0], 1) } @factors), 10);
if (Math::GMPz::Rmpz_divisible_p($u, $t)) {
$callback->(Math::GMPz::Rmpz_init_set($v));
}
}
}
lucas_carmichael_from_multiple(11 * 17, sub ($n) { say $n });