-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiophantine.java
4218 lines (4064 loc) · 158 KB
/
diophantine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// <XMP>
// Diophantine Quadratic Equation Solver
// ax^2 + bxy + cy^2 + dx + ey + f = 0 (unknowns x,y integer numbers)
//
// Written by Dario Alejandro Alpern (Buenos Aires - Argentina)
// Last updated December 15th, 2003
//
// No part of this code can be used for commercial purposes without
// the written consent from the author. Otherwise it can be used freely.
//
//import java.applet.*;
import java.util.Scanner;
import java.util.*;
//import java.awt.*;
import java.math.*;
//public final class quad extends Applet implements Runnable {
public class diophantine {
//private static BigInteger Primes[];
//private static int Exponents[];
//private static BigInteger PrimesBak[];
//private static int ExponentsBak[];
private static int digitsInGroup = 6;
private static Vector sortedSolsX = new Vector(50, 50);
private static Vector sortedSolsY = new Vector(50, 50);
private static boolean allSolsFound;
private static long A, B, C, D, E, F;
private static long Xi;
private static long Xl;
private static long Yi;
private static long Yl;
private static long CY1, CY0;
private static boolean also, ExchXY, teach;
private static long SQD;
private static long NUM[] = new long[6];
private static long DEN[] = new long[6];
private static long DET;
private static final long BILL = 1000000000;
private static final long BILL_BILL = BILL * BILL;
private static final long DosALa32 = (long) 1 << 32;
private static final long DosALa31 = (long) 1 << 31;
private static final double dDosALa32 = (double) DosALa32;
private static final double dDosALa64 = dDosALa32 * dDosALa32;
private static final double dDosALa96 = dDosALa64 * dDosALa32;
private static final double dDosALa128 = dDosALa96 * dDosALa32;
private static final double dDosALa160 = dDosALa128 * dDosALa32;
private static final long DosALa32_1 = DosALa32 - 1;
private static long A1;
private static long A2;
private static long B1;
private static long B2;
private static String UU = "";
private static String VU = "";
private static String UL = "";
private static String VL = "";
private static String UL1 = "";
private static String VL1 = "";
private static String FP = "";
private static final String MSG = "There are no solutions !!!";
private static String sq = "^2";
private static String txt = "";
private static final String DIVGCD = "Dividing the equation by the greatest common divisor we obtain:\n";
//private static long CX2;
//private static long CXY;
//private static long CY2;
//private static long CX;
//private static long CY;
//private static long C1;
private static long H1[];
private static long H2[];
private static long K1[];
private static long K2[];
private static long L1[];
private static long L2[];
private static int NbrSols, NbrCo, NbrEqs, EqNbr;
// private static StringBuffer info;
// private static BigInteger ValA, ValB, ValC, ValD, ValE, ValF;
// private volatile Thread calcThread;
// private static String textError;
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
try {
// info = new StringBuffer();
long a, b, c, d, e, f;
boolean owndata = choice("\"Enter own data (else run standard tests)? (Y/N): ");
teach = choice("Detailed explanation required? (Y/N): ");
if (owndata) {
System.out.println("Solve Diophantine equations of the form: Ax^2 + Bxy + Cy^2 + Dx + Ey + T = 0");
a = enternum("enter value for A ");
b = enternum("enter value for B ");
c = enternum("enter value for C ");
d = enternum("enter value for D ");
e = enternum("enter value for E ");
f = enternum("enter value for F ");
Date OldDate = new Date();
long Old = OldDate.getTime(); // store clock time
NbrSols = 0;
init();
SolveEquation(a, b, c, d, e, f);
if (also == false) {
w(MSG);
}
/* calculate elapsed time */
Date NewDate = new Date();
long New = NewDate.getTime(); // get clock time
w("\nCalculation time: ");
int t = (int) (((New - Old) / 1000 + 86400) % 86400);
w(t / 3600 + "h " + ((t % 3600) / 60) + "m " + (t % 60) + "s");
} else {
Date OldDate = new Date();
long Old = OldDate.getTime();
NbrSols = 0;
init();
System.out.println("Run standard tests");
System.out.println("\nNo solution exists \n");
a = 0;
b = 0;
c = 0;
d = 10;
e = 84;
f = 15;
SolveEquation(a, b, c, d, e, f);
System.out.println("\nExample 1");
/* x = -136 + 42t, y = 16 - 5t, where t is any integer number. */
a = 0;
b = 0;
c = 0;
d = 10;
e = 84;
f = 16;
SolveEquation(a, b, c, d, e, f);
System.out.println("\nExample 2");
a = 0;
b = 2;
c = 0;
d = 5;
e = 56;
f = 7;
/*
x = (2-56)/2 = -27, y = (266/2-5)/2 = 64
x = (-2-56)/2 = -29, y = [266/(-2)-5]/2 = -69
x = (14-56)/2 = -21, y = (266/14-5)/2 = 7
x = (-14-56)/2 = -35, y = [266/(-14)-5]/2 = -12
x = (38-56)/2 = -9, y = (266/38-5)/2 = 1
x = (-38-56)/2 = -47, y = [266/(-38)-5]/2 = -6
x = (266-56)/2 = 105, y = (266/266-5)/2 = -2
x = (-266-56)/2 = -161, y = [266/(-266)-5]/2 = -3
The only 8 solutions to the equation are above
*/
SolveEquation(a, b, c, d, e, f);
System.out.println("\nExample 3");
a = 42;
b = 8;
c = 15;
d = 23;
e = 17;
f = -4915;
/* x = -11, y = -1 is the only solution */
// teach = true;
SolveEquation(a, b, c, d, e, f);
// teach = false;
System.out.println("\nExample 4");
a = 8;
b = -24;
c = 18;
d = 5;
e = 7;
f = 16;
/*
set 1: x = -174t^2 - 17t - 2, y = -116t^2 - 21t - 2
set 2: x = -174t^2 - 41t - 4, y = -116t^2 - 37t - 4
*/
SolveEquation(a, b, c, d, e, f);
System.out.println("\nProject Euler problem 140");
a = -1;
b = 0;
c = 5;
d = 0;
e = 14;
f = 1;
SolveEquation(a, b, c, d, e, f);
/*
basic solutions are:
x = ±1, y = 0
x = ±2, y = -3
x = ±5, y = -4
x = ±7, y = 2
also, for each of the solutions above we can generate an infinite number of other
solutions using the formulas:
Xn+1 = PXn + QYn + K
Yn+1 = RXn + SYn + L
where:
P = -9
Q = -20
K = 28
R = -4
S = -9
L = -14
*/
System.out.println("\nExample 5\n");
// teach = true;
a = 18;
b = 41;
c = 19;
d = 0;
e = 0;
f = -24;
/*
this is called a homogeneous equation
basic solutions are:
x = 10, y = -6
x = -10, y = 6
X = 7, Y = -11
X = -7 Y = 11
X = -202 Y = 312
X = 202 Y = -312
x = 10130 Y = -15646
x = -10130 Y = 15646
X = -14 247838 (8 digits) Y = 22 006088 (8 digits)
X = -9245 980567 328630 (16 digits) Y = 14280 613101 505146 (17 digits)
X = 9245 980567 328630 (16 digits) Y = -14280 613101 505146 (17 digits)
X = 6582 595298 (10 digits) Y = -4037 589688 (10 digits)
X = -6582 595298 (10 digits) Y = 4037 589688 (10 digits)
X = 9 258415 606510 (13 digits) Y = -5 678867 025506 (13 digits)
X = -9 258415 606510 (13 digits) Y = 5 678867 025506 (13 digits)
X = 464 279223 068342 (15 digits) Y = -284 776584 090312 (15 digits)
X = -464 279223 068342 (15 digits) Y = 284 776584 090312 (15 digits)
*/
SolveEquation(a, b, c, d, e, f);
a = 2;
b = 5;
c = 2;
d = 6;
e = 6;
f = 4;
SolveEquation(a, b, c, d, e, f);
Date NewDate = new Date();
long New = NewDate.getTime();
w("\nCalculation time: ");
int t = (int) (((New - Old) / 1000 + 86400) % 86400);
w(t / 3600 + "h " + ((t % 3600) / 60) + "m " + (t % 60) + "s");
}
} catch (ArithmeticException err) {
w("\nexception: " + err.getMessage());
err.printStackTrace();
}
w("\nIf you found any mistake or any solution is missing, please send me an email to: alpertron@hotmail.com\n");
// w("<A HREF = \"GBOOK.HTM\">Click here</A> to view or sign my Guestbook.\n");
// calcThread = null;
}
/* get a number. If user types invalid format it will crash! */
private static long enternum(String msg) {
long result;
System.out.print(msg);
result = input.nextLong();
return result;
}
private static boolean choice(String msg) {
String yn = "";
while (!yn.startsWith("y") && !yn.startsWith("Y")
&& !yn.startsWith("N") && !yn.startsWith("n")) {
System.out.print(msg);
yn = input.next();
}
return (yn.startsWith("y") || yn.startsWith("Y"));
}
private static void SolveEquation(long a, long bb, long c, long d, long e,
long f) {
byte b;
long Fact1, Fact2;
long biA[] = new long[6];
long biB[] = new long[6];
long biC[] = new long[6];
boolean teachaux;
long NegDisc, E1, F1, G, H, K, X1, Y1;
long Disc, T, g, gcdA_E;
String x, y, x1, y1;
also = ExchXY = false;
sortedSolsX.removeAllElements();
/* Start fresh for new equation */
sortedSolsY.removeAllElements();
allSolsFound = false;
NbrCo = -1;
w(txt);
A = a;
B = bb;
C = c;
D = d;
E = e;
F = f;
ShowEq(A, B, C, D, E, F, "x", "y");
w(" = 0\n by Dario Alejandro Alpern\n");
gcdA_E = gcd(A, gcd(B, gcd(C, gcd(D, E))));
if (teach) {
w("First of all we must determine the gcd of all coefficients but the constant term, that is: gcd(" + A + ", " + B + ", " + C + ", " + D + ", " + E + ") = " + gcdA_E + ".\n");
}
if (gcdA_E != 0) {
if (F % gcdA_E != 0) {
NoGcd(F);
return;
} else {
A /= gcdA_E;
/* divide all coefficients by gcd */
B /= gcdA_E;
C /= gcdA_E;
D /= gcdA_E;
E /= gcdA_E;
F /= gcdA_E;
if (teach && gcdA_E != 1) {
w(DIVGCD);
ShowEq(A, B, C, D, E, F, "x", "y");
w(" = 0\n");
}
}
}
if (D == 0 && A != 0 && C != 0) {
if (CheckMod(A, B, C, E, F)) {
return;
}
}
if (E == 0 && A != 0 && C != 0) {
if (CheckMod(B, C, A, D, F)) {
return;
}
}
Disc = B * B - 4 * A * C;
if (Disc > 0
&& sqrt(Disc) * sqrt(Disc) != Disc
&& D == 0 && E == 0
&& F != 0) {
/* Disc is not a perfect square, D, E are 0, F not zeor.
this is a type of homogeneous equation*/
teachaux = teach;
if (abs(F) != 1) {
teach = false;
}
LongToDoublePrecLong(A, biA);
LongToDoublePrecLong(B, biB);
LongToDoublePrecLong(C, biC);
GetRoot(biA, biB, biC);
teach = teachaux;
G = H = F;
K = 1;
T = 3;
/* remove any double factors from G, add to K*/
while (G % 4 == 0) {
G /= 4;
K *= 2;
}
while (abs(G) >= T * T) {
while (G % (T * T) == 0) {
G /= T * T;
K *= T;
}
T += 2;
}
for (T = 1; T * T <= K; T++) {
if (K % T == 0) {
SolContFrac(H, T, A, B, C, "");
/* call SolContFrac for each factor of K */
}
}
for (T = T - 1; T > 0; T--) {
if (K % T == 0 && T * T < K) {
SolContFrac(H, K / T, A, B, C, "");
}
}
w("\n");
if (also == false) {
return;
}
if (teach) {
// w("<TABLE BORDER = 1><TR><TH>");
} else {
ShowAllLargeSolutions();
}
if (teach == false) {
w("If (x,y) is a solution, (-x,-y) is also a solution.\n");
}
ShowRecursion((byte) 0);
also = true;
return;
}
if (A == 0 && C == 0) {
if (B == 0) {
/* linear equation: A=B=C=0 */
b = Linear(D, E, F);
if (teach) {
// w("<TABLE BORDER = 1><TR><TH>");
}
PrintLinear(b, "t");
if (teach) {
// w("</TABLE>");
}
return;
} else {
/* simple hyperbolic; A = C = 0; B ≠ 0 */
SolveSimpleHyperbolic();
return;
}
}
/* not linear or simple hyperbolic equation */
if (teach) {
w("We try now to solve this equation module 9, 16 and 25.\n");
}
if (Mod(9)) {
return;
}
if (Mod(16)) {
return;
}
if (Mod(25)) {
return;
}
if (teach) {
w("There are solutions, so we must continue.\n");
}
if (A == 0) {
T = A;
/* swap A and C */
A = C;
C = T;
T = D;
/* swap D and E */
D = E;
E = T;
ExchXY = true;
}
NegDisc = 4 * A * C - B * B;
E1 = 4 * A * E - 2 * B * D;
F1 = 4 * A * F - D * D;
x = (ExchXY ? "y" : "x");
y = (ExchXY ? "x" : "y");
x1 = x + "´";
y1 = y + "´";
if (NegDisc == 0) {
/* Parabolic case */
SolveParabolic(E1, F1, x, y, x1);
return;
}
/* not linear, parabolic or simple hyperbolic equation */
g = gcd(NegDisc, E1 / 2);
CY1 = NegDisc / g;
CY0 = E1 / 2 / g;
long D0 = NegDisc;
long N0 = CY0 * CY0 * g - CY1 * F1;
long h = gcd(CY1, gcd(g, N0));
double sqrt = Math.sqrt((double) g * (double) N0);
if (teach) {
w("We want to convert this equation to one of the form:\n");
w(x1 + sq + " + B " + y + sq + " + C " + y + " + D = 0\n");
w("Multiplying the equation by " + par(4 * A) + ":\n");
ShowEq(4 * A * A, 4 * A * B, 4 * A * C, 4 * A * D, 4 * A * E, 4 * A * F, x, y);
w(" = 0\n");
ShowLin(4 * A * A, 0, 0, x + sq, y);
if (B != 0 || D != 0) {
w(" + (");
ShowLin(0, 4 * A * B, 4 * A * D, x, y);
w(")" + x);
}
if (C != 0 || E != 0 || F != 0) {
w(" + (");
ShowEq(0, 0, 4 * A * C, 0, 4 * A * E, 4 * A * F, x, y);
w(") = 0\n");
}
if (B != 0 || D != 0) {
w("To complete the square we should add and subtract:\n(");
ShowLin(0, B, D, x, y);
w(")" + sq + "\nThen the equation converts to:\n(");
ShowLin(2 * A, B, D, x, y);
w(")" + sq + " + (");
ShowEq(0, 0, 4 * A * C, 0, 4 * A * E, 4 * A * F, x, y);
w(") - (");
ShowEq(0, 0, B * B, 0, 2 * B * D, D * D, x, y);
w(") = 0\n");
}
w("(");
ShowLin(2 * A, B, D, x, y);
w(")" + sq + " + (");
ShowEq(0, 0, NegDisc, 0, E1, F1, x, y);
w(") = 0\nNow we perform the substitution:\n");
w(x1 + " = ");
ShowLin(2 * A, B, D, x, y);
w("\nThis gives:\n");
ShowEq(1, 0, NegDisc, 0, E1, F1, x1, y);
w(" = 0\n");
}
if (D0 > 0) {
/* elliptical case */
SolveElliptical(NegDisc, E1, F1, N0, sqrt, x, x1, y);
return;
}
if (teach) {
if (NegDisc != g * h) {
w("Multiplying the equation by " + CY1 / h + ":\n");
ShowEq(CY1 / h, 0, NegDisc * CY1 / h, 0, NegDisc * E1 / g / h, NegDisc * F1 / g / h, x1, y);
}
w(" = 0\n");
if (E1 != 0) {
Show(g / h, "(", Show(CY1 / h, x1 + sq, (byte) 0));
ShowEq(CY1 * CY1, 0, 0, 2 * CY0 * CY1, 0, 0, y, "");
w(")");
Show1(NegDisc * F1 / g / h, (byte) 1);
w(" = 0\n");
Show(g / h, "(", Show(CY1 / h, x1 + sq, (byte) 0));
if (CY1 != 1) {
w(par(CY1) + sq + " ");
}
w(y + sq + " + 2*");
if (CY1 != 1) {
w(par(CY1) + "*");
}
w(par(CY0) + " " + y + ")");
Show1(NegDisc * F1 / g / h, (byte) 1);
w(" = 0\n");
w("Adding and subtracting " + (g == h ? "" : g / h + " * ") + par(E1 / 2 / g) + sq + ":\n");
}
Show(g / h, "(", Show(CY1 / h, x1 + sq, (byte) 0));
if (CY1 != 1) {
w(par(CY1) + sq + " ");
}
w(y + sq + " + 2*");
if (CY1 != 1) {
w(par(CY1) + "*");
}
w(par(CY0) + " " + y + " + " + par(CY0) + sq + ")");
Show1(NegDisc * F1 / g / h, (byte) 1);
w(" - " + (g == h ? "" : g / h + " * ") + par(E1 / 2 / g) + sq + " = 0\n");
Show(g / h, "(", Show(CY1 / h, x1 + sq, (byte) 0));
ShowLin(0, CY1, CY0, x, y);
w(")" + sq);
Show1(-N0 / h, (byte) 1);
w(" = 0\nMaking the substitution " + y1 + " = ");
ShowLin(0, CY1, CY0, x, y);
w(":\n");
ShowLin(CY1 / h, g / h, -N0 / h, x1 + sq, y1 + sq);
w(" = 0\n");
}
long Sqd = sqrt(-D0);
long N1 = abs(N0 / h);
long Xc = sqrt(abs(CY1 / h));
long Yc = sqrt(g / h);
if (Sqd * Sqd == -D0) {
if (teach) {
w("(");
ShowLin(Yc, Xc, 0, y1, x1);
w(") (");
ShowLin(Yc, -Xc, 0, y1, x1);
w(") = " + (N0 / h) + "\n");
}
if (N0 == 0) {
if (teach) {
w("\nOne of the parentheses must be zero, so:\n");
}
for (T = (Sqd == 0 ? 1 : 0); T < 2; T++) {
if (teach) {
// w("<LI>");
ShowLin(Yc, Xc, 0, y1, x1);
w(" = 0\n" + par(Yc) + " (");
ShowLin(0, CY1, CY0, x, y);
w(") + " + par1(Xc) + " (");
ShowLin(2 * A, B, D, x, y);
w(") = 0\n");
ShowLin(2 * A * Xc, Xc * B + CY1 * Yc, D * Xc + CY0 * Yc, x, y);
w(" = 0\n");
}
b = Linear(2 * A * Xc, Xc * B + CY1 * Yc, D * Xc + CY0 * Yc);
if (teach) {
// w("<TABLE BORDER = 1><TR><TH>");
}
PrintLinear(b, "t");
if (teach) {
w("\n");
}
Xc = -Xc;
}
Sqd = abs(Sqd);
if (teach) {
// w("</UL>");
}
return;
}
Disc = sqrt(N1);
if (teach) {
w("Now we have to find all factors of " + N1 + ".\n");
}
for (T = 1; T <= Disc; T++) {
if (N1 % T == 0) {
Fact1 = T;
Fact2 = N0 / h / T;
if (teach) {
w("Since " + (Fact1 * Fact2) + " is equal to " + Fact1 + " times " + Fact2 + ", we can set:\n");
ShowLin(Yc, Xc, 0, y1, x1);
w(" = " + Fact1 + "\n");
ShowLin(Yc, -Xc, 0, y1, x1);
w(" = " + Fact2 + "\n");
if ((Fact1 - Fact2) % (2 * Xc) == 0 && (Fact1 + Fact2) % (2 * Yc) == 0) {
X1 = (Fact1 - Fact2) / (2 * Xc);
Y1 = (Fact1 + Fact2) / (2 * Yc);
w(x1 + " = " + X1 + "\n" + y1 + " = " + Y1 + "\n");
ShowX1Y1(X1, Y1, A, B, D, CY1, CY0);
w("");
} else {
w("Solving this system we do not obtain integer values for " + x1 + " and " + y1 + ".\n");
}
} else {
if ((Fact1 - Fact2) % (2 * Xc) == 0 && (Fact1 + Fact2) % (2 * Yc) == 0) {
X1 = (Fact1 - Fact2) / (2 * Xc);
Y1 = (Fact1 + Fact2) / (2 * Yc);
ShowX1Y1(X1, Y1, A, B, D, CY1, CY0);
}
}
}
}
if (teach) {
w("\n");
}
return;
}
if (N0 == 0) {
ShowX1Y1(0, 0, A, B, D, NegDisc, E1 / 2);
return;
}
/* Test if we need two cycles or four cycles */
LongToDoublePrecLong(A, biA);
LongToDoublePrecLong(C, biB);
MultDoublePrecLong(biA, biB, biC);
LongToDoublePrecLong(1, biA);
LongToDoublePrecLong(B, biB);
GetRoot(biA, biB, biC);
LongToDoublePrecLong(A, biA);
ContFrac(biA, (byte) 5, (byte) 1, 0, B * B - 4 * A * C, 1, A);
/* A2, B2 solutions */
DET = B * B - 4 * A * C;
G = (2 * A2 + B * B2) % DET;
H = (B * A2 + 2 * A * C * B2) % DET;
if (((C * D * (G - 2) + E * (B - H)) % DET != 0 || (D * (B - H) + A * E * (G - 2)) % DET != 0)
&& ((C * D * (-G - 2) + E * (B + H)) % DET != 0 || (D * (B + H) + A * E * (-G - 2)) % DET != 0)) {
NbrCo *= 2;
}
LongToDoublePrecLong(NegDisc / g / h, biA);
LongToDoublePrecLong(0, biB);
LongToDoublePrecLong(g / h, biC);
GetRoot(biA, biB, biC);
G = H = -N0 / h;
K = 1;
T = 3;
while (G % 4 == 0) {
G /= 4;
K *= 2;
}
while (abs(G) >= T * T) {
while (G % (T * T) == 0) {
G /= T * T;
K *= T;
}
T += 2;
}
for (T = 1; T * T <= K; T++) {
if (K % T == 0) {
SolContFrac(H, T, NegDisc / g / h, 0, g / h, "'");
}
}
for (T = T - 1; T > 0; T--) {
if (K % T == 0 && T * T < K) {
SolContFrac(H, K / T, NegDisc / g / h, 0, g / h, "'");
}
}
w("\n");
if (also == false) {
return;
} else {
if (teach == false) {
ShowAllLargeSolutions();
}
}
ShowRecursion((byte) 1);
also = true;
return;
}
private static void SolveElliptical(long NegDisc, long E1, long F1, long N0,
double sqrt, String x, String x1, String y) {
byte b;
long u, w1, w2;
/* elliptical case */
if (N0 < 0) {
w("The polynomial in " + y + " is always positive,");
NoSol();
return;
}
double R3 = (-E1 / 2 - sqrt) / NegDisc;
long R1 = (long) Math.ceil(R3);
double R4 = (-E1 / 2 + sqrt) / NegDisc;
long R2 = (long) Math.floor(R4);
if (teach) {
w("Since " + x1 + sq + " is always greater than, or equal to zero,\n");
ShowEq(0, 0, NegDisc, 0, E1, F1, x1, y);
w(" must be less than, or equal to zero. This is verified in the segment limited by the roots.\n");
w("The roots are: (-" + par(E1) + " - sqrt(" + E1 + sq + " - 4 * " + par(NegDisc) + " * " + par(F1) + ")) / (2 * " + par(NegDisc) + ") = " + R3 + "\n");
w("and: (-" + par(E1) + " + sqrt(" + E1 + sq + " - 4 * " + par(NegDisc) + " * " + par(F1) + ")) / (2 * " + par(NegDisc) + ") = " + R4 + "\n");
if (R2 < R1) {
w("There are no integers in this range,");
NoSol();
return;
}
w("All values of " + y + " from " + R1 + " to " + R2 + " should be replaced in \n");
ShowEq(0, 0, NegDisc, 0, E1, F1, x1, y);
w(". The result should be the negative of a perfect square.\n");
b = 0;
for (u = R1; u <= R2; u++) {
w1 = -NegDisc * u * u - E1 * u - F1;
w2 = sqrt(w1);
if (w2 * w2 == w1) {
if (b != 0) {
w(", " + u);
} else {
w("The values of " + y + " are: " + u);
b = 1;
}
}
}
if (b == 0) {
w("This is not satisfied by any value of " + y);
NoSol();
return;
}
w("\n");
}
for (u = R1; u <= R2; u++) {
w1 = -NegDisc * u * u - E1 * u - F1;
w2 = sqrt(w1);
if (w2 * w2 == w1) {
if (teach) {
w("" + y + " = " + u + "\n");
w(x1 + " = ");
ShowLin(2 * A, B, D, x, y);
w(" = ±sqrt(" + (w2 * w2) + ") = ±" + w2 + "\n");
}
ShowElipSol(A, B, D, u, x, x1, y, w2);
if (w2 != 0) {
ShowElipSol(A, B, D, u, x, x1, y, -w2);
}
if (teach) {
// w("</UL>");
}
}
}
if (teach) {
// w("</UL>");
}
return;
}
private static void SolveSimpleHyperbolic() {
long R, S, T;
/* simple hyperbolic; A = C = 0; B ≠ 0 */
R = D * E - B * F;
if (teach) {
w("Multiplying by " + B + " we obtain:\n");
ShowEq(0, B * B, 0, D * B, E * B, 0, "x", "y");
w(" = " + (-F * B) + "\n");
w("Adding " + D * E + " to both sides of the equal sign:\n");
ShowEq(0, B * B, 0, D * B, E * B, D * E, "x", "y");
w(" = " + R + "\n");
w("Now the left side can be factored as follows:\n");
w("(");
Show1(E, Show(B, " x", (byte) 0));
w(") (");
Show1(D, Show(B, " y", (byte) 0));
w(") = " + R + "\n");
if (R != 0) {
w("Then ");
Show1(E, Show(B, " x", (byte) 0));
w(" must be a factor of " + R + ", so we must find the factors of " + R + ":\n ");
} else {
w("One of the parentheses must be zero, so:\n");
Show1(E, Show(B, " x", (byte) 0));
w(" = 0");
if (E % B == 0) {
w(" means that x = " + (-E / B) + " and y could be any integer.\n");
also = true;
} else {
w("This equation cannot be solved in integers.\n");
}
Show1(D, Show(B, " y", (byte) 0));
w(" = 0");
if (D % B == 0) {
w(" means that y = " + (-D / B) + " and x could be any integer.\n");
also = true;
} else {
w("This equation cannot be solved in integers.\n");
}
return;
}
}
if (R != 0) {
S = sqrt(abs(R));
for (T = 1; T <= S; T++) {
if (R % T == 0) {
SolByFact(R, T, B, D, E);
SolByFact(R, -T, B, D, E);
if (T * T != abs(R)) {
SolByFact(R, R / T, B, D, E);
SolByFact(R, -R / T, B, D, E);
}
}
}
/* end for */
if (teach) {
// w("</UL>");
}
return;
}
if (E % B == 0) {
Xi = -E / B;
Xl = 0;
Yi = 0;
Yl = 1;
PrintLinear((byte) 0, "t");
}
if (D % B == 0) {
Xi = 0;
Xl = 1;
Yi = -D / B;
Yl = 0;
PrintLinear((byte) 0, "t");
}
return;
}
private static void SolveParabolic(long E1, long F1, String x, String y,
String x1) {
/* Parabolic case */
byte b;
String t1;
long r, s, u, P, P1, P2, Q, Q1, Q2, R, R1, S, S1, S2, T;
r = gcd(2 * A, B);
s = 2 * A / r;
P = r / 2;
Q = D;
R = (2 * A * E - B * D) / r;
S = 2 * A * F / r;
if (teach) {
if (s != 1) {
w("Multiplying the equation by " + par(s) + ":\n");
ShowEq(A * s, B * s, C * s, D * s, E * s, F * s, x, y);
w(" = 0\n");
}
if (r != 2) {
w("Extracting the factor " + r / 2 + " in the quadratic terms:\n");
w(par1(r / 2) + " (");
ShowEq(s * s, 2 * s * B / r, 2 * s * C / r, 0, 0, 0, x, y);
w(")");
b = Show(D * s, " " + x, (byte) 1);
b = Show(E * s, " " + y, b);
Show1(F * s, b);
w(" = 0\n");
}
if (B != 0) {
w(par1(r / 2) + "(");
ShowLin(s, B / r, 0, x, y);
w(")" + sq);
if (D != 0 || E != 0 || F != 0) {
w(" + (");
ShowLin(D * s, E * s, F * s, x, y);
w(")");
}
w(" = 0\n");
if (D != 0) {
w("Adding and subtracting " + par1(B * D / r) + "" + y + ":\n");
w(par1(r / 2) + "(");
ShowLin(s, B / r, 0, x, y);
w(")" + sq);
w(" + " + par1(D) + " (");
ShowLin(s, B / r, 0, x, y);
w(")");
}
if (E1 != 0 || F != 0) {
w(" + (");
ShowLin(0, R, F * s, x, y);
w(")");
}
} else {
w(par1(r / 2) + "(");
ShowLin(s, B / r, 0, x, y);
w(")" + sq);
if (D != 0) {
w(" + " + par1(D) + " (");
ShowLin(s, B / r, 0, x, y);
w(")");
}
if (E1 != 0 || F != 0) {
w(" + (");
ShowLin(0, R, F * s, x, y);
w(")");
}
}
w(" = 0\nNow we perform the substitution:\n");
w(x1 + " = ");
ShowLin(s, B / r, 0, x, y);
w("\nThis gives:\n");
ShowEq(r / 2, 0, 0, D, R, F * s, x1, y);
w(" = 0\n");
}
if (E1 == 0) {
if (teach) {
w("This can be solved by the standard quadratic equation formula:\n");
w("The roots are: " + x1 + " = ");
if (D != 0) {
w("(" + par(-D) + " ± sqrt(" + (-F1) + "))");
} else {
w(" ± sqrt(" + (-F1) + ")");
}
w(" / " + par(r) + "\n");
}
if (F1 > 0) {
if (teach) {
w("This quadratic equation has no solution in reals, so it has no solution in integers.\n");
also = true;
}
return;
}
T = (long) Math.floor((-D + Math.sqrt(-F1)) / r + 0.5);
if (r * T * T / 2 + D * T + 2 * A * F / r == 0) {
if (teach) {
w("The first root is: " + x1 + " = " + T + "\n");
ShowLin(s, B / r, 0, x, y);
w(" = " + T + "\n");
}
b = Linear(s, B / r, -T);
if (teach) {
// w("<TABLE BORDER = 1><TR><TH>");
}
PrintLinear(b, "t");
if (teach) {
// w("</TABLE>");
}
} else {
w("The first root is not an integer.\n");
}
if (F1 == 0) {
return;
}
T = (long) Math.floor((-D - Math.sqrt(-F1)) / r + 0.5);
if (r * T * T / 2 + D * T + 2 * A * F / r == 0) {
if (teach) {
w("The second root is: " + x1 + " = " + T + "\n");
ShowLin(s, B / r, 0, x, y);
w(" = " + T + "\n");
}
b = Linear(s, B / r, -T);
if (teach) {
// w("<TABLE BORDER = 1><TR><TH>");
}
PrintLinear(b, "t");
if (teach) {
// w("</TABLE>");
}
} else {
w("The second root is not an integer.\n");
}
return;
}
long N = DivideGcd(P, Q, R, S, x1, y);
if (N == 0) {