//计算两个随机小于等于n的互异正整数互素 的 概率,
public class ProbRelPrim {
public static double probRelPrim(int n){
int rel =0;
int tot = 0;
for(int i = 1; i <= n; i++)
for(int j = i + 1; j <= n; j++){
tot++;
if(gcd(i, j) == 1)//最大公约数为一,则互素,
rel++;
}
return (double) rel / tot;
}
public static int gcd(int m, int n){//求最大公约数,
while(n != 0){
int rem = m % n;
m = n;
n = rem;
}
return m;
}
public static void main(String[] args){
int x = 100;
double y = probRelPrim(x);
System.out.println(y);
}
}
//计算两个随机小于等于n的互异正整数互素 的 概率,
最新推荐文章于 2021-11-12 14:09:08 发布