So if these two numbers are large, how do we find the greatest common divisor, the least common multiple? What if it’s n numbers? Let’s say the least common multiple of a thousand numbers

Enter 2, 4, 6, 3, 2, 5, 7

Output 12, 70

First of all, the greatest common divisor can be defined as LCM (m,n) by the division method of toss and turn, and then define a method GCD (m,n) to find the greatest common divisor by the formula method: Int nums[] = new int[n]; int nums[] = new int[n]; , and then uses a while loop to input the data into the array NUMs. Finally, define an int a to store the m in the GCD (m,n) argument.

Do not know the division of toss and turn, you can see my blog, introduced in detail, very simple.

Public class Test4 {public static void main(String[] args) {int I = 0,m; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Int nums[] = new int[n]; while (n ! = 0 ){ m = sc.nextInt(); if (m == 0){

}else{ nums[i] = m; i++; } n--; } int a =nums[0]; // the first parameter of the LCMCopy the code

// System.out.println(Arrays.toString(nums)); for (int j = 1 ; j < nums.length; j++){ a = gcd(a,nums[j]); } system.out. println(” their least common multiple =”+a); } public static int LCM (int m,int n) {int left = (m > n)? m : n; Int right = (m > n)? n : m; If ((left % right) == 0){return right; } return lcm(right , left % right); } public static int GCD (int m,int n){return m * n/LCM (m, n); }

}