-
Notifications
You must be signed in to change notification settings - Fork 0
/
phi(k)_divides_k-3.pl
70 lines (47 loc) · 1.36 KB
/
phi(k)_divides_k-3.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
#!/usr/bin/perl
# Numbers k where phi(k) divides k - 3.
# https://oeis.org/A350777
# Known terms:
# 1, 2, 3, 9, 195, 5187
# No term larger than 5187 is known...
use 5.020;
use strict;
use warnings;
use Math::GMPz;
use ntheory qw(:all);
use Math::Prime::Util::GMP;
use experimental qw(signatures);
use POSIX qw(ULONG_MAX);
eval { require GDBM_File };
my $cache_db = "cache/factors.db";
dbmopen(my %db, $cache_db, 0444)
or die "Can't create/access database <<$cache_db>>: $!";
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;
}
my $t = Math::GMPz::Rmpz_init();
while (my ($key, $value) = each %db) {
#next if length($key) > 100;
my @factors = split(' ', $value);
next if $factors[-1] >= ULONG_MAX;
my $phi = my_euler_phi(\@factors);
Math::GMPz::Rmpz_set_str($t, $key, 10);
Math::GMPz::Rmpz_sub_ui($t, $t, 3);
if (Math::GMPz::Rmpz_divisible_p($t, $phi)) {
say $key;
}
}
dbmclose(%db);