Java实现算法导论中Pollard的rho启发式方法
生活随笔
收集整理的这篇文章主要介绍了
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启发式方法的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 算法导论之有关数论的算法
- 下一篇: Java实现算法导论中朴素字符串匹配算法