dbm2perl_fermat.pl 752 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/perl
  2. use 5.020;
  3. use autodie;
  4. use warnings;
  5. use Math::GMPz;
  6. use Storable;
  7. use ntheory qw(:all);
  8. use Math::Prime::Util::GMP;
  9. eval { require GDBM_File };
  10. my $cache_db = "factors.db";
  11. my $storable_file = "factors-fermat.storable";
  12. dbmopen(my %cache_db, $cache_db, 0444)
  13. or die "Can't create/access database <<$cache_db>>: $!";
  14. my $table = {};
  15. if (-e $storable_file) {
  16. say "# Loading data...";
  17. $table = retrieve($storable_file);
  18. }
  19. say "# Checking database...";
  20. while (my ($key, $value) = each %cache_db) {
  21. next if exists $table->{$key};
  22. if (Math::Prime::Util::GMP::is_pseudoprime($key, 2)) {
  23. $table->{$key} = $value;
  24. }
  25. }
  26. say "# Storing data...";
  27. store($table, $storable_file);
  28. dbmclose(%cache_db);