Skip to content

Commit ebd3c7d

Browse files
authored
Add files via upload
Fixed the error in spec of "Egyptian Fractions" splitting problem, and changed the problem name to "pairing" in line of original Eppstein page.
1 parent 6ef9b9f commit ebd3c7d

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

CCPS 209 Labs.pdf

3.14 MB
Binary file not shown.

EgyptianFractionsTest.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ private static boolean addsUp(List<BigInteger> egyptian, Fraction f) {
3333
EgyptianFractions.greedy(new Fraction(124, 229)).toString());
3434
assertEquals("[2, 13, 199, 52603, 4150560811, 34454310087467394631]",
3535
EgyptianFractions.greedy(new Fraction(71, 122)).toString());
36+
assertEquals("[19, 433, 249553, 93414800161, 17452649778145716451681]",
37+
EgyptianFractions.greedy(new Fraction(5, 91)).toString());
3638

3739
/* Pseudorandom fuzz tester */
3840
CRC32 check = new CRC32();
@@ -44,7 +46,13 @@ private static boolean addsUp(List<BigInteger> egyptian, Fraction f) {
4446
a = rng.nextInt(b - 1) + 1;
4547
} while(a % 2 == 0 && b % 2 == 0);
4648
Fraction apb = new Fraction(a, b);
49+
// Verify that the result unit fractions add up to the original.
4750
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);
4856
try {
4957
check.update(gRes.toString().getBytes("UTF-8"));
5058
} catch(UnsupportedEncodingException ignored) {}
@@ -53,21 +61,22 @@ private static boolean addsUp(List<BigInteger> egyptian, Fraction f) {
5361
assertEquals(93321355L, check.getValue());
5462
}
5563

56-
@Test public void testSplitting() {
64+
@Test public void testPairing() {
5765
/* 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());
7180

7281
/* Pseudorandom fuzz tester */
7382
CRC32 check = new CRC32();
@@ -79,12 +88,18 @@ private static boolean addsUp(List<BigInteger> egyptian, Fraction f) {
7988
a = rng.nextInt(b - 1) + 1;
8089
} while(a % 2 == 0 && b % 2 == 0);
8190
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);
8398
try {
84-
check.update(gRes.toString().getBytes("UTF-8"));
99+
check.update(pRes.toString().getBytes("UTF-8"));
85100
} catch(UnsupportedEncodingException ignored) {}
86-
assertTrue(addsUp(gRes, apb));
101+
assertTrue(addsUp(pRes, apb));
87102
}
88-
assertEquals(2886553470L, check.getValue());
103+
assertEquals(2989506769L, check.getValue());
89104
}
90105
}

0 commit comments

Comments
 (0)