/* gnubc program : posformrep */ /* Needs squareroot and reduceneg */ /* sign of an integer a */ /* sign(a)=1,-1,0, according as a>0,a<0,a=0 */ define sign(a){ if(a>0) return(1) if(a<0) return(-1) return(0) } /* absolute value of an integer n */ define abs(n){ if(n>=0) return(n) return(-n) } /* mod(a,b)=the least non-negative remainder when an integer a is divided by a positive integer b */ define mod(a,b){ auto c c=a%b if(a>=0) return(c) if(c==0) return(0) return(c+b) } /* int(a,b)=integer part of a/b, a, b integers, b != 0 */ define int(a,b){ auto c c=sign(b) a=a*c b=b*c return((a-mod(a,b))/b) } /* posrep(a,b,c,n) deals with the more general case of finding the primitive soluitons of * ax^2+bxy+cy^2=n, where gcd(a,n) may be >1. Again we assume d=b^2-4ac<0 * and that d is not a square. Also we assume gcd(a,b,c)=1. Here a>0 and n>0. * The number r of primitive solutions is returned. */ define posrep(a,b,c,n,printflag){ auto aa,bb,cc,i,r,s,tmp1,tmp2,tmp3,tmp4 if(gcd(a,n)==1){ r=posrep0(a,b,c,n,printflag) for(i=0;i1 and produces * (x,y)=(gauss_alpha,gauss_gamma) and an m=gauss_m such that ax^2+bxy+cy^2=m, * gcd(m,n)=1. See L.-K. Hua 'Introduction to number theory', 311-312. */ define gauss(a,b,c,n){ auto absn,e,g,i,s,t,tmp1,tmp2,xx[],yy[] absn=abs(n) e=omega(absn) for(i=0;i0,d>0,a>0. * We first solve the congruence b.theta^2+c.theta+d=0 (mod a). * We let t=theta.u-ay to get p.u^2+quy+ry^2=1, where p=(b.theta^2+c.theta+d)/a, * q=-(2b.theta+c) and r=ab. We find the reduced form (global_a,global_b,global_c) * which is equivalent to (p,q,r). If global_a>1, we continue the loop. * If global_a=1, denote the unimodular transformation employed by * u=a11.X+a12.Y, y=a21.X+a22.Y. we get two solutions (u,y)=(a11,a21) and (-a11,-a21). * If global_a=global_c, we get two more solutions (u,y)=(a12,a22) and (-a12,-a22). * Then we get corresponding solutions (t,u), where t=theta.u-ay. * The number of primitive solutions is returned. * printflag=1 prints out the intermediate results. * See http://www.numbertheory.org/pdfs/posrep.pdf. */ define posrep0(b,c,d,a,printflag){ auto theta,p,q,r,q0,k,tt,u,y,solnumber,t,s,denomp s=quadratic(b,c,d,a,0) /* s=0 means no solutions of bx^2+cx+d=0 (mod a) */ /* If s>0, we get all solutions x, 0<=x1){ continue }else{ u=globala11 y=globala21 tt=theta*u-a*y if(printflag){ print "solution0[",solnumber,"]: (t,u)=(",tt,",",u,")\n" } posrep_solutionx0[solnumber]=tt posrep_solutiony0[solnumber]=u solnumber=solnumber+1 if(printflag){ print "solution0[",solnumber,"]: (t,u)=(",-tt,",",-u,")\n" } posrep_solutionx0[solnumber]=-tt posrep_solutiony0[solnumber]=-u solnumber=solnumber+1 if(global_a==global_c){ u=globala12 y=globala22 tt=theta*u-a*y if(printflag){ print "solution0[",solnumber,"]: (t,u)=(",tt,",",u,")\n" } posrep_solutionx0[solnumber]=tt posrep_solutiony0[solnumber]=u solnumber=solnumber+1 if(printflag){ print "solution0[",solnumber,"]: (t,u)=(",-tt,",",-u,")\n" } posrep_solutionx0[solnumber]=-tt posrep_solutiony0[solnumber]=-u solnumber=solnumber+1 if(global_b==1){ u=globala11-globala12 y=globala21-globala22 tt=theta*u-a*y if(printflag){ print "solution0[",solnumber,"]: (t,u)=(",tt,",",u,")\n" } posrep_solutionx0[solnumber]=tt posrep_solutiony0[solnumber]=u solnumber=solnumber+1 if(printflag){ print "solution0[",solnumber,"]: (t,u)=(",-tt,",",-u,")\n" } posrep_solutionx0[solnumber]=-tt posrep_solutiony0[solnumber]=-u solnumber=solnumber+1 } } } } return(solnumber) } /* This finds all solutions (primitive and imprimitive) of ax^2+bxy+cy^2=n as a global array. */ /* b^2-4ac<0, a>0,n>0. */ define posrepgen(a,b,c,n){ auto absn,t,g,countpatzgen,temp,h,f if(a<0){ a=-a;b=-b;c=-c;n=-n; } if(n<0){ print "no solutions\n" return(0) } if(n==0){ print "one solution (x,y)=(0,0)\n" globalposrepx[0]=0 globalposrepy[0]=0 return(1) } absn=abs(n) if(absn!=1){ t=omega(absn) g=divisors(qglobal[],kglobal[],t) }else{ g=1 divisor[0]=1 } countpatzgen=0 for(f=0;f0){ for(h=0;h0, n>0. */ define posrepgenlist(a,b,c,n){ auto h,g g=posrepgen(a,b,c,n) for(h=0;h0, b^2-4ac<0. * We use the transformation of Hua's book, p.278. */ define ssw1(a,b,c,d,e,f){ auto dd,gcount,k,u,v,h,r,s,g,kk if(a==0){ print "a=0\n" return(-1) } gcount=0 dd=b^2-4*a*c if(dd>0){ print "D>0\n" return(-1) } if(dd==0){ print "D=0\n" return(-1) } print "D=",dd,"\n" u=b*e-2*c*d v=b*d-2*a*e k=-(a*u^2+b*u*v+c*v^2-d*dd*u-e*dd*v+f*dd^2) kk=-dd*(a*e^2-b*e*d+c*d^2+f*dd) print "k=",k,"\n" print "kk=",kk,"\n" if(k==0){ print "k=0\n" r=-u s=-v if(r%dd==0 && s%dd==0){ print "(x,y)=(",r/dd,",",s/dd,")\n" gcount=1 } return(1) } print dd,"x=X-(",u,")," print dd,"y=Y-(",v,")\n" print "Find the solutions of ",a,"X^2+",b,"XY+",c,"Y^2=",k,"\n" g=posrepgen(a,b,c,k) print "There are ",g," solutions (X,Y)\n" for(h=0;h