Modulus of a fraction
조회 수: 7 (최근 30일)
이전 댓글 표시
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
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
2020년 10월 1일
@Walter Roberson: mod(a/x,y) is possible only when x and y is relatively prime.
답변 (2개)
Tanveer ul haq
2020년 10월 1일
Download both the .m files and the run "fraction_modulo.m" to get your desired modulus.
댓글 수: 0
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!