Find all soultions for ax+by+cz=N

This is the place for queries that don't fit in any of the other categories.

Find all soultions for ax+by+cz=N

Postby älgkött » Thu Mar 14, 2013 1:56 pm

Hello,

In a previous post "Struggling with classes" I made a program that analyzed sales for a number of casinos. I have now expanded the program so that I can solve for all possible solutions of the form ax+by+cz=N
where a,b and c are my given entry fees for adults, child and elderly. N is my total revenues for each casino. I want to find all x,y,z combinations (non negative) for a given N.

This is what I got so far, but I'm kinda stuck because this only gives me the 0,0,0-solution

Any ideas how I should proceed?

Code: Select all
    def combinations(self):
        A=self.adult
        C=self.child
        E=self.elderly
        print(self.name,':',A,'*X +',C,'*Y +',E,'*Z =',self.revenueTot)

#gcd = greatest common divider

        GCD=gcd(self.revenueTot,E)
       
        for a in range(0,GCD):
            for b in range(0,GCD):
                for c in range(0,GCD):
                    solution=self.A*a+self.C*b+self.E*c
                    if solution==self.revenueTot:
                        print(a,b,c)
                    else:
                        return
älgkött
 
Posts: 6
Joined: Sun Feb 17, 2013 6:29 pm

Re: Find all soultions for ax+by+cz=N

Postby casevh » Fri Mar 15, 2013 4:52 am

I think your use of gcd() is incorrect. It is mostly likely 1, which means your range functions can only return 0.

The following example should get you started:

Code: Select all
def combinations(A, C, E, total):
    result = []
    for a in range(0, total//A):
        for c in range(0, total//C):
            for e in range(0, total//E):
                if A*a + C*c + E*e == total:
                    result.append((a,c,e))
    return result

if __name__ == "__main__":
    print(combinations(1,1,1,4))
    print(combinations(5,1,3,117))


casevh
casevh
 
Posts: 70
Joined: Sat Feb 09, 2013 7:35 am


Return to General Coding Help

Who is online

Users browsing this forum: l_mono, stranac and 2 guests