Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This file is for testing purposes only.
- */
- import java.util.Scanner;
- public class Test {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // input
- int n = in.nextInt();
- int[] arr = new int[n];
- for(int i = 0; i < n; i++) {
- arr[i] = in.nextInt();
- }
- // find the least common multiple of the given sequence
- int lcm = arr[0];
- for(int i = 1; i < arr.length; i++) {
- lcm = (arr[i] * lcm) / getGcd(arr[i], lcm);
- }
- // output
- System.out.println("LCM = " + lcm);
- }
- /***
- * Calculates the greatest common divisor (GCD) of two numbers
- * @param a
- * @param b
- * @return
- */
- static int getGcd(int a, int b) {
- if(b == 0) {
- return a;
- }
- return getGcd(b, a % b);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement