(a^b) mod c

hi all, i need your help. i want to use (a^b) mod c
but in matlab if i use mod((a^b),c);
it's return inf
because a,b,c are too big.
are you know how to do that with another way, in matlab?? for example use a loop ??

1 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 6 月 29 日
- What do you mean by too big?
- Are they scalars?

サインインしてコメントする。

回答 (2 件)

Jan
Jan 2011 年 6 月 29 日

1 投票

If b is not prime, but x * y:
(a^b) mod c = (a^x mod c)^y mod c
But if b is prime, this does not help to avoid the overflow. Then I'd try FEX:John's VPI or FEX:Ben Barrowes' Multiple Precision Toolbox.
EDITED: A loop method:
function r = ApowerBmodC(a, b, c)
r = 1;
for i = 1:b
r = mod(a * r, c);
end
Then "ApowerBmodC(1234, 5678, 1256)" replies 1112. For these numbers the "(a^x mod c)^y mod c" does not help, because 5678 = 2*17*167 and "mod(1234^2, 1256)^17" is 4.38e45 such that trailing figures are all zero due to rounding.

3 件のコメント

zikrullah muhammad
zikrullah muhammad 2011 年 6 月 29 日
thank you, but if b prime what must i do??
Jan
Jan 2011 年 6 月 29 日
Have you looked at the two posted links?
Sean de Wolski
Sean de Wolski 2011 年 6 月 29 日
VPI is back !!

サインインしてコメントする。

Oleg Komarov
Oleg Komarov 2011 年 6 月 29 日

0 投票

If you're looking for elementwise elevation to a power then:
mod(a.^b,c)

1 件のコメント

zikrullah muhammad
zikrullah muhammad 2011 年 6 月 29 日
i need loop, so the number not bigger than realmax
for example:
mod((1234^5678),1256)---> it's return NaN

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Types についてさらに検索

タグ

質問済み:

2011 年 6 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by