三种算法求最大公约数——Java实现

 求两个自然数mn的最大公约数。

连续整除检测:

   1. t=min{m,n}

   2. m除以t,如果余数为0,则执行步骤3,否则,执行步骤4

   3. n除以t,如果余数为0,返回t的值作为结果,否则,执行步骤4

   4. t=t-1,转第2步;

    例如,要计算gcd(66,12),首先令t=12,因为66除以12余数不为0,将t1,而12除以11余数不为0,再将t1,重复上述过程,直到t=6,此时12除以6的余数为0并且66除以6的余数为0,则gcd(66,11)=6

GreatestCommonDivisor1.java                                             //连续整除算法

 import java.util.*;

public class GreatestCommonDivisor1
{
 public static void main(String[] args)
 {
  /**
   *从键盘输入要求的两个整数:m和n
   */
  Scanner in=new Scanner(System.in);
  System.out.println("Please input two integers, m and n: ");
  int m=in.nextInt();
  int n=in.nextInt();
  double startTime=System.currentTimeMillis();//starttime
  /**
   *求解最大公约数
   */
  int t=m<n?m:n;
  while(m%t!=0 || n%t!=0)
  {
   t=t-1;
  }
  double endTime=System.currentTimeMillis();//endtime
  /**
   *打印输出最后求出的结果和程序用的时间
   */
  System.out.println("The greatest common divisor is ,gcd(m,n)="+t);
  System.out.println("Basic Statements take "+(endTime-startTime)+" milliseconds!");
 }
}

********************************************************************************************************************************************************

欧几里得算法:

1. r = m % n;

    2. 循环直到 r 等于0 

          2.1 m = n;

          2.2 n = r;

          2.3 r = m % n;

    3. 输出 n ;

      例如,要计算gcd(66,12),因为66 除以12的余数为6再将12除以6, 余数为0 ,gcd(66,12)=6

GreatestCommonDivisor2.java                                    //欧几里得算法

import java.util.*;

 

public class GreatestCommonDivisor2
{
 public static void main(String[] args)
 {
  /**
   *从键盘输入要求的两个整数:m和n
   */
  Scanner in=new Scanner(System.in);
  System.out.println("Please input two integers,m and n: " );
  int m=in.nextInt();
  int n=in.nextInt();
  int r=m%n;
  double startTime=System.currentTimeMillis();
  /**
   *求解最大公约数
   */
  while(r!=0)
  {
   m=n;
   n=r;
   r=m%n;
  }
  double endTime=System.currentTimeMillis();
  /**
   *打印输出最后求出的结果和程序用的时间
   */
  System.out.println("The greatest common divisor is, gcd(m,n)="+n);
  System.out.println("Basic Statements take "+(endTime-startTime)+" milliseconds!");
 }
}

*********************************************************************************************************************************************************

分解质因数: 

1.m 分解质因数;

2.n 分解质因数;

3.提取mn中的公共质因数相乘;

4.mn中的公共质因数相乘,乘积作为结果输出;

例如,要计算gcd(66,12),首先分解质因数66=2*3*11, 12=2*2*3,然后提取两者的公共质因数2*3,则gcd(66,12)=2*3=6

GreatestCommonDivisor3.java                                            //分解质因数算法

 

import java.util.*;

 

public class GreatestCommonDivisor3
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  System.out.println("Please input two integers,m and n: ");
  int m=in.nextInt();
  int n=in.nextInt();
  
  double startTime=System.currentTimeMillis();
  /**
   *将m分解质因数,因子存放在Integer类型的数组maa(由泛型数组列表ma转换而来)中
   */
  ArrayList<Integer> ma=new ArrayList<Integer>();
  for(int i=2;i<=m;i++)
  {
   while(m!=i)
   {
    if(m%i==0)
    {
     ma.add(i);;
     m=m/i;
    }
    else break;
   }
  }
  ma.add(m);
  Integer[] maa=new Integer[ma.size()];
  ma.toArray(maa);
  /**
   *将n分解质因数,因子存放在Integer类型的数组naa(由泛型数组列表na转换而来)中
   */
  ArrayList<Integer> na=new ArrayList<Integer>();
  for(int i=2;i<=n;i++)
  {
   while(n!=i)
   {
    if(n%i==0)
    {
     na.add(i);;
     n=n/i;
    }
    else break;
   }
  }
  na.add(n);
  Integer[] naa=new Integer[na.size()];
  na.toArray(naa);
  /**
   *比较两个数m和n的质因数,提取公共的质因子,存放到Integer类型数组ga(由泛型数组列表g转换而来)中
   */
  ArrayList<Integer> g=new ArrayList<Integer>();
  for(int i=0;i<ma.size();i++)
  {
   int j=0;
   do
   {
    if(maa[i]==naa[j])
    {
     g.add(maa[i]);
     ma.remove(i);
     ma.toArray(maa);
    }
    j++;
   }
   while(j<na.size());   
  }
  Integer[] ga=new Integer[g.size()];
  g.toArray(ga);
  double endTime=System.currentTimeMillis();
  
  /**
   *用存放在ga中的公共质因子相乘得到m和n的最大公约数gcd,并打印显示出来
   */ 
  int gcd=1; 
  for(int i=0;i<ga.length;i++)
   gcd*=ga[i];
  System.out.println("The greatest common divisor is,gcd(m,n)= "+gcd);
  System.out.println("Basic statements take "+(endTime-startTime)+" milliseconds!");
 }
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值