calculating an inverse multiplicative modulus in java...

Yer I have the kind of feeling this is going to go well or be very painful.
So I cant find any detailed example that explains this well , if someone would be able to just show me how it works with an example of code that calculates for example d = inverse multiplicate modulus of x(mod d)

What’s with matrix inversion questions…what do you really want to do? What’s the field of the matrix elements?

@Roquen, this isn’t about matrices.

@lcass, are you guaranteed that d is a prime? If not, the inverse may not exist. If it does exist, it can be found efficiently with the extended Euclidean algorithm. That page has a section on multiplicative inverses.

Modulus inversion , not matrix inversion , So for instance if I have something of the form ax = 1 (mod(n)) how do I solve for x (in an algorithm) I know you turn it to the form of a fraction by performing a^-1 = x (mod(n)) then you find a value of x that works , however running through every value of x (for values in the long size) is difficult.

Thanks for that info , yes I have made a prime number generator.

Bad me. There’s a modulo inverse for all odd integers. This is newton’s method instead: https://github.com/roquendm/JGO-Grabbag/blob/master/src/roquen/math/Int32.java

I think I need to go have a drink or something. Trouble with reading comprehension.

No, there’s a modulo inverse for all numbers which are coprime with the modulus. Hence the link with Euclid’s algorithm for finding gcd. (I’m not sure whether you realised this and that’s why the bit about needing a drink, but even if you have it might be helpful for other people reading the thread).

I think your linked code is calculating inverses modulo 2^32 for odd integers, but the Javadoc isn’t clear enough for me.

I should add that in general you can use Euler’s theorem, and for primes that’s Fermat’s little theorem: a^(p-1) == 1 (mod p) provided that a is not a multiple of p. So if you already have a modular exponentiation function and you don’t have an extended Euclid function, that’s an alternative approach. I think they should have the same asymptotic complexity.

Indeed it’s mod 2^32. I should update the docs.

I use a nifty little function for generating long primenumbers , I am still finding it really difficult to understand how inverse modulus works so if I plonk this down would someone be able to give an explanation of it?
So say i need to calculate D , D = inverse multiplicate modulus of e % n; or d = e^-1 %n;