How to get mod of large numbers?

13 ビュー (過去 30 日間)
Mohsin Shah
Mohsin Shah 2017 年 5 月 1 日
コメント済み: Mohsin Shah 2017 年 5 月 2 日
I have this calculation: c=(5652)^42.(23)^77mod5929, when i use scientific calculator I get c=4624 an when I do this by MATLAB mod command it gives 0...why?

採用された回答

Steven Lord
Steven Lord 2017 年 5 月 1 日
If you compute (5652^42)*(23^77) to use the naive approach for computing this quantity, the resulting number is much greater than flintmax. If we look at the spacing between that number and the next largest number, we see that the spacing is (much) greater than 5929.
>> x = (5652^42)*(23^77)
x =
2.78954779454946e+262
>> eps(x)
ans =
3.49595995098571e+246
>> (x + 5929) == x
ans =
logical
1
As Sean stated in this answer from a while ago, you could use Symbolic Math Toolbox. Or you could use one of the algorithms described in this Wikipedia page to perform these computations with numbers no greater than 5652^2.
  1 件のコメント
Mohsin Shah
Mohsin Shah 2017 年 5 月 2 日
Thank you so much

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2017 年 5 月 1 日
編集済み: John D'Errico 2017 年 5 月 1 日
Because MATLAB works in double precision.
5652^42 * 23^77
ans =
2.78954779454946e+262
This is a number with 263 decimal digits, so far beyond the precision available to a double. A double can represent no integer exactly that is larger than 2^53. And 2^53 is just not very large in this context.
You can either use a tool (like my VPI toolbox or the symbolic toolbox)
vpi(5652)^42 * vpi(23)^77
ans =
27895477945494593633174830929266408591443695665432884022901567138564930227891230414671987539540619495363075575420684353178819655415398533495806892010942366092828416772086484568033389306894581602342651519614654523921262520198453838046500944165540916333162855923712
mod(ans,5929)
ans =
4624
Or, you can recognize that you can use mod in a loop. Thus, this law applies:
mod(a*b,n) = mod(mod(a,n)*mod(b,n),n)
So you can compute the mod of a power (or a product of powers) in a simple loop, while never exceeding the limits imposed by double precision.
  1 件のコメント
Mohsin Shah
Mohsin Shah 2017 年 5 月 2 日
Thank you so much

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

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by