Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implementing 2/13 according to ahmet

  • Loading branch information...
commit 4aa5b286d18c2329ac6d9750ed6b42822486e901 1 parent db9c465
John Trammell authored
View
1  .gitignore
@@ -5,3 +5,4 @@ pm_to_blib
MANIFEST.bak
Makefile.old
Math-Fraction-Egyptian-*
+Debian_*
View
33 lib/Math/Fraction/Egyptian/StrictPractical.pm
@@ -2,20 +2,47 @@ package Math::Fraction::Egyptian::StrictPractical;
use strict;
use warnings FATAL => 'all';
+use List::Util 'max';
+use Math::Fraction::Egyptian::Utils 'is_practical';
-=head2 s_practical_strict($n,$d)
+=head1 NAME
+Math::Fraction::Egyptian::StrictPractical - stricter usage of practical
+numbers
+=head1 SYNOPSIS
+ use Math::Fraction::Egyptian::StrictPractical;
+ my @e = Math::Fraction::Egyptian::StrictPractical->expand(2,13);
+
+=head1 DESCRIPTION
+
+Ahmes uses the following expansion of 2/13 using the property of 8 * 13
+being a practical number:
+
+ (2/13) * (8/8) = 16/104
+ = (13 + 2 + 1)/104
+ = 1/8 + 1/52 + 1/104
+
+When a solution with a smaller practical number exists:
+
+ (2/13) * (6/6) = 12/78
+ = (6 + 3 + 2 + 1)/78
+ = 1/13 + 1/26 + 1/39 + 1/78
+
+=head1 METHODS
+
+=head2 $class->expand($numer,$denom)
=cut
-sub s_practical_strict {
- my ($N,$D) = @_;
+sub expand {
+ my ($class,$N,$D) = @_;
# find multiples of $d that are practical numbers
my @mult = grep { is_practical($_ * $D) } 1 .. $D;
+ warn ">>> @mult";
die "unsuitable strategy" unless @mult;
MULTIPLE:
View
2  lib/Math/Fraction/Egyptian/Utils.pm
@@ -5,7 +5,7 @@ use warnings FATAL => 'all';
use base 'Exporter';
use List::Util qw(first reduce max);
-our @EXPORT_OK = qw( decompose GCD simplify primes );
+our @EXPORT_OK = qw( decompose GCD is_practical simplify primes );
our %EXPORT_TAGS = (all => \@EXPORT_OK);
my %PRIMES = map { $_ => undef } primes();
View
2  t/20-rhind.t
@@ -70,7 +70,7 @@ for my $r (@rhind) {
my @TODO = qw(
- 13 17 19 25 29 31 35 37 41 43 47 53 55 59
+ 17 19 25 29 31 35 37 41 43 47 53 55 59
61 65 67 71 73 79 83 85 89 91 95 97
);
my %TODO = map { $_, 1 } @TODO;
View
3  t/strategy/01-small-prime.t
@@ -20,3 +20,6 @@ is_deeply([small_prime(2,5)],[0,1,3,15]);
# 2/11 => 2/12 + 2/(11)(12) = 0/1 + 1/6 + 1/66
is_deeply([small_prime(2,11)],[0,1,6,66]);
+# 2/13 => 2/14 + 2/(13)(14) = 0/1 + 1/7 + 1/91
+is_deeply([small_prime(2,13)],[0,1,7,91]);
+
View
1  t/strategy/02-practical.t
@@ -9,4 +9,5 @@ local *practical = sub {
};
is_deeply([practical(2,9)], [0,1,6,18], "2/9 => 0/1 + 1/6 + 1/18");
+warn "@{[ practical(2,13) ]}\n";
View
12 t/strategy/05-strict-practical.t
@@ -0,0 +1,12 @@
+use strict;
+use warnings FATAL => 'all';
+use Test::More 'no_plan';
+
+use_ok('Math::Fraction::Egyptian::StrictPractical');
+
+local *sp = sub {
+ Math::Fraction::Egyptian::StrictPractical->expand(@_);
+};
+
+is_deeply([sp(2,13)], [0,1,8,52,104], "2/13 => 0/1 + 1/8 + 1/52 + 1/104");
+
Please sign in to comment.
Something went wrong with that request. Please try again.