How to solve diophantine problems?

Emile van Sebille emile at fenx.com
Mon May 13 19:18:11 EDT 2002


Joshua, Y.J. Lai
> I can roughly solve the diophantine problem by using a nest loop

I'm not familiar with the "diophantine problem" and didn't, in a quick
look, spot anything obvious to me stating it.

> ex.
> def td(x):
>     "The number of balls used to construct a tetrahedron"
>     return x*(x+1)*(x+2)/6
>
> def tri(y):
>     "The number of balls used to construct a triangle"
>     return y*(y+1)/2
>
> for x in xrange(100):
>     for y in xrange(100):
>         a=td(x)
>         b=tri(y)
>         if a==b!=0:
>             print "x = %d and y = %d , number = %d" % (x,y,a)
> ----------------------------------------------------------------------
------
> -------
> But if I want to limit the maximum amount of balls (k) in this
question
> then I will add an additional line:
> k=input("Please define the Max of balls: ")

you'll want this to be raw_input

> However, I do not know how to write the checking loops in this case?
> Because, the x and y are uncertain now. Could anyone please kindly
help me.

I think you want to limit the sum of a and b to k?  Does this do it?:

        if a and a == b and a+b <= k:
            print "x = %d and y = %d , number = %d" % (x,y,a)

HTH,

Emile van Sebille
emile at fenx.com






More information about the Python-list mailing list