243 Resilience.pl 744 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # https://projecteuler.net/problem=243
  6. use List::Util qw(product);
  7. use ntheory qw(euler_phi next_prime);
  8. sub p {
  9. $_[0]
  10. ? map p($_[0] - 1, [@{$_[1]}, $_[$_]], @_[$_ .. $#_]), 2 .. $#_
  11. : $_[1];
  12. }
  13. my @primes = (2);
  14. my $min = 'inf';
  15. for (; ;) {
  16. push @primes, next_prime($primes[-1]);
  17. foreach my $i (2 .. @primes) {
  18. foreach my $comb (p($i, [], @primes)) {
  19. my $prod = product(@$comb);
  20. if ($prod < $min and euler_phi($prod)/($prod - 1) < 15499/94744) {
  21. $min = $prod;
  22. }
  23. }
  24. }
  25. if ($min < 'inf') {
  26. print "Candidate: $min\n";
  27. }
  28. }