073 Counting fractions in a range -- comb.pl 538 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # Date: 15 August 2016
  4. # License: GPLv3
  5. # Website: https://github.com/trizen
  6. # https://projecteuler.net/problem=73
  7. # Runtime: 26.004s
  8. use 5.010;
  9. use strict;
  10. use ntheory qw(gcd forcomb);
  11. sub count_frac {
  12. my ($n, $min, $max) = @_;
  13. my $count = 0;
  14. forcomb {
  15. my ($x, $y) = ($_[0] + 1, $_[1] + 1);
  16. if ($x / $y > $min and $x / $y < $max and gcd($x, $y) == 1) {
  17. ++$count;
  18. }
  19. } $n, 2;
  20. $count;
  21. }
  22. say count_frac(12_000, 1 / 3, 1 / 2);