Java中素数分解的Pollard Rho算法
它是对给定整数执行因式分解的算法。以下是实现Rho算法进行素因分解的程序。
程序
public class PollardsRho {
int num = 65;
public int gcd(int a, int b) {
int gcd = 0;
for(int i = 1; i <= a || i <= b; i++) {
if( a%i == 0 && b%i == 0 ) {
gcd = i;
}
}
return gcd;
}
int g(int x) {
return ((x*x)-1) % num;
}
public static void main(String args[]) {
PollardsRho obj = new PollardsRho();
int x = 2, y = 2, d = 1;
while(d==1) {
x = obj.g(x);
y = obj.g(obj.g(y));
d = obj.gcd((x - y), obj.num);
}
if (d == obj.num) {
System.out.println("Cannot calculate GCD for this element");
} else {
System.out.println("One of the divisors of given number is "+d);
}
}
}输出结果
One of the divisors of given number are 5
热门推荐
7 入学校简短祝福语
10 春节拜年祝福语儿童简短
11 横幅元旦祝福语大全简短
12 新大学开学祝福语简短
13 对睡觉的祝福语简短
14 表妹结婚祝福语简短藏语
15 侄儿生日祝福语简短独特
16 宝宝新年祝福语大全简短
17 祝女人美丽祝福语简短
18 幼儿住院祝福语老师简短