' Thanks BSpinoza, orig ref: http://jean-pierre.moreau.pagesperso-orange.fr/Basic/diophan_bas.txt
' I am curious about the technique so I study while I translate to QB64.
' 2018-08-16 OK seems to work now. But you sort of need a routine to translate the results of
' Diophantian x0, y0, p, q into equations for x, y when a solution is found.
'**********************************************
'* Solving a diophantian equation ax+by = c *
'* (a,b,c,x,y are integer numbers) *
'* ------------------------------------------ *
'* Ref.: "Mathematiques en Turbo-Pascal By *
'* M. Ducamp and A. Reverchon (2), *
'* Eyrolles, Paris, 1988" [BIBLI 05]. *
'* ------------------------------------------ *
'* Sample run: *
'* *
'* SOLVING IN Z EQUATION AX + BY = C *
'* *
'* A = 3 *
'* B = -2 *
'* C = 7 *
'* *
'* Solutions are: *
'* *
'* X = 1 + 2*K *
'* Y = -2 + 3*K *
'* *
'* BASIC version by J-P Moreau. *
'* (www.jpmoreau.fr) *
'**********************************************
PRINT " SOLVING EQUATION A*X + B*Y = C" solnFound = FALSE: x0 = 0: y0 = 0: p = 0: q = 0
Diophantian a, b, c, x0, y0, p, q, solnFound
'now to translate these numbers!
IF p
* q
> 0 THEN s$
= "-": m
= -1 ELSE s$
= "+": m
= 1
PRINT " X = "; x0;
" + ";
ABS(q
);
" * K" PRINT " Y = "; y0;
" "; s$;
" ";
ABS(p
);
" * K" PRINT:
PRINT "Press spacebar to go again, escape to quit."
'*************************************************************************************************
'* Solving equation a*x + b*y = c, a, b, c, x, y are integer numbers
'* ------------------------------------------------------- ---------------------------------------
'* INPUT: a,b,c coefficients of equation note: these numbers won't be changed by sub
'* x0, y0, p, q, solutionFound all zero'd for output
'* OUTPUT: solutions are x0+kp and y0-kq, with k=0,1,2...
'* or k=-1,-2,-3...
'* The solutionFound returns -1 if solutions exist ie if the GCD of a,b is also a divisor of c.
'*****************************************************************************************************
SUB Diophantian
(aa
, bb
, cc
, x0
, y0
, p
, q
, solutionFound
) solutionFound = 0
a = aa: b = bb: c = cc 'make copies as these will be changed
pg = GCD(a, b)
a = a / pg: b = b / pg: c! = c / pg
x1 = 0: y2 = 0
ifound = 0
y1! = (c - a * x1) / b
x1
= -1 * x1:
IF x1
>= 0 THEN x1
= x1
+ 1 x2! = (c - b * y2) / a
y2
= -1 * y2:
IF y2
>= 0 THEN y2
= y2
+ 1 x0
= INT(x2!
): y0
= y2: ifound
= -1 ifound = -1
p = a: q = b
solutionFound = -1
c
= ABS(a
): d
= ABS(b
) 'make copies as these will be changed GCD = c + d