-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarmichael_with_gcd_phi_lambda_cached.pl
90 lines (67 loc) · 2.15 KB
/
carmichael_with_gcd_phi_lambda_cached.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
#!/usr/bin/perl
# Carmichael numbers n such that gcd(n-1, phi(n))**2 / lambda(n)**2 >= n-1.
# Problem from:
# https://math.stackexchange.com/questions/4157474/carmichael-number-n-such-that-frac-gcdn-1-phin2-lambdan2-geq-n
use 5.020;
use strict;
use warnings;
use Storable;
use Math::GMPz;
use ntheory qw(:all);
use POSIX qw(ULONG_MAX);
#use Math::Sidef qw(is_fibonacci);
#use Math::Prime::Util::GMP;
use experimental qw(signatures);
#use List::Util qw(uniq);
my $carmichael_file = "cache/factors-carmichael.storable";
my $carmichael = retrieve($carmichael_file);
sub my_euler_phi ($factors) { # assumes n is squarefree
state $t = Math::GMPz::Rmpz_init();
state $u = Math::GMPz::Rmpz_init();
Math::GMPz::Rmpz_set_ui($t, 1);
foreach my $p (@$factors) {
if ($p < ULONG_MAX) {
Math::GMPz::Rmpz_mul_ui($t, $t, $p - 1);
}
else {
Math::GMPz::Rmpz_set_str($u, "$p", 10);
Math::GMPz::Rmpz_sub_ui($u, $u, 1);
Math::GMPz::Rmpz_mul($t, $t, $u);
}
}
return $t;
}
sub my_carmichael_lambda ($factors) { # assumes n is squarefree
state $t = Math::GMPz::Rmpz_init();
state $u = Math::GMPz::Rmpz_init();
Math::GMPz::Rmpz_set_ui($t, 1);
foreach my $p (@$factors) {
if ($p < ULONG_MAX) {
Math::GMPz::Rmpz_lcm_ui($t, $t, $p - 1);
}
else {
Math::GMPz::Rmpz_set_str($u, "$p", 10);
Math::GMPz::Rmpz_sub_ui($u, $u, 1);
Math::GMPz::Rmpz_lcm($t, $t, $u);
}
}
return $t;
}
my $u = Math::GMPz::Rmpz_init_set_ui(0);
my $t = Math::GMPz::Rmpz_init_set_ui(0);
while(my($key, $value) = each %$carmichael) {
my @factors = split(' ', $value);
my $phi = my_euler_phi(\@factors);
my $lambda = my_euler_phi(\@factors);
Math::GMPz::Rmpz_set_str($u, $key, 10);
Math::GMPz::Rmpz_sub_ui($u, $u, 1);
Math::GMPz::Rmpz_gcd($t, $u, $phi);
Math::GMPz::Rmpz_mul($t, $t, $t);
Math::GMPz::Rmpz_mul($lambda, $lambda, $lambda);
Math::GMPz::Rmpz_divexact($t, $t, $lambda);
if (Math::GMPz::Rmpz_cmp($t, $u) >= 0) {
say $key;
}
}
__END__
# No such number is known...