Wrong results with "mod"

조회 수: 8 (최근 30일)
Matthew Durkin
Matthew Durkin 2019년 2월 10일
답변: John D'Errico 2019년 2월 10일
Why does Matlab give me the wrong answer to mod(14^27,55) = 32, it should equal 9. What am I doing wrong?
  댓글 수: 2
Matthew Durkin
Matthew Durkin 2019년 2월 10일
Never mind, I found out I need to use "powermod" to get the answer.
the cyclist
the cyclist 2019년 2월 10일
Maybe you should copy your comment to an answer, and accept it.

댓글을 달려면 로그인하십시오.

답변 (1개)

John D'Errico
John D'Errico 2019년 2월 10일
This fails because MATLAB cannot represent an integer larger than 2^53-1 as an exact integer, when working in double precision.
14^27 is far larger than that of course. So it fails. This leaves you with two basic options, to use symbolic arithmetic, (or perhaps my VPI toolbox) or to use powermod.
mod(sym(14)^27,55)
ans =
9
powermod(14,27,55)
ans =
9
The powermod solution is better of course, in the sense that the huge number 14^27 never had to be fully computed.

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by