如何计算pow(x,y)%n [英] How to compute pow(x, y) % n

查看:93
本文介绍了如何计算pow(x,y)%n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我必须计算表达式的值:pow(x,y)%n(pow(x,y)的余数除以n)
我已经这样尝试过:

Hello everyone!

I have to compute the value of expression: pow(x, y) % n (remainder of pow(x,y) divided by n)
I''ve tried like this:

long L1 = ((long)Math.pow(123,17)) % 3233;
long L2 = ((long)Math.pow(L1,2753)) % 3233;
System.out.format("L1 = %d%n", L1);
System.out.format("L2 = %d%n", L2);


而且我没有达到我的期望.
我认为问题在于我们处理的数字太大.

如何计算这些东西?

在此先感谢!


and I haven''t got what I''ve expected.
I think the problem is that we''re dealing with too big numbers.

How can I compute these thing?

Thanks in advance!

推荐答案

使用
Use the BigInteger[^] class.
For instance:
// implementation of 'long L1 = ((long)Math.pow(123,17)) % 3233;'
BigInteger b = new BigInteger("123");
BigInteger e = new BigInteger("17");
BigInteger m = new BigInteger("3233");
BigInteger r;
r = b.modPow(e, m);


这篇关于如何计算pow(x,y)%n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆