sanitize.pl 970 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/perl
  2. use 5.020;
  3. use autodie;
  4. use warnings;
  5. use Math::GMPz;
  6. use ntheory qw(:all);
  7. eval { require GDBM_File };
  8. my $cache_db = "factors.db";
  9. dbmopen(my %cache_db, $cache_db, 0666)
  10. or die "Can't create/access database <<$cache_db>>: $!";
  11. while (my ($key, $value) = each %cache_db) {
  12. my $n = Math::GMPz::Rmpz_init_set_str($key, 10);
  13. my @factors = map { Math::GMPz::Rmpz_init_set_str($_, 10) } split(' ', $value);
  14. if (scalar(@factors) <= 1) {
  15. die "Error for n = $n with [@factors]";
  16. }
  17. if (vecprod(@factors) != $n) {
  18. die "Error for n = $n with [@factors]";
  19. }
  20. if (not vecall { is_provable_prime($_) } @factors) {
  21. die "Composite factor for n = $n with [@factors]";
  22. }
  23. @factors = sort { $a <=> $b } @factors;
  24. if ($value ne join(' ', @factors)) {
  25. say "Factors not in order for n = $n with [@factors]";
  26. $cache_db{$key} = join(' ', @factors);
  27. }
  28. }
  29. dbmclose(%cache_db);