-
Notifications
You must be signed in to change notification settings - Fork 0
/
carmichael.pl
50 lines (36 loc) · 1.05 KB
/
carmichael.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
#!/usr/bin/perl
use 5.020;
use autodie;
use warnings;
use Math::GMPz;
use Storable;
use ntheory qw(:all);
use List::Util qw(uniq);
use experimental qw(signatures);
eval { require GDBM_File };
my $cache_db = "/home/swampyx/Other/Programare/experimental-projects/pseudoprimes/programs/cache/factors.db";
dbmopen(my %cache_db, $cache_db, 0444)
or die "Can't create/access database <<$cache_db>>: $!";
sub my_is_carmichael_fast ($n, $factors) {
my $nm1 = Math::Prime::Util::GMP::subint($n, 1);
return if not vecall {
Math::Prime::Util::GMP::modint($nm1, ($_ < ~0) ? ($_-1) : Math::Prime::Util::GMP::subint($_, 1)) eq '0'
} @$factors;
scalar(uniq(@$factors)) == scalar(@$factors);
}
#while (my ($key, $value) = each %cache_db) {
while (<>) {
/\S/ || next;
chomp;
my $n = $_;
$n > ~0 or next;
my $value = $cache_db{$n};
if (not defined $value) {
warn "# $n does not exist in the DB!\n";
next;
}
if (my_is_carmichael_fast($n, [split(' ', $value)])) {
say $n;
}
}
dbmclose(%cache_db);