[OT] Reciprocal function?

Anton Vredegoor anton at vredegoor.doge.nl
Thu Nov 21 07:54:03 EST 2002


Hi All,

Suppose I have these functions:

def rowinc(x,base):
    i,j = 0,1
    while i <= x:
        j = j * base
        i = i + j
    return j

def concat(a,b,base):
    f = rowinc(b,base)
    return f*(a+1)+b

And, for the moment, lets assume "base" is fixed to the value 2.

Then concat(9,5,2) will return 45.
But concat(3,13,2) will also return 45.

In fact if the number that concat returns is known, and the "base"
argument is also known, the function below can be used to calculate
how many other tuples (a,b) will have the same result:
        
def seqlen(x,base):
    i,j,k = 0,1,0
    while i <= x:
        j = j * base
        i = i + j
        k = k + 1
    return k

So seqlen(45,2) returns 5. The number of possible tuples (a,b) that
make concat(a,b,2) return 45 is seqlen(45,2)-1 which is 4.

As an example:

For (a,b) in [(0, 29), (3, 13), (9, 5), (21, 1)], concat(a,b,2)
returns 45 and there are no other tuples (a,b) that return 45 for
concat(a,b,2).

Now I need a function that looks a bit like this:

def splitat(i,a,base):
	"""
	returns the i'th tuple (x,y) out of the set of possible
	tuples that make concat(x,y,base) return a
	"""
    return x,y

Any pointers appreciated. At the moment I have some way of getting the
desired output, but the detour the code makes to get at it is so big
that I'd rather have some fresh input instead of posting the code
here.

Regards,

		Anton.




More information about the Python-list mailing list