I made this simple program that can solve every set of linear equations in two variables.
You just need to enter two linear equations of the form ax+by=c (without any unnecessary spaces) where 'a' is a positive integer (i.e. x should not have a negative coefficient) and b and c are just integers.
The program can also show outputs like "The two equations have no solution" or "the two equations have infinite solutions" if it is so...
I only used the knowledge of 9th and 10th grade Python along with the concept of functions to make this program and I will update it on the basis of the suggestions that I got from some great people in my last question. I will try to make it shorter, more readable and definitely more efficient soon, will post that too when done.
Here's the Python code :
def getindex(a,b):
size = len(b)
status = 0
for i in range(0,size,1):
element = b[i]
if (element == a):
index = i
status = 1
break
if (status == 1):
return index
else:
return "not found"
def getstring(a,b,c):
string = c[a]
for i in range(a+1,b,1):
element = c[i]
string = string + element
return string
def lcm(a,b):
found = False
if (a>b):
larger = a
else:
larger = b
i = larger
while found == False:
if (i%a == 0) and (i%b == 0):
found = True
break
else:
i += larger
return i
print ()
print ("Please enter two equations of the form ax+by=c where a is a positive integer and b and c are integers...")
print ()
a = str(input("Please enter the first equation : "))
b = str(input("Please enter the second equation : "))
a = list(a)
b = list(b)
sizea = len(a)
sizeb = len(b)
# Getting a, b and c of equation one
symbolindexone = getindex('+',a)
equalindexone = getindex('=',a)
symbolstatusone = "positive"
if (symbolindexone == "not found"):
symbolindexone = getindex('-',a)
symbolstatusone = "negative"
xindexone = getindex('x',a)
yindexone = getindex('y',a)
a1 = getstring(0,xindexone,a)
if (a1 == 'x'):
a1 = 1
else:
a1 = int(a1)
b1 = getstring(symbolindexone+1,yindexone,a)
if (b1 == 'y'):
if (symbolstatusone == 'positive'):
b1 = 1
else:
b1 = -1
else:
b1 = int(b1)
if (symbolstatusone == "negative"):
b1 = -b1
c1 = getstring(equalindexone+1,sizea,a)
c1 = int(c1)
# getting a, b and c of equation two
symbolindextwo = getindex('+',b)
equalindextwo = getindex('=',b)
symbolstatustwo = 'positive'
if (symbolindextwo == 'not found'):
symbolindextwo = getindex('-',b)
symbolstatustwo = 'negative'
xindextwo = getindex('x',b)
yindextwo = getindex('y',b)
a2 = getstring(0,xindextwo,b)
if (a2 == 'x'):
a2 = 1
else:
a2 = int(a2)
b2 = getstring(symbolindextwo+1,yindextwo,b)
if (b2 == 'y'):
if (symbolstatustwo == 'positive'):
b2 = 1
else:
b2 = -1
else:
b2 = int(b2)
if (symbolstatustwo == 'negative'):
b2 = -b2
c2 = getstring(equalindextwo+1,sizeb,b)
c2 = int(c2)
# LCM and the final phase
lcma = lcm(a1,a2)
tmbone = lcma/a1
tmbtwo = lcma/a2
a1 *= tmbone
b1 *= tmbone
c1 *= tmbone
a2 *= tmbtwo
b2 *= tmbtwo
c2 *= tmbtwo
ratioa = a1/a2
ratiob = b1/b2
ratioc = c1/c2
print ()
if (ratioa == ratiob == ratioc):
print ("The equations that you entered have infinite solutions...")
elif ((ratioa == ratiob) and (ratioa != ratioc)):
print ("The equations that you entered have no solution...")
else:
ysolution = (c1-c2)/(b1-b2)
xsolution = ((c1)-(b1*ysolution))/(a1)
print ("The value of x is : ",xsolution)
print ("The value of y is : ",ysolution)
Let me know what you think
Thanks :)
get_indexandgetstring\$\endgroup\$