如何在一组数字上findGCD,LCM

在一组数字上计算最大公约数和最小公倍数最简单的方法是什么? 什么math函数可以用来find这些信息?

我用Euclid的algorithmfind两个数的最大公约数; 它可以迭代获得更大的一组数字的GCD。

private static long gcd(long a, long b) { while (b > 0) { long temp = b; b = a % b; // % is remainder a = temp; } return a; } private static long gcd(long[] input) { long result = input[0]; for(int i = 1; i < input.length; i++) result = gcd(result, input[i]); return result; } 

最小公倍数是有点棘手,但可能最好的办法是减lessGCD ,这可以类似地迭代:

 private static long lcm(long a, long b) { return a * (b / gcd(a, b)); } private static long lcm(long[] input) { long result = input[0]; for(int i = 1; i < input.length; i++) result = lcm(result, input[i]); return result; } 

GCD有一个Euclidalgorithm ,

 public int GCF(int a, int b) { if (b == 0) return a; else return (GCF (b, a % b)); } 

顺便说一下, ab应该大于或等于0 ,而LCM = |ab| / GCF(a, b) |ab| / GCF(a, b)

它没有构build的function。 你可以使用Euclid的algorithmfind两个数的GCD。

对于一组数字

 GCD(a_1,a_2,a_3,...,a_n) = GCD( GCD(a_1, a_2), a_3, a_4,..., a_n ) 

recursion地应用它。

LCM也一样:

 LCM(a,b) = a * b / GCD(a,b) LCM(a_1,a_2,a_3,...,a_n) = LCM( LCM(a_1, a_2), a_3, a_4,..., a_n ) 
 int gcf(int a, int b) { while (a != b) // while the two numbers are not equal... { // ...subtract the smaller one from the larger one if (a > b) a -= b; // if a is larger than b, subtract b from a else b -= a; // if b is larger than a, subtract a from b } return a; // or return b, a will be equal to b either way } int lcm(int a, int b) { // the lcm is simply (a * b) divided by the gcf of the two return (a * b) / gcf(a, b); } 
 int lcmcal(int i,int y) { int n,x,s=1,t=1; for(n=1;;n++) { s=i*n; for(x=1;t<s;x++) { t=y*x; } if(s==t) break; } return(s); } 

如果你可以使用Java 8(实际上是想),你可以使用lambdaexpression式来解决这个function:

 private static int gcd(int x, int y) { return (y == 0) ? x : gcd(y, x % y); } public static int gcd(int... numbers) { return Arrays.stream(numbers).reduce(0, (x, y) -> gcd(x, y)); } public static int lcm(int... numbers) { return Arrays.stream(numbers).reduce(1, (x, y) -> x * (y / gcd(x, y))); } 

我以Jeffrey Hantin的回答为导向,但是

  • 在function上计算了gcd
  • 使用varargs-Syntax来创build一个更简单的API(我不确定重载是否可以正常工作,但是在我的机器上)
  • numbers的gcd转换成函数式语法,这种语法更加紧凑,IMO更易于阅读(至less如果您习惯于函数式编程)

由于附加的函数调用,这种方法可能会稍微慢一些,但对于大多数用例来说,这可能根本就不重要。

使用Java 8,有更多优雅和function的方法来解决这个问题。

LCM:

 private static int lcm(int numberOne, int numberTwo) { final int bigger = Math.max(numberOne, numberTwo); final int smaller = Math.min(numberOne, numberTwo); return IntStream.rangeClosed(1,smaller) .filter(factor -> (factor * bigger) % smaller == 0) .map(factor -> Math.abs(factor * bigger)) .findFirst() .getAsInt(); } 

GCD:

 private static int gcd(int numberOne, int numberTwo) { return (numberTwo == 0) ? numberOne : gcd(numberTwo, numberOne % numberTwo); } 

当然,如果一个参数是0,那么这两种方法都不起作用。

更简单的了解:

 { int num1,num2; int m1,m2,r; cout<<"Enter two numbers: "<<endl; cin>>num1>>num2; if(num1>num2) { m1=num1; m2=num2; } else { m1=num2; m2=num1; } while(r!=0) { r=m1%m2; m1=m2; m2=r; } cout<<"result of gcd of two number is "<<m1<<endl; int lcm= (num1*num2)/m1; cout<<"result of lcm of two number is "<<lcm; } 

– 更多信息,请访问: http : //www.easycppcodes.com/2015/01/find-gcd-and-lcm-of-two-numbers-using.html#sthash.lAWwqgS5.dpuf

import java.util.Scanner; 公共课Lcmhcf {

 /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner scan = new Scanner(System.in); int n1,n2,x,y,lcm,hcf; System.out.println("Enter any 2 numbers...."); n1=scan.nextInt(); n2=scan.nextInt(); x=n1; y=n2; do{ if(n1>n2){ n1=n1-n2; } else{ n2=n2-n1; } } while(n1!=n2); hcf=n1; lcm=x*y/hcf; System.out.println("HCF IS = "+hcf); System.out.println("LCM IS = "+lcm); } } //## Heading ##By Rajeev Lochan Sen 
 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n0 = input.nextInt(); // number of intended input. int [] MyList = new int [n0]; for (int i = 0; i < n0; i++) MyList[i] = input.nextInt(); //input values stored in an array int i = 0; int count = 0; int gcd = 1; // Initial gcd is 1 int k = 2; // Possible gcd while (k <= MyList[i] && k <= MyList[i]) { if (MyList[i] % k == 0 && MyList[i] % k == 0) gcd = k; // Update gcd k++; count++; //checking array for gcd } // int i = 0; MyList [i] = gcd; for (int e: MyList) { System.out.println(e); } } } 
 int lcm(int x,int y){ int i=1; while(true){ if(!(x*i)%y) return x*i; i++; } 
 int main() { int n1,n2,num1,num2,rem,gcd,lcm; printf("Enter a number:"); scanf("%d %d",&num1,&num2); num1=n1; num2=n2; while(num2!=0) { rem=num1%num2; num1=num2; num2=rem; } gcd=num1; lcm=(n1*n2)/gcd; printf("Gcd=%d\n",gcd); printf("Lcm=%d\n",lcm); }