820 Nth Digit of Reciprocals.pl 372 B

1234567891011121314151617181920212223
  1. #!/usr/bin/perl
  2. # N-th Digit of Reciprocals
  3. # https://projecteuler.net/problem=820
  4. # Runtime: 10.467s
  5. use 5.036;
  6. use ntheory qw(:all);
  7. sub nth_digit_of_fraction($n, $x, $y, $base = 10) {
  8. divint($base * powmod($base, $n - 1, $y) * $x, $y) % $base;
  9. }
  10. my $sum = 0;
  11. my $n = 1e7;
  12. foreach my $k (1 .. $n) {
  13. $sum += nth_digit_of_fraction($n, 1, $k);
  14. }
  15. say $sum;