Modulus of a fraction

4 visualizzazioni (ultimi 30 giorni)
Thomas Kinnari
Thomas Kinnari il 3 Gen 2012
Modificato: John D'Errico il 1 Ott 2020
How do I calculate the modulus of a fraction? For example if I want to calculate mod(33/4,5) -- I know the result is 2, because 33/4 = 132/16 and when I calculate modulus 5 that gives me 2/1=2. Can anyone please help me?
Cheers!
  4 Commenti
Walter Roberson
Walter Roberson il 3 Gen 2012
I can see a certain logic in Thomas's calculation, but that logic would fail if the denominator and the modulus are not relatively prime.
Tanveer ul haq
Tanveer ul haq il 1 Ott 2020
@Walter Roberson: mod(a/x,y) is possible only when x and y is relatively prime.

Accedi per commentare.

Risposte (2)

Tanveer ul haq
Tanveer ul haq il 1 Ott 2020
Download both the .m files and the run "fraction_modulo.m" to get your desired modulus.

John D'Errico
John D'Errico il 1 Ott 2020
Modificato: John D'Errico il 1 Ott 2020
I assume you are talking about the ring of integers modulo 5. In that case, 33/4 can be thought af as 33 times the mulitplicative inverse of 4, in that ring.
As long as the modulus n is prime, then all numbers from 1 to n-1 have a multiplicative inverse. (If the modulus is composite, then SOME numbers will still have a multiplicative inverse, though many will not.) 4 for example is its own inverse modulo 5, because we see that
mod(4*4,5)
ans =
1
And therefore we can compute what you want using code I've posted on the file echange in my VPI toolbox.
inv4 = minv(4,5)
inv4 =
4
And now we see the result as 2, as you expected.
mod(33*inv4,5)
ans =
2
Do you need minv? Well, no. You can use gcd. As long as 4 and 5 are relatively prime, 4 has a multiplicative inverse in the generated ring of integers modulo 5. We can both test that fact, and get the inverse directly from gcd.
[G,C] = gcd(4,5)
G =
1
C =
-1
Q will always be 1 as long as it is true they are relatively prime. C gives you the inverse, as
inv4 = mod(C,5)
inv4 =
4
So there is no need for any special tools. Not even my own minv utility.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by