forked from wandering007/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiophantineQuadraticEquation.java
3747 lines (3631 loc) · 100 KB
/
DiophantineQuadraticEquation.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
// http://www.alpertron.com.ar/QUAD.HTM
// <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.*;
import java.awt.*;
import java.math.*;
public final class quad extends Applet implements Runnable {
private BigInteger Primes[];
private int Exponents[];
private BigInteger PrimesBak[];
private int ExponentsBak[];
private int digitsInGroup;
private Vector sortedSolsX = new Vector(50, 50);
private Vector sortedSolsY = new Vector(50, 50);
private boolean allSolsFound;
private long A, B, C, D, E, F;
private long Xi;
private long Xl;
private long Yi;
private long Yl;
private long CY1, CY0;
private boolean also, ExchXY, teach;
private long SQD;
private long NUM[] = new long[6];
private long DEN[] = new long[6];
private long DET;
private long Mi=1000000000;
private long Bi=Mi*Mi;
private long DosALa32 = (long)1 << 32;
private long DosALa31 = (long)1 << 31;
private double dDosALa32 = (double)DosALa32;
private double dDosALa64 = dDosALa32 * dDosALa32;
private double dDosALa96 = dDosALa64 * dDosALa32;
private double dDosALa128 = dDosALa96 * dDosALa32;
private double dDosALa160 = dDosALa128 * dDosALa32;
private long DosALa32_1 = DosALa32 - 1;
private long A1;
private long A2;
private long B1;
private long B2;
private String UU="";
private String VU="";
private String UL="";
private String VL="";
private String UL1="";
private String VL1="";
private String FP="";
private String msg="There are no solutions !!!",sq;
private String txt="";
private String divgcd="Dividing the equation by the greatest common divisor we obtain:<BR>";
private long CX2;
private long CXY;
private long CY2;
private long CX;
private long CY;
private long C1;
private long H1[];
private long H2[];
private long K1[];
private long K2[];
private long L1[];
private long L2[];
private int NbrSols, NbrCo, NbrEqs, EqNbr;
private StringBuffer info;
private BigInteger ValA, ValB, ValC, ValD, ValE, ValF;
private volatile Thread calcThread;
private String textError;
private void SolveEquation() {
byte b;
long Fact1, Fact2, Tmp;
long biA[] = new long[6];
long biB[] = new long[6];
long biC[] = new long[6];
boolean teachaux;
long D1, E1, F1, G, H, K, P, P1, P2, Q, Q1, Q2, R, R1, R2, X1, Y1;
long S, S1, S2, T, g, r, s, t, u, w1, w2;
String t1,x,y,x1,y1;
CX2=ValA.longValue();
CXY=ValB.longValue();
CY2=ValC.longValue();
CX=ValD.longValue();
CY=ValE.longValue();
C1=ValF.longValue();
also = ExchXY = false;
sortedSolsX.removeAllElements(); /* Start fresh for new equation */
sortedSolsY.removeAllElements();
allSolsFound = false;
NbrCo = -1;
w("<TITLE>"+txt+"</TITLE><CENTER><B>");
A=CX2;B=CXY;C=CY2;D=CX;E=CY;F=C1;
ShowEq(A,B,C,D,E,F,"x","y");
w(" = 0</B></CENTER><P><I>by Dario Alejandro Alpern</I><P>");
t=gcd(A,gcd(B,gcd(C,gcd(D,E))));
if (teach) {
w("First of all we must determine the <B>gcd</B> of all coefficients but the constant term, that is: <B>gcd</B>("+A+", "+B+", "+C+", "+D+", "+E+") = "+t+".<P>");
}
if (t!=0) {
if (F%t!=0) {
NoGcd(F);return;
}
else {
A/=t;B/=t;C/=t;D/=t;E/=t;F/=t;
if (teach) {
w(divgcd);
ShowEq(A,B,C,D,E,F,"x","y");
w(" = 0</B><P>");
}
}
}
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;
}
}
S=B*B-4*A*C;
if (S>0 && sqrt(S)*sqrt(S)!=S && D==0 && E==0 && F!=0)
{
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;
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,"");
}
}
for (T=T-1;T>0;T--) {
if (K%T==0 && T*T<K) {
SolContFrac(H,K/T,A,B,C,"");
}
}
w("<P>");
if (also==false) {
return;
}
if (teach) {
w("<TABLE BORDER=1><TR><TH>");
}
else {
ShowAllLargeSolutions();
}
if (teach==false) {
w("If <B>(x,y)</B> is a solution, <B>(-x,-y)</B> is also a solution.<P>");
}
ShowRecursion((byte)0);
also=true;
return;
}
if (A==0 && C==0) {
if (B==0) {
b=Linear(D,E,F);
if (teach) {
w("<TABLE BORDER=1><TR><TH>");
}
PrintLinear(b,"t");
if (teach) {
w("</TABLE>");
}
return;
}
else {
R=D*E-B*F;
if (teach) {
w("Multiplying by "+B+" we obtain:<BR>");
ShowEq(0,B*B,0,D*B,E*B,0,"x","y");
w(" = "+(-F*B)+"<P>");
w("Adding "+D*E+" to both sides of the equal sign:<BR>");
ShowEq(0,B*B,0,D*B,E*B,D*E,"x","y");
w(" = "+R+"<P>");
w("Now the left side can be factored as follows:<BR>");
w("(");
Show1(E,Show(B," x",(byte)0));
w(") (");
Show1(D,Show(B," y",(byte)0));
w(") = "+R+"<P>");
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+":<P><UL>");
}
else {
w("One of the parentheses must be zero, so:<P>");
Show1(E,Show(B," x",(byte)0));
w(" = 0");
if (E%B==0) {
w(" means that <B>x = "+(-E/B)+"</B> and <B>y</B> could be any integer.<P>");
also=true;
}
else {
w("This equation cannot be solved in integers.<P>");
}
Show1(D,Show(B," y",(byte)0));
w(" = 0");
if (D%B==0) {
w(" means that <B>y = "+(-D/B)+"</B> and <B>x</B> could be any integer.<P>");
also=true;
}
else {
w("This equation cannot be solved in integers.<P>");
}
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;
}
}
if (teach) {
w("We try now to solve this equation module 9, 16 and 25.<P>");
}
if (Mod(9)) {return;}
if (Mod(16)) {return;}
if (Mod(25)) {return;}
if (teach) {
w("There are solutions, so we must continue.<P>");
}
if (A==0) {
T=A;A=C;C=T;T=D;D=E;E=T;ExchXY=true;
}
D1=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 (D1==0) { /* Parabolic case */
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)+":<BR>");
ShowEq(A*s,B*s,C*s,D*s,E*s,F*s,x,y);
w(" = 0<P>");
}
if (r!=2) {
w("Extracting the factor "+r/2+" in the quadratic terms:<BR>");
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<P>");
}
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<P>");
if (D!=0) {
w("Adding and subtracting "+par1(B*D/r)+""+y+":<BR>");
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<P>Now we perform the substitution:<BR>");
w(x1+" = ");
ShowLin(s,B/r,0,x,y);
w("<P>This gives:<BR>");
ShowEq(r/2,0,0,D,R,F*s,x1,y);
w(" = 0<P>");
}
if (E1==0) {
if (teach) {
w("This can be solved by the standard quadratic equation formula:<P>");
w("The roots are: "+x1+" = ");
if (D!=0) {
w("("+par(-D)+" ± sqrt("+(-F1)+"))");
}
else {
w(" ± sqrt("+(-F1)+")");
}
w(" / "+par(r)+"<P>");
}
if (F1>0) {
if (teach) {
w("This quadratic equation has no solution in reals, so it has no solution in integers.<P>");
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+"<P>");
ShowLin(s,B/r,0,x,y);
w(" = "+T+"<P>");
}
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.<P>");
}
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+"<P>");
ShowLin(s,B/r,0,x,y);
w(" = "+T+"<P>");
}
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.<P>");
}
return;
}
long N=DivideGcd(P,Q,R,S,x1,y);
if (N==0) {
return;
}
P/=N;Q/=N;R/=N;S/=N;
int numEq=1;
for (u=0;u<abs(R);u++) {
T=P*u*u+Q*u+S;
if (T%R==0) {
int numEq2=3;
P1=B*P*R/r;Q1=abs(R)+B*(2*P*u+Q)*abs(R)/R/r;
R1=-s;S1=B*T/R/r+u;
P2=-P*R;Q2=(2*P*u+Q)*abs(R)/-R;S2=-T/R;
if (teach) {
t1=((u==0)?"":u+" + ")+abs(R)+"t";
w("<LI>"+x1+" = "+t1+" <B>("+numEq+")</B>");
w("<P>Replacing this in the equation shown above:<BR>");
w(par1(-R)+y+" = ");
ShowEq(P,0,0,Q,0,S,"("+t1+")",y);
w("<P>"+par1(-R)+y+" = ");
ShowEq(P*R*R,0,0,(2*P*u+Q)*abs(R),0,T,"t",y);
w("<P>"+y+" = ");
ShowEq(P2,0,0,Q2,0,S2,"t",y);
w(" <B>("+(numEq+1)+")</B><P>From <B>("+numEq+")</B>: ");
ShowLin(s,B/r,0,x,y);
w(" = "+t1+"<P>Replacing <B>("+(numEq+1)+")</B> here:<BR>");
w(par1(s)+x+" = ");
ShowEq(P1,0,0,Q1,0,S1,"t",y);
w(" <B>("+(numEq+2)+")</B><P>");
}
long N1=DivideGcd(P1,Q1,R1,S1,"t",x);
if (N1==0) {
continue;
}
P1/=N1;Q1/=N1;R1/=N1;S1/=N1;
for (long u1=0;u1<abs(R1);u1++) {
long T1=P1*u1*u1+Q1*u1+S1;
if (T1%R1==0) {
if (teach && abs(R1)!=1) {
t1=((u1==0)?"":u1+" + ")+abs(R1)+"u";
w("<LI>t = "+t1+" <B>("+(numEq+numEq2)+")</B>");
w("<P>Replacing this in <B>("+(numEq+2)+")</B>:<BR>");
w(par1(-R1)+x+" = ");
ShowEq(P1,0,0,Q1,0,S1,"("+t1+")",y);
w("<P>"+par1(-R1)+x+" = ");
ShowEq(P1*R1*R1,0,0,(2*P1*u1+Q1)*abs(R1),0,T1,"u","");
w("<P>"+x+" = ");
ShowEq(-P1*R1,0,0,(2*P1*u1+Q1)*abs(R1)/-R1,0,-T1/R1,"u",y);
w("<P>From <B>("+(numEq+1)+")</B> and <B>("+(numEq+numEq2)+")</B>:<BR>"+y+" = ");
ShowEq(P2,0,0,Q2,0,S2,"("+t1+")",y);
w("<P>"+y+" = ");
ShowEq(P2*R1*R1,0,0,(2*P2*u1+Q2)*abs(R1),0,P2*u1*u1+Q2*u1+S2,"u","");
w("<P>");
numEq2++;
}
if (teach) {
w("<TABLE BORDER=1><TR><TH>");
}
showAlso();
w("x = ");
if (ExchXY==false) {
ShowEq(-P1*R1,0,0,(2*P1*u1+Q1)*abs(R1)/-R1,0,-T1/R1,"u","");
w("<BR>y = ");
ShowEq(P2*R1*R1,0,0,(2*P2*u1+Q2)*abs(R1),0,P2*u1*u1+Q2*u1+S2,"u","");
}
else {
ShowEq(P2*R1*R1,0,0,(2*P2*u1+Q2)*abs(R1),0,P2*u1*u1+Q2*u1+S2,"u","");
w("<BR>y = ");
ShowEq(-P1*R1,0,0,(2*P1*u1+Q1)*abs(R1)/-R1,0,-T1/R1,"u","");
}
if (teach) {
w("</TABLE>");
}
w("<P>");
}
}
if (teach && abs(R1)!=1) {
w("</UL>");
}
numEq+=numEq2;
}
}
if (teach) {
w("</UL>");
}
return;
}
g=gcd(D1,E1/2);
CY1=D1/g;
CY0=E1/2/g;
long D0=D1;
long N0=CY0*CY0*g-CY1*F1;
long h=gcd(CY1,gcd(g,N0));
double sqrt = Math.sqrt((double)g*(double)N0);
double R3=(-E1/2-sqrt)/D1;
R1=(long)Math.ceil(R3);
double R4=(-E1/2+sqrt)/D1;
R2=(long)Math.floor(R4);
if (teach) {
w("We want to convert this equation to one of the form:<BR>");
w(x1+sq+" + B "+y+sq+" + C "+y+" + D = 0<P>");
w("Multiplying the equation by "+par(4*A)+":<BR>");
ShowEq(4*A*A,4*A*B,4*A*C,4*A*D,4*A*E,4*A*F,x,y);
w(" = 0<P>");
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<P>");
}
if (B!=0 || D!=0) {
w("To complete the square we should add and subtract:<BR>(");
ShowLin(0,B,D,x,y);
w(")"+sq+"<P>Then the equation converts to:<BR>(");
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<P>");
}
w("(");
ShowLin(2*A,B,D,x,y);
w(")"+sq+" + (");
ShowEq(0,0,D1,0,E1,F1,x,y);
w(") = 0<P>Now we perform the substitution:<BR>");
w(x1+" = ");
ShowLin(2*A,B,D,x,y);
w("<P>This gives:<BR>");
ShowEq(1,0,D1,0,E1,F1,x1,y);
w(" = 0<P>");
if (D0>0) { /* elliptical case */
w("Since "+x1+sq+" is always greater than, or equal to zero,<BR>");
ShowEq(0,0,D1,0,E1,F1,x1,y);
w(" must be less than, or equal to zero. This is verified in the segment limited by the roots.<P>");
if (N0<0) {
w("The polynomial in "+y+" is always positive,");
NoSol();
return;
}
w("The roots are: (-"+par(E1)+" - sqrt("+E1+sq+" - 4 * "+par(D1)+" * "+par(F1)+")) / (2 * "+par(D1)+") = "+R3+"<BR>");
w("and: (-"+par(E1)+" + sqrt("+E1+sq+" - 4 * "+par(D1)+" * "+par(F1)+")) / (2 * "+par(D1)+") = "+R4+"<P>");
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 <BR>");
ShowEq(0,0,D1,0,E1,F1,x1,y);
w(". The result should be the negative of a perfect square.<P>");
b=0;
for (u=R1;u<=R2;u++) {
w1=-D1*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("<P><UL>");
}
}
if (D0>0 && N0<0) {
return;
}
if (D0>0) {
for (u=R1;u<=R2;u++) {
w1=-D1*u*u-E1*u-F1;w2=sqrt(w1);
if (w2*w2==w1) {
if (teach) {
w("<LI>"+y+" = "+u+"<BR>");
w(x1+" = ");
ShowLin(2*A,B,D,x,y);
w(" = ±sqrt("+(w2*w2)+") = ±"+w2+"<P><UL>");
}
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;
}
if (teach) {
if (D1!=g*h) {
w("Multiplying the equation by "+CY1/h+":<BR>");
ShowEq(CY1/h,0,D1*CY1/h,0,D1*E1/g/h,D1*F1/g/h,x1,y);
}
w(" = 0<P>");
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(D1*F1/g/h,(byte)1);
w(" = 0<P>");
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(D1*F1/g/h,(byte)1);
w(" = 0<P>");
w("Adding and subtracting "+(g==h?"":g/h+" * ")+par(E1/2/g)+sq+":<BR>");
}
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(D1*F1/g/h,(byte)1);
w(" - "+(g==h?"":g/h+" * ")+par(E1/2/g)+sq+" = 0<P>");
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<P>Making the substitution "+y1+" = ");
ShowLin(0,CY1,CY0,x,y);
w(":<BR>");
ShowLin(CY1/h,g/h,-N0/h,x1+sq,y1+sq);
w(" = 0<P>");
}
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)+"<P>");
}
if (N0==0) {
if (teach) {
w("<P>One of the parentheses must be zero, so:<P><UL>");
}
for (T=(Sqd==0?1:0); T<2; T++) {
if (teach) {
w("<LI>");
ShowLin(Yc,Xc,0,y1,x1);
w(" = 0<P>"+par(Yc)+" (");
ShowLin(0,CY1,CY0,x,y);
w(") + "+par1(Xc)+" (");
ShowLin(2*A,B,D,x,y);
w(") = 0<P>");
ShowLin(2*A*Xc,Xc*B+CY1*Yc,D*Xc+CY0*Yc,x,y);
w(" = 0<P>");
}
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("</TABLE><P>");
}
Xc=-Xc;
}
Sqd=abs(Sqd);
if (teach) {
w("</UL>");
}
return;
}
S=sqrt(N1);
if (teach) {
w("Now we have to find all factors of "+N1+".<P><OL>");
}
for (T=1;T<=S;T++) {
if (N1%T==0) {
Fact1=T;
Fact2=N0/h/T;
if (teach) {
w("<LI>Since "+(Fact1*Fact2)+" is equal to "+Fact1+" times "+Fact2+", we can set:<BR>");
ShowLin(Yc,Xc,0,y1,x1);
w(" = "+Fact1+"<BR>");
ShowLin(Yc,-Xc,0,y1,x1);
w(" = "+Fact2+"<BR>");
if ((Fact1-Fact2)%(2*Xc)==0 && (Fact1+Fact2)%(2*Yc)==0) {
X1=(Fact1-Fact2)/(2*Xc);
Y1=(Fact1+Fact2)/(2*Yc);
w(x1+" = "+X1+"<BR>"+y1+" = "+Y1+"<P><B>");
ShowX1Y1(X1,Y1,A,B,D,CY1,CY0);
w("</B>");
}
else {
w("Solving this system we do not obtain integer values for "+x1+" and "+y1+".<P>");
}
}
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("</OL><P>");
}
return;
}
if (N0==0) {
ShowX1Y1(0,0,A,B,D,D1,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(D1/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,D1/g/h,0,g/h,"'");
}
}
for (T=T-1;T>0;T--) {
if (K%T==0 && T*T<K) {
SolContFrac(H,K/T,D1/g/h,0,g/h,"'");
}
}
w("<P>");
if (also==false) {
return;
}
else {
if (teach==false) {
ShowAllLargeSolutions();
}
}
ShowRecursion((byte)1);
also=true;
return;
}
private void NoSol() {
w(" so there are no integer solutions.<P>");
also=true;
}
private void NoGcd(long F) {
if (teach) {
w("This <B>gcd</B> is not a divisor of the constant term ("+F+"),");
NoSol();
}
}
private void w(String texto) {
info=info.append(texto);
}
private long gcd(long M,long N) {
long P=M;
long Q=N;
while (P!=0) {
long R=Q%P;
Q=P;
P=R;
}
return abs(Q);
}
private long abs(long num) {
return (num<0)?-num:num;
}
private long floordiv(long num,long den) {
if ((num<0 && den>0 || num>0 && den<0) && num%den!=0) {
return num/den-1;
}
return num/den;
}
private long ceildiv(long num,long den) {
if ((num>0 && den>0 || num<0 && den<0) && num%den!=0) {
return num/den+1;
}
return num/den;
}
private long sqrt(long num) {
long num1=0;
long num2=(long)65536*(long)32768; /* 2^31 */
while (num2!=0) {
if ((num1+num2)*(num1+num2)<=num) {
num1+=num2;
}
num2/=2;
}
return num1;
}
/* Calculate factor1*factor2 mod Mod */
private long MultMod(long factor1, long factor2, long Mod) {
long aux;
aux=factor1*factor2-Mod*(long)(((double)factor1*(double)factor2)/(double)Mod);
if (aux>=Mod) {return aux-Mod;}
if (aux<0) {return aux+Mod;}
return aux;
}
private long ModPow(long Base, long Exp, long Mod) {
long Pot, Pwr, mask, value;
if (Exp==0) {return 1L;}
mask = 1L;
Pot = 1L;
Pwr = Base;
value = 0;
while (true) {
if ((Exp & mask) != 0) {
Pot = MultMod(Pot,Pwr,Mod);
value += mask;
if (value == Exp) {return Pot;}
}
mask *= 2L;
Pwr = MultMod(Pwr,Pwr,Mod);
}
}
private long ModInv(long Val, long Mod) {
long U1,U3,V1,V3,Aux,Q;
U1=1;U3=Val;V1=0;V3=Mod;
while (V3!=0) {
Q=U3/V3;
Aux=U1-V1*Q;U1=V1;V1=Aux;
Aux=U3-V3*Q;U3=V3;V3=Aux;
}
return (U1+Mod)%Mod;
}
private boolean Mod(long mod) {
for (long x=0;x<mod;x++) {
long z=A*x*x+D*x+F;
long t=B*x+E;
for (long y=0;y<mod;y++) {
if ((z+y*(t+C*y))%mod == 0) {
return false;
}
}
}
if (teach) {
w("No solutions found using mod "+mod+",");
NoSol();
}
return true;
}
private long DivideGcd(long P,long Q,long R,long S,String x1,String y) {
byte t;
long N=gcd(P,gcd(Q,R));
if (N!=1) {
if (teach) {
w("We must get the gcd of all terms except the constant:<BR>");
w("gcd("+P);
if (Q!=0) {
w(", "+Q);
}
if (R!=0) {
w(", "+R);
}
w(") = "+N+"<P>");
}
if (S%N!=0) {
NoGcd(S);
return 0;
}
if (teach) {
w(divgcd);
P/=N;Q/=N;R/=N;S/=N;
ShowEq(P,0,0,Q,R,S,x1,y);
w(" = 0<P>");
}
}
if (teach) {
if (abs(R)!=1) {
w("This means that ");
ShowEq(P,0,0,Q,0,S,x1,y);
w(" should be a multiple of "+abs(R)+"<P>");
w("To determine this, we should try all values of "+x1+" from 0 to "+(abs(R)-1)+" to check if the condition holds.<BR>");
t=0;
for (long u=0;u<abs(R);u++) {
if ((P*u*u+Q*u+S)%R == 0) {
if (t!=0) {
w(", "+u);
}
else {
w("The values of "+x1+" (mod "+abs(R)+") are: "+u);
t=1;
}
}
}
if (t==0) {
w("The modular equation is not satisfied by any "+x1);
NoSol();
return 0;
}
w("<UL>");
}
else {
t=1;
}
}
return N;
}
private String par(long num) {
if (num<0) {
return "("+num+")";
}
return ""+num;
}
private String par1(long num) {
return (num==1)?"":par(num);
}
private byte Linear(long D,long E,long F) {
long Tx;
int t;
if (teach) {
w("This is a linear equation ");
ShowLin(D,E,F,"x","y");
w(" = 0<P>");
}
if (D==0) {
if (E==0) {
if (F!=0) {
return 1; // No solutions
}
else {
return 2; // Infinite number of solutions
}
}
if (F%E!=0) {
return 1; // No solutions
}
else {
Xi=0;Xl=1;Yi=-F/E;Yl=0;
return 0; // Solution found
}
}
if (E==0) {