Author Topic: AX + BY = C  (Read 2550 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
AX + BY = C
« on: August 17, 2018, 12:42:30 am »
For math fans:
Code: QB64: [Select]
  1. _TITLE "Ax + By = C"
  2. SCREEN _NEWIMAGE(1000, 600, 32)
  3.  
  4. ' Thanks BSpinoza, orig ref: http://jean-pierre.moreau.pagesperso-orange.fr/Basic/diophan_bas.txt
  5. ' I am curious about the technique so I study while I translate to QB64.
  6. ' 2018-08-16 OK seems to work now. But you sort of need a routine to translate the results of
  7. ' Diophantian  x0, y0, p, q into equations for x, y when a solution is found.
  8.  
  9. '**********************************************
  10. '* Solving a diophantian equation ax+by = c   *
  11. '*      (a,b,c,x,y are integer numbers)       *
  12. '* ------------------------------------------ *
  13. '* Ref.: "Mathematiques en Turbo-Pascal By    *
  14. '*        M. Ducamp and A. Reverchon (2),     *
  15. '*        Eyrolles, Paris, 1988" [BIBLI 05].  *
  16. '* ------------------------------------------ *
  17. '* Sample run:                                *
  18. '*                                            *
  19. '* SOLVING IN Z EQUATION AX + BY = C          *
  20. '*                                            *
  21. '*    A = 3                                   *
  22. '*    B = -2                                  *
  23. '*    C = 7                                   *
  24. '*                                            *
  25. '* Solutions are:                             *
  26. '*                                            *
  27. '*   X =    1 +     2*K                       *
  28. '*   Y =   -2 +     3*K                       *
  29. '*                                            *
  30. '*              BASIC version by J-P Moreau.  *
  31. '*                   (www.jpmoreau.fr)        *
  32. '**********************************************
  33.  
  34.  
  35. DEFINT A-Z
  36.  
  37.  
  38.     CLS
  39.     PRINT
  40.     PRINT " SOLVING EQUATION A*X + B*Y = C"
  41.     PRINT
  42.     INPUT "   A = ", a
  43.     INPUT "   B = ", b
  44.     INPUT "   C = ", c
  45.     IF a = 0 OR b = 0 THEN END
  46.     solnFound = FALSE: x0 = 0: y0 = 0: p = 0: q = 0
  47.     Diophantian a, b, c, x0, y0, p, q, solnFound
  48.  
  49.     'now to translate these numbers!
  50.     IF p * q > 0 THEN s$ = "-": m = -1 ELSE s$ = "+": m = 1
  51.  
  52.     IF solnFound = -1 THEN
  53.         PRINT
  54.         PRINT " Solutions are:"
  55.         PRINT
  56.         PRINT "   X = "; x0; " + "; ABS(q); " * K"
  57.         PRINT "   Y = "; y0; " "; s$; " "; ABS(p); " * K"
  58.         PRINT: PRINT " Check: Plug-in 10 integers for K:": PRINT
  59.         FOR k = -5 TO 5
  60.             x = x0 + ABS(q) * k
  61.             y = y0 + m * ABS(p) * k
  62.             PRINT " For K = "; k;
  63.             LOCATE CSRLIN, 20: PRINT " X, Y is "; x; " , "; y;
  64.             LOCATE CSRLIN, 50: PRINT a; " * "; x; " + "; b; " * "; y; " = "; a * x + b * y,
  65.             LOCATE CSRLIN, 85: IF a * x + b * y = c THEN PRINT " Yes, C = "; c ELSE PRINT " No, C = "; c
  66.         NEXT
  67.     ELSE
  68.         PRINT " No solutions."
  69.     END IF
  70.     PRINT: PRINT "Press spacebar to go again, escape to quit."
  71.     H& = _KEYHIT
  72.     WHILE H& <> -32
  73.         H& = _KEYHIT
  74.         IF _KEYDOWN(27) THEN END
  75.         _LIMIT 100
  76.     WEND
  77.  
  78. '*************************************************************************************************
  79. '* Solving equation a*x + b*y = c, a, b, c, x, y are integer numbers
  80. '* ------------------------------------------------------- ---------------------------------------
  81. '* INPUT:   a,b,c  coefficients of equation   note: these numbers won't be changed by sub
  82. '*          x0, y0, p, q, solutionFound all zero'd for output
  83. '* OUTPUT:  solutions are x0+kp and y0-kq, with k=0,1,2...
  84. '*          or k=-1,-2,-3...
  85. '* The solutionFound returns -1 if solutions exist ie if the GCD of a,b is also a divisor of c.
  86. '*****************************************************************************************************
  87. SUB Diophantian (aa, bb, cc, x0, y0, p, q, solutionFound)
  88.     solutionFound = 0
  89.     IF aa = 0 THEN EXIT SUB: IF bb = 0 THEN EXIT SUB
  90.     a = aa: b = bb: c = cc 'make copies as these will be changed
  91.     pg = GCD(a, b)
  92.     a = a / pg: b = b / pg: c! = c / pg
  93.     IF c! <> INT(c!) THEN EXIT SUB ELSE c = INT(c!) 'pg must be also a divisor of c
  94.     x1 = 0: y2 = 0
  95.     ifound = 0
  96.     WHILE ifound = 0
  97.         y1! = (c - a * x1) / b
  98.         IF y1! <> INT(y1!) THEN
  99.             x1 = -1 * x1: IF x1 >= 0 THEN x1 = x1 + 1
  100.             x2! = (c - b * y2) / a
  101.             IF x2! <> INT(x2!) THEN
  102.                 y2 = -1 * y2: IF y2 >= 0 THEN y2 = y2 + 1
  103.             ELSE
  104.                 x0 = INT(x2!): y0 = y2: ifound = -1
  105.             END IF
  106.         ELSE
  107.             x0 = x1: y0 = INT(y1!)
  108.             ifound = -1
  109.         END IF
  110.     WEND
  111.     p = a: q = b
  112.     solutionFound = -1
  113.  
  114. FUNCTION GCD (a, b)
  115.     c = ABS(a): d = ABS(b) 'make copies as these will be changed
  116.     IF c = 0 OR d = 0 THEN GCD = -1: EXIT FUNCTION 'signal error?
  117.     WHILE c <> 0 AND d <> 0
  118.         IF c > d THEN c = c MOD d ELSE d = d MOD c
  119.     WEND
  120.     GCD = c + d
  121.  
« Last Edit: August 17, 2018, 12:48:06 am by bplus »