Given an odd long x
, I'm looking for long y
such that their product modulo 2**64
(i.e., using the normal overflowing arithmetic) equals to 1. To make clear what I mean: This could be computed in a few thousand year this way:
for (long y=1; ; y+=2) {
if (x*y == 1) return y;
}
I know that this can be solved quickly using the extended Euclidean algorithm, but it requires the ability to represent all the involved numbers (ranging up to 2**64
, so even unsigned arithmetic wouldn't help). Using BigInteger
would surely help, but I wonder if there's a simpler way, possibly using the extended Euclidean algorithm implemented for positive longs.