欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

Java实现算法导论中Pollard的rho启发式方法

发布时间:2025/4/16 java 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Java实现算法导论中Pollard的rho启发式方法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Pollard的rho启发式方法用于启发式求解大整数n分解因子,具体要结合导论中来理解,参考代码如下:

package cn.ansj;import java.math.BigInteger; import java.security.SecureRandom;class PollardRho{private final static BigInteger ZERO = new BigInteger("0");private final static BigInteger ONE = new BigInteger("1");private final static BigInteger TWO = new BigInteger("2");private final static SecureRandom random = new SecureRandom();public static BigInteger rho(BigInteger N) {BigInteger divisor;BigInteger c = new BigInteger(N.bitLength(), random);BigInteger x = new BigInteger(N.bitLength(), random);BigInteger xx = x;if (N.mod(TWO).compareTo(ZERO) == 0) return TWO;do {x = x.multiply(x).mod(N).add(c).mod(N);xx = xx.multiply(xx).mod(N).add(c).mod(N);xx = xx.multiply(xx).mod(N).add(c).mod(N);divisor = x.subtract(xx).gcd(N);} while((divisor.compareTo(ONE)) == 0);return divisor;}public static void factor(BigInteger N) {if (N.compareTo(ONE) == 0) return;if (N.isProbablePrime(20)) { System.out.println(N); return; }BigInteger divisor = rho(N);factor(divisor);factor(N.divide(divisor));}public static void main(String[] args) {BigInteger N = BigInteger.valueOf(96879);System.out.println(N);factor(N);} } 执行结果:

96879 3 43 751


总结

以上是生活随笔为你收集整理的Java实现算法导论中Pollard的rho启发式方法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。