271 Modular Cubes part 1.pl 867 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 13 October 2017
  5. # https://github.com/trizen
  6. # https://projecteuler.net/problem=271
  7. # Runtime: 0.170s
  8. use 5.010;
  9. use strict;
  10. use warnings;
  11. use ntheory qw(chinese powmod factor_exp forsetproduct);
  12. sub modular_cubes {
  13. my ($n) = @_;
  14. my %table;
  15. foreach my $p (factor_exp($n)) {
  16. my $pp = $p->[0]**$p->[1];
  17. push @{$table{$pp}}, [1, $pp];
  18. foreach my $x (2 .. $pp - 1) {
  19. if (powmod($x, 3, $pp) == 1) {
  20. push @{$table{$pp}}, [$x, $pp];
  21. }
  22. }
  23. }
  24. my $sum = 0;
  25. forsetproduct {
  26. my $x = chinese(@_);
  27. if ($x > 1 and powmod($x, 3, $n) == 1) {
  28. $sum += $x;
  29. }
  30. } values %table;
  31. return $sum;
  32. }
  33. modular_cubes(91) == 363 or die "error";
  34. say modular_cubes(13082761331670030);