Skip to content
Advertisement

Diophantine Equation

that takes a number (n) as an argument and returns tuple of four numbers which are; total number of packages, the number of packages of 6 nuggets, the number of packages of 9 nuggets and the number of packages of 20 nuggets that are needed to sell n number of nuggets. If the combination of nuggets cannot be made then it returns a tuple of four zeros i.e. (0,0,0,0).

Note that there can be multiple solutions for a given n, then your solution should ensure that the smaller packages are used before the larger packages. For example, buy_nuggets(18) should return (3,3,0,0) instead of (2,0,2,0), that is 3 boxes of 6 piece nuggets over 2 boxes of nine piece.

This function has input Format Integer (n) and restrictions -10^6<=a,b,c,n<=10^6

The output format would be a tuple of 4 numbers (d,a,b,c) where

d = total number of packages
a – number of packages of 6
b – number of packages of 9
c – number of packages of 20

Any help would be great, thank you.

def nugget_boxes(n): 
    def extended_nuggets(m,n):
        assert m>=n and n>=0 and m+n>0
        if n==0:
            d,x,y= m,1,0
        else:
            (d,p,q)=extended_gcd(n,m%n)
            x=q
            y=p-x*(m//n)
        assert m%d==0 and n%d==0
        assert d==m*x + n*y
        return(d,x,y)
    
    def diophantine(a,b,c,d):
        if a>b and c and d:
            q=extended_nuggets(a,b,c,d)
            a1=q[1]
            b1=q[2]
            c1=q[3]
            d1=q[4]
        if b>a and c and d:
            q=extended_nuggets(a,b,c,d)
            a1=q[2]
            b1=q[1]
            c1=q[3]
            d1=q[4]
        if c>a and b and d:
            q=extended_nuggets(a,b,c,d)
            a1=q[3]
            b1=q[1]
            c1=q[2]
            d1=q[4]
            
        else:
            q=extended_nuggets(a,b,c,d)
            a1=q[4]
            b1=q[1]
            c1=q[2]
            d1=q[3]
        assert c%q[0]==0
        d=q[0]
        p=c/d
        return nugget_boxes(int(p*x1),int(p*y1), int(p*z1))

This function returns nothing, I could not find anything on the website that would help

Advertisement

Answer

I don’t know if i’m right, i think you need to call the function to see the output?

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement