[Tutor] programming exercise in Python
Kermit Rose
kermit at polaris.net
Wed Aug 9 16:46:59 CEST 2006
Thanks to John,
I've written tenative code for the incr routine.
Now to test and possibly debug it.
Here is the routine. It's very short.
#
#
# def incr(mult,zlim,mpylist):
# # mult is a vector of exponents for the multipliers in mpylist.
# # z is the integer to be factored
# # zlim is the critical value for the sum of multiplier * index
# # where kth multiplier is mpylist [k][0]
# # and kth index is mpylist [k][1]
#
# # function incr returns the next value of vector mult.
# # mult may be thought as a number written in a variable base.
# # mult[0] is the least significant and matches multiplier mpylist[0][0]
# # adding one to mult would mean adding 1 to mult[0]
# # unless doing so would make sum of mpylist[k][1] * mult[k] exceed zlim.
# # in that case mult[0] is set to zero, and 1 is added to mult[1]
# # unless doing so would make sum of multilier * index exceed zlim
# # in that case, mult[0] and mult[1] is set to zero,
# # and 1 is added to mult[2]
# # unless . . .
#
#
# # mult[0] is set to -1 to indicate that the largest possible value of mult
has been exceeded.
# # mpylist[0] = 2 and all other values in mpylist are odd.
# # mult[0], the index on multiplier 2, must not equal 1. It may equal zero
or any integer > 1,
# # provided it meets the zlim constraint.
#
# lenmult = len(mult)
# for k in range(lenmult):
# mult[k] = mult[k] + 1
# if k == 0:
# if mult[k] == 1:
# mult[k] = 2
# testsum = 0
# if k > 0:
# mult[k-1] = 0
# for j in range(k,lenmult):
# testsum = testsum + mpylist[j][1] * mult[j]
# if testsum < zlim:
# return mult
# mult[0] = -1
# return mult
#
#
More information about the Tutor
mailing list