Abt. Ganzzahlige (Diophantische) Gleichungen schneller lösen
======================================
Erinnert sich noch jemand an das Hunde-Katzen-Ratten-Rätsel (Onkel Ottos Tierhandlung)? Damals habe ich eine reine BruteForce-Methode zun lösen vorgeschlagen, was natürlich unelegant ist. Viel eleganter ist die nachstehende Methode zwar nicht, aber dafür durch vorherige formelmäßige Ermittlung der Schnittmenge, in der die Lösung(en) liegen muß/müssen, deutlich schneller:
Code
WindowTitle "Lineare Diophantische Gleichung in 3 Variablen rascher lösen"
'(CL) Copyleft 2013-05 by P. Specht, ohne jede Gewähr!
Window %maxy/2,0-%maxy,%maxy-40:font 0:set("decimals",17):randomize:cls rnd(8^8)
Declare a$,bl!,b!,l!,dn!,d!,n!,fo!,f!,o!,ep!,e!,p!,Y!,q!,g!,k!,a!,X!,m!,c!,Z!,nenn!,nenn2!
a!=1:b!=1:c!=1:d!=1:e!=1:f!=1:g!=100 : k!=10:l!=1:m!=3:n!=1:o!=1:p!=2: q!=100
' Default-Eingabe: Das Hunde 10 €, Katzen 3 €, Ratten 1/2 €-Problem: 100 Tiere um 100 Euro?
Print " "
Print " Zwei allgemeine Ebenengleichungen liefern eine Schnittgerade, "
Print " nur die wird dann BruteForce auf ganzzahlige Lösungen geprüft."
rept:
Print " ------------------------------------------------------------- "
Print " Universelles Eingabeschema (Abschnittsvielfach-Form) "
Print " I: a / b * X + c / d * Y + e / f * Z = g "
Print " II: k / l * X + m / n * Y + o / p * Z = q "
Print " ------------------------------------------------------------- "
Print " Y = ((epq+fgo)*bldn - (bekp-aflo)*dn*X )/(bl*(demp-cfno)) "
Print " ------------------------------------------------------------- "
Print " Ebene 1: "
Print " a = ";format$("%g",a!):locate %csrlin-1,6:input a$:if a$>"":a!=val(a$):endif
Print " b = ";format$("%g",b!):locate %csrlin-1,6:input a$:if a$>"":b!=val(a$):endif
Print " c = ";format$("%g",c!):locate %csrlin-1,6:input a$:if a$>"":c!=val(a$):endif
Print " d = ";format$("%g",d!):locate %csrlin-1,6:input a$:if a$>"":d!=val(a$):endif
Print " e = ";format$("%g",e!):locate %csrlin-1,6:input a$:if a$>"":e!=val(a$):endif
Print " f = ";format$("%g",f!):locate %csrlin-1,6:input a$:if a$>"":f!=val(a$):endif
Print
Print " g = ";format$("%g",g!):locate %csrlin-1,6:input a$:if a$>"":g!=val(a$):endif
Print " ------------------------------------------------------------- "
Print " Ebene 2: "
Print " k = ";format$("%g",k!):locate %csrlin-1,6:input a$:if a$>"":k!=val(a$):endif
Print " l = ";format$("%g",l!):locate %csrlin-1,6:input a$:if a$>"":l!=val(a$):endif
Print " m = ";format$("%g",m!):locate %csrlin-1,6:input a$:if a$>"":m!=val(a$):endif
Print " n = ";format$("%g",n!):locate %csrlin-1,6:input a$:if a$>"":n!=val(a$):endif
Print " o = ";format$("%g",o!):locate %csrlin-1,6:input a$:if a$>"":o!=val(a$):endif
Print " p = ";format$("%g",p!):locate %csrlin-1,6:input a$:if a$>"":p!=val(a$):endif
Print
Print " q = ";format$("%g",q!):locate %csrlin-1,6:input a$:if a$>"":q!=val(a$):endif
Print " ------------------------------------------------------------- "
Print " Lösungsmenge [x,y,z]: "
bl!=b!*l!:dn!=d!*n!:fo!=f!*o!:ep!=e!*p!
y!=diophant()
print "\n ENDE."
beep
waitinput
cls rnd(8^8)
goto "rept"
proc diophant
x!=1
repeat
y!=fff(x!)
if (y!=int(y!)) and (y!>=0)
nenn2!=b!*d!*e!
if nenn2!<>0
z!= -(b!*c!*f!*Y! + a!*d!*f!*X!-b!*d!*f!*g!)/nenn2!
if (z!=int(z!)) and (z!>=0)
set("decimals",0)
locate %csrlin,30;:print x!,y!,z!
set("decimals",17)
endif
endif
endif
x!=x!+1
until y!<0:return
endproc
proc fff :parameters x!
nenn! = bl!*(d!*ep!*m!-c!*fo!*n!)
if nenn!<>0
y!=(bl!*dn!*(ep!*q!+fo!*g!)-(b!*ep!*k!-a!*fo!*l!)*dn!*X!) / nenn!
else
y!=-999999999999.4
print " Unbestimmt ";
endif
return y!
endproc
Alles anzeigen
P.S: Das universelle Inputschema erlaubt die Eingabe gemäß der unterschiedlichen kursierenden Rätseltexte, z.B. in Abschnittsform der Ebenengleichung x/a+y/b+z/c=1.