Java BigInteger 实现大数运算及取余操作

版权申诉
0 下载量 22 浏览量 更新于2024-09-05 收藏 11KB PDF 举报
"该资源是一个Java编程练习,主要涉及大数运算,使用了`BigInteger`类来处理大整数的计算。练习的目标是计算91的92次方,并求出这个结果除以100的余数。此外,代码还展示了如何初始化`BigInteger`对象以及调用其方法进行乘法运算和取模运算。" 在Java中,`BigInteger`类是用于处理任意精度的整数。当需要进行大数运算,例如超过`int`或`long`类型的范围时,可以使用这个类。`BigInteger`提供了丰富的数学操作,如加法、减法、乘法、除法以及幂运算等,且能保证精确性。 在提供的代码中,可以看到以下几个关键点: 1. **初始化 BigInteger 对象**:使用 `BigInteger.valueOf(long)` 或 `new BigInteger(String)` 创建 `BigInteger` 对象。`k` 被初始化为1,`j` 被初始化为91,`m` 被初始化为100(都是以字符串形式创建的)。 2. **大数乘法**:`k=k.multiply(j);` 这行代码用于计算91的累乘,每次循环将 `k` 的值乘以 `j`,实现91的92次方运算。 3. **打印计算结果**:`System.out.println(k.toString());` 在每次乘法后打印当前的结果,以便观察计算过程。 4. **大数取模运算**:`k.remainder(m);` 这行代码用于求91的92次方除以100的余数。`n` 存储了这个余数,并通过 `System.out.println(n.toString());` 打印出来。 5. **BigDecimal 类**:虽然在代码中创建了一个 `BigDecimal` 对象 `l`,但在这个练习中并没有实际使用到。`BigDecimal` 类通常用于处理浮点数的大数运算,保持更高的精度。 6. **方法调用**:`BigInteger` 类提供了很多其他方法,如 `add`、`subtract`、`divide`、`pow` 等,用于大数的加、减、除和求幂。代码中有一个未使用的示例方法 `getAdd` 和 `getSubtract`,这两个方法分别用于两个 `BigInteger` 对象的加法和减法操作。 在实际编程中,`BigInteger` 类是非常有用的工具,特别是在金融、科学计算或者任何需要处理大整数的场景。使用它可以确保计算的精确性和无限的整数范围。此外,`BigInteger` 还支持一些高级功能,如质因数分解、GCD(最大公约数)和LCM(最小公倍数)计算等。

改正下面C#的代码错误public partial class Form7 : Form { private BigInteger p, q, n, phi_n, e, d; public Form7() { InitializeComponent(); } private void Form7_Load(object sender, EventArgs e) { GenerateKeys(); } private void GenerateKeys() { // 选择两个质数p和q string string1, string2; Int64 n, p, q, phi_n, e; p = BigInteger.Parse("12347534159895123"); q = BigInteger.Parse( "987654321357159852"); n = p * q; phi_n = (p - 1) * (q - 1); // 选择e并计算d e = 65537; d = ModInverse(e, phi_n); publicKeyTextBox.Text = $"{n} {e}"; privateKeyTextBox.Text = $"{n} {d}"; } private BigInteger ModInverse(BigInteger e, long phi_n) { throw new NotImplementedException(); } // 加密函数 private string Encrypt(string message, BigInteger n, BigInteger e) { BigInteger m = StrToBig(message); BigInteger c = BigInteger.ModPow(m, e, n); return BigToStr(c); } // 解密函数 private string Decrypt(string ctext, BigInteger n, BigInteger d) { BigInteger c = StrToBig(ctext); BigInteger m = BigInteger.ModPow(c, d, n); return BigToStr(m); } // 字符串转换为大整数 private BigInteger StrToBig(string str) { byte[] bytes = System.Text.Encoding.Unicode.GetBytes(str); return new BigInteger(bytes); } // 大整数转换为字符串 private string BigToStr(BigInteger big) { byte[] bytes = big.ToByteArray(); return System.Text.Encoding.Unicode.GetString(bytes); } // 计算模反元素 private BigInteger ModInverse(BigInteger a, BigInteger m) { BigInteger x, y; ExtendedGCD(a, m, out x, out y); return x; } // 扩展欧几里得算法 private void ExtendedGCD(BigInteger a, BigInteger b, out BigInteger x, out BigInteger y) { if (b == 0) { x = 1; y = 0; } else { ExtendedGCD(b, a % b, out y, out x); y -= a / b * x; } } private void encryptButton_Click(object sender, EventArgs e) { string message = inputTextBox.Text; string ctext = Encrypt(message, n, e); outputTextBox.Text = ctext; } private void decryptButton_Click(object sender, EventArgs e) { string ctext = outputTextBox.Text; string message = Decrypt(ctext, n, d); outputTextBox.Text = message; } } }

2023-06-12 上传