Modulus of a fraction

9 次查看(过去 30 天)
Thomas Kinnari
Thomas Kinnari 2012-1-3
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 个评论
Walter Roberson
Walter Roberson 2012-1-3
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 2020-10-1
@Walter Roberson: mod(a/x,y) is possible only when x and y is relatively prime.

请先登录,再进行评论。

回答(2 个)

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

John D'Errico
John D'Errico 2020-10-1
编辑:John D'Errico 2020-10-1
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.

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by