I have a program that takes user input in the form of an integer, lets call it k. Three other numbers are known, a,b and c. My task is to find all positive integer solutions {x,y,z} such that ax + by + cz = k. I wrote a method that I call on objects that have a,b and c built in. There is an additional constraint that says that x + y + z cannot be greater than a known integer p.
def find_all_solutions(self, k):
for x in range(0, k/c +1):
for y in range(0, k/c +1):
for z in range(0,k/c +1):
if x+y+z <= self.p and self.a*x+self.b*y+self.c*z == k:
one_solution = [x,y,z]
list_of_combinations.insert(END,"x: {0}, y: {1}, z: {2} ".format(one_solution[0], one_solution[1], one_solution[2]))
K = IntVar()
KassaBox= Entry(TeaterGUI, relief=GROOVE,textvariable=Kassa,width="15")
KassaBox.place(x="400",y="240")
KombinationsKnapp = Button(TeaterGUI, text="Tryck har for att visa alla mojliga kombinationer", command= lambda: TeaterLista[Teater_Index.get()].find_all_solutions(K.get()))
KombinationsKnapp.place(x="400",y="260")
This works when k is somewhat small (<100000), although as it exceeds 3 digits the interpreter freezes for a couple of seconds as it does its calculations, but eventually it does what its supposed to.
My problem is that if k is larger, the amount of combinations that have to be checked becomes too many to handle for the python interpreter.
So I was thinking that maybe a way of avoiding these crashes is to, instead of the program finding all solutions and adding them all at once, to make the program find each solution and add it one-by-one to the listbox, so as to avoid the computer storing to much information in its RAM before its used. However, with tkinters .insert method seeming like the only way of appending the listbox with info, I don't know how to do this.
Any help would be greatly appreciated!