Continued fraction/Arithmetic/G(matrix ng, continued fraction n): Difference between revisions

Content added Content deleted
(New post.)
m (Improved coding.)
Line 5,021: Line 5,021:
public static void main(String[] aArgs) {
public static void main(String[] aArgs) {
List<CFData> cfData = List.of(
List<CFData> cfData = List.of(
new CFData("[1; 5, 2] + 1/2 ", new int[] { 2, 1, 0, 2 }, (CFIterator) new r2cfIterator(13, 11) ),
new CFData("[1; 5, 2] + 1/2 ", new int[] { 2, 1, 0, 2 }, (CFIterator) new R2cfIterator(13, 11) ),
new CFData("[3; 7] + 1/2 ", new int[] { 2, 1, 0, 2 }, (CFIterator) new r2cfIterator(22, 7) ),
new CFData("[3; 7] + 1/2 ", new int[] { 2, 1, 0, 2 }, (CFIterator) new R2cfIterator(22, 7) ),
new CFData("[3; 7] divided by 4 ", new int[] { 1, 0, 0, 4 }, (CFIterator) new r2cfIterator(22, 7) ),
new CFData("[3; 7] divided by 4 ", new int[] { 1, 0, 0, 4 }, (CFIterator) new R2cfIterator(22, 7) ),
new CFData("sqrt(2) ", new int[] { 0, 1, 1, 0 }, (CFIterator) new reciprocalRoot2() ),
new CFData("sqrt(2) ", new int[] { 0, 1, 1, 0 }, (CFIterator) new ReciprocalRoot2() ),
new CFData("1 / sqrt(2) ", new int[] { 0, 1, 1, 0 }, (CFIterator) new root2() ),
new CFData("1 / sqrt(2) ", new int[] { 0, 1, 1, 0 }, (CFIterator) new Root2() ),
new CFData("(1 + sqrt(2)) / 2 ", new int[] { 1, 1, 0, 2 }, (CFIterator) new root2() ),
new CFData("(1 + sqrt(2)) / 2 ", new int[] { 1, 1, 0, 2 }, (CFIterator) new Root2() ),
new CFData("(1 + 1 / sqrt(2)) / 2", new int[] { 1, 1, 0, 2 }, (CFIterator) new reciprocalRoot2() ) );
new CFData("(1 + 1 / sqrt(2)) / 2", new int[] { 1, 1, 0, 2 }, (CFIterator) new ReciprocalRoot2() ) );
for ( CFData data : cfData ) {
for ( CFData data : cfData ) {
Line 5,058: Line 5,058:


public void ingress(int aN) {
public void ingress(int aN) {
int temp = a;
int temp = a; a = a1; a1 = temp + a1 * aN;
a = a1;
temp = b; b = b1; b1 = temp + b1 * aN;
a1 = temp + a1 * aN;
temp = b;
b = b1;
b1 = temp + b1 * aN;
}
}


public int egress() {
public int egress() {
final int n = a / b;
final int n = a / b;
int temp = a;
int temp = a; a = b; b = temp - b * n;
a = b;
temp = a1; a1 = b1; b1 = temp - b1 * n;
b = temp - b * n;
temp = a1;
a1 = b1;
b1 = temp - b1 * n;
return n;
return n;
}
}
Line 5,083: Line 5,075:
public int egressDone() {
public int egressDone() {
if ( needsTerm() ) {
if ( needsTerm() ) {
a = a1;
a = a1;
b = b1;
b = b1;
}
}
Line 5,103: Line 5,095:
}
}
private static class r2cfIterator extends CFIterator {
private static class R2cfIterator extends CFIterator {
public r2cfIterator(int aNumerator, int aDenominator) {
public R2cfIterator(int aNumerator, int aDenominator) {
numerator = aNumerator; denominator = aDenominator;
numerator = aNumerator; denominator = aDenominator;
}
}
Line 5,125: Line 5,117:
}
}


private static class root2 extends CFIterator {
private static class Root2 extends CFIterator {
public root2() {
public Root2() {
firstReturn = true;
firstReturn = true;
}
}
Line 5,147: Line 5,139:
}
}
private static class reciprocalRoot2 extends CFIterator {
private static class ReciprocalRoot2 extends CFIterator {
public reciprocalRoot2() {
public ReciprocalRoot2() {
firstReturn = true;
firstReturn = true;
secondReturn = true;
secondReturn = true;