The function worked when tested with base-10 because all of your conversions int("{0:b}".format(x))
have no effect on x:
37 == int("{0:b}".format(37), 2) # >>> True
Number objects in python are all base-10. Converting your numbers to binary strings, then back to integers has no effect. Here is an alternative version of your function that should work on a
and b
as base-10 ints and return them in binary. You can remove the bin()
function to return the numbers in base-10, or use something like lambda x: int("%d".format(x))
to convert a
and b
from binary to decimal in the first line of the function.
def extendedEuclideanGF2(a, b): # extended euclidean. a,b are values 10110011... in integer form
inita, initb = a, b # if a and b are given as base-10 ints
x, prevx = 0, 1
y, prevy = 1, 0
while b != 0:
q = a//b
a, b = b, a%b
x, prevx = prevx - q*x, x
y, prevy = prevy - q*y, y
print("Euclidean %d * %d + %d * %d = %d" % (inita, prevx, initb, prevy, a))
i2b = lambda n: int("{0:b}".format(n)) # convert decimal number to a binary value in a decimal number
return i2b(a), i2b(prevx), i2b(prevy) # returns gcd of (a,b), and factors s and t
All that said, don't use lambdas in a function like this - I'd suggest writing your program to avoid using binary altogether, which you can do by only converting from/to binary at the interface of your program with source data.