find_least_common_denominator.pl 729 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 10 May 2016
  5. # Website: https://github.com/trizen
  6. # Find the least common denominator for a list of fractions and map each
  7. # numerator to the ratio of the common denominator over the original denominator.
  8. use 5.010;
  9. use strict;
  10. use warnings;
  11. use ntheory qw(lcm);
  12. use Math::AnyNum qw(:overload);
  13. my @fractions = (
  14. 19 / 6,
  15. 160 / 51,
  16. 1744 / 555,
  17. 644 / 205,
  18. 2529 / 805,
  19. );
  20. my $common_den = lcm(map { $_->denominator } @fractions);
  21. my @numerators = map {
  22. $_->numerator * $common_den / $_->denominator
  23. } @fractions;
  24. say "=> Numerators:";
  25. foreach my $n (@numerators) { say "\t$n" }
  26. say "\n=> Common denominator: $common_den";