417 Reciprocal cycles II.pl 793 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 17 May 2020
  4. # https://github.com/trizen
  5. # Reciprocal cycles II
  6. # https://projecteuler.net/problem=417
  7. # Runtime: 2 minutes
  8. # Simple solution, by removing any divisibility by 2 and 5 from n, then:
  9. # L(n) = znorder(10, n)
  10. # Optimization: iterate over the odd integers k and ingore multiples of 5.
  11. use 5.014;
  12. use integer;
  13. use ntheory qw(:all);
  14. use experimental qw(signatures);
  15. my $sum = 0;
  16. my $limit = 100_000_000;
  17. for (my $k = 3 ; $k <= $limit ; $k += 2) {
  18. ($k % 5) || next;
  19. my @smooth = ($k);
  20. foreach my $p (2, 5) {
  21. foreach my $n (@smooth) {
  22. if ($n * $p <= $limit) {
  23. push @smooth, $n * $p;
  24. }
  25. }
  26. }
  27. $sum += scalar(@smooth) * znorder(10, $k);
  28. }
  29. say $sum;