Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 calculating an inverse multiplicative modulus in java...  (Read 656 times) 0 Members and 1 Guest are viewing this topic.
lcass
 « Posted 2014-08-21 23:14:07 »

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)
Roquen
 « Reply #1 - Posted 2014-08-22 07:56:42 »

What's with matrix inversion questions...what do you really want to do?  What's the field of the matrix elements?
pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « Reply #2 - Posted 2014-08-22 09:25:24 »

@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.
 Games published by our own members! Check 'em out!
lcass
 « Reply #3 - Posted 2014-08-22 09:28:18 »

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.
lcass
 « Reply #4 - Posted 2014-08-22 09:28:55 »

@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.
Thanks for that info , yes I have made a prime number generator.
Roquen
 « Reply #5 - Posted 2014-08-22 09:47:44 »

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
Roquen
 « Reply #6 - Posted 2014-08-22 09:51:09 »

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

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « Reply #7 - Posted 2014-08-22 11:07:46 »

Bad me. There's a modulo inverse for all odd integers.
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.

Thanks for that info , yes I have made a prime number generator.
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.
Roquen
 « Reply #8 - Posted 2014-08-22 11:39:19 »

Indeed it's mod 2^32.  I should update the docs.
lcass
 « Reply #9 - Posted 2014-08-22 11:45:49 »

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;
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 theagentd (28 views) 2015-05-27 22:21:17 theagentd (34 views) 2015-05-27 22:21:08 CopyableCougar4 (24 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (32 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21 BurntPizza (79 views) 2015-05-10 15:53:18 FrozenShade (64 views) 2015-05-07 09:11:21 TheLopais (228 views) 2015-05-06 13:36:48
 Rayvolution 24x Spasi 21x Riven 16x theagentd 15x Drenius 15x BurntPizza 15x ra4king 13x opiop65 12x EgonOlsen 12x princec 12x DavidBVal 11x Husk 11x orangepascal 11x KevinWorkman 10x scanevaro 8x kevglass 8x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org