@@ -33,6 +33,8 @@ private static boolean addsUp(List<BigInteger> egyptian, Fraction f) {
33
33
EgyptianFractions .greedy (new Fraction (124 , 229 )).toString ());
34
34
assertEquals ("[2, 13, 199, 52603, 4150560811, 34454310087467394631]" ,
35
35
EgyptianFractions .greedy (new Fraction (71 , 122 )).toString ());
36
+ assertEquals ("[19, 433, 249553, 93414800161, 17452649778145716451681]" ,
37
+ EgyptianFractions .greedy (new Fraction (5 , 91 )).toString ());
36
38
37
39
/* Pseudorandom fuzz tester */
38
40
CRC32 check = new CRC32 ();
@@ -44,7 +46,13 @@ private static boolean addsUp(List<BigInteger> egyptian, Fraction f) {
44
46
a = rng .nextInt (b - 1 ) + 1 ;
45
47
} while (a % 2 == 0 && b % 2 == 0 );
46
48
Fraction apb = new Fraction (a , b );
49
+ // Verify that the result unit fractions add up to the original.
47
50
List <BigInteger > gRes = EgyptianFractions .greedy (apb );
51
+ Fraction result = new Fraction (0 );
52
+ for (BigInteger n : gRes ) {
53
+ result = result .add (new Fraction (BigInteger .ONE , n ));
54
+ }
55
+ assertEquals (apb , result );
48
56
try {
49
57
check .update (gRes .toString ().getBytes ("UTF-8" ));
50
58
} catch (UnsupportedEncodingException ignored ) {}
@@ -53,21 +61,22 @@ private static boolean addsUp(List<BigInteger> egyptian, Fraction f) {
53
61
assertEquals (93321355L , check .getValue ());
54
62
}
55
63
56
- @ Test public void testSplitting () {
64
+ @ Test public void testPairing () {
57
65
/* Explicit test cases */
58
- assertEquals ("[2, 4, 8, 16]" , EgyptianFractions .splitting (new Fraction (15 , 16 )).toString ());
59
- assertEquals ("[2, 7, 14]" , EgyptianFractions .splitting (new Fraction (20 , 28 )).toString ());
60
- assertEquals ("[7, 8, 56]" , EgyptianFractions .splitting (new Fraction (2 , 7 )).toString ());
61
- assertEquals ("[3, 9, 10, 27, 90]" , EgyptianFractions .splitting (new Fraction (16 , 27 )).toString ());
62
- assertEquals ("[3, 11, 33, 132]" , EgyptianFractions .splitting (new Fraction (61 , 132 )).toString ());
63
- assertEquals ("[9, 10, 17, 90, 153, 154, 23562]" , EgyptianFractions .splitting (new Fraction (5 , 17 )).toString ());
64
- assertEquals ("[5, 15]" , EgyptianFractions .splitting (new Fraction (4 , 15 )).toString ());
65
- assertEquals ("[11, 21, 22, 41, 42, 231, 431, 462, 492, 861, 862, 1722, 371091, 742182]" ,
66
- EgyptianFractions .splitting (new Fraction (121 , 492 )).toString ());
67
- assertEquals ("[6, 11, 12, 66, 132]" , EgyptianFractions .splitting (new Fraction (132 , 363 )).toString ());
68
- assertEquals ("[13, 14, 65, 66, 182, 4290]" , EgyptianFractions .splitting (new Fraction (12 , 65 )).toString ());
69
- assertEquals ("[9, 10, 17, 90, 153, 154, 23562]" , EgyptianFractions .splitting (new Fraction (5 , 17 )).toString ());
70
- assertEquals ("[3, 4, 12, 27, 28, 756]" , EgyptianFractions .splitting (new Fraction (20 , 27 )).toString ());
66
+ assertEquals ("[2, 4, 8, 16]" , EgyptianFractions .pairing (new Fraction (15 , 16 )).toString ());
67
+ assertEquals ("[2, 7, 14]" , EgyptianFractions .pairing (new Fraction (20 , 28 )).toString ());
68
+ assertEquals ("[4, 28]" , EgyptianFractions .pairing (new Fraction (2 , 7 )).toString ());
69
+ assertEquals ("[3, 5, 27, 45]" , EgyptianFractions .pairing (new Fraction (16 , 27 )).toString ());
70
+ assertEquals ("[3, 11, 33, 132]" , EgyptianFractions .pairing (new Fraction (61 , 132 )).toString ());
71
+ assertEquals ("[5, 17, 45, 77, 11781]" , EgyptianFractions .pairing (new Fraction (5 , 17 )).toString ());
72
+ assertEquals ("[5, 15]" , EgyptianFractions .pairing (new Fraction (4 , 15 )).toString ());
73
+ assertEquals ("[6, 21, 66, 116, 216, 492, 861, 26796, 93096, 185546, 68854450686]" ,
74
+ EgyptianFractions .pairing (new Fraction (121 , 492 )).toString ());
75
+ assertEquals ("[3, 33]" , EgyptianFractions .pairing (new Fraction (132 , 363 )).toString ());
76
+ assertEquals ("[7, 33, 91, 2145]" , EgyptianFractions .pairing (new Fraction (12 , 65 )).toString ());
77
+ assertEquals ("[5, 17, 45, 77, 11781]" , EgyptianFractions .pairing (new Fraction (5 , 17 )).toString ());
78
+ assertEquals ("[2, 6, 14, 378]" , EgyptianFractions .pairing (new Fraction (20 , 27 )).toString ());
79
+ assertEquals ("[23, 91, 2093]" , EgyptianFractions .pairing (new Fraction (5 , 91 )).toString ());
71
80
72
81
/* Pseudorandom fuzz tester */
73
82
CRC32 check = new CRC32 ();
@@ -79,12 +88,18 @@ private static boolean addsUp(List<BigInteger> egyptian, Fraction f) {
79
88
a = rng .nextInt (b - 1 ) + 1 ;
80
89
} while (a % 2 == 0 && b % 2 == 0 );
81
90
Fraction apb = new Fraction (a , b );
82
- List <BigInteger > gRes = EgyptianFractions .splitting (apb );
91
+ // Verify that the result unit fractions add up to the original.
92
+ List <BigInteger > pRes = EgyptianFractions .pairing (apb );
93
+ Fraction result = new Fraction (0 );
94
+ for (BigInteger n : pRes ) {
95
+ result = result .add (new Fraction (BigInteger .ONE , n ));
96
+ }
97
+ assertEquals (apb , result );
83
98
try {
84
- check .update (gRes .toString ().getBytes ("UTF-8" ));
99
+ check .update (pRes .toString ().getBytes ("UTF-8" ));
85
100
} catch (UnsupportedEncodingException ignored ) {}
86
- assertTrue (addsUp (gRes , apb ));
101
+ assertTrue (addsUp (pRes , apb ));
87
102
}
88
- assertEquals (2886553470L , check .getValue ());
103
+ assertEquals (2989506769L , check .getValue ());
89
104
}
90
105
}
0 commit comments