-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCM2.java
More file actions
75 lines (67 loc) · 2.01 KB
/
LCM2.java
File metadata and controls
75 lines (67 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.*;
public class LCM2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long[] nums = new long[3];
for (int i = 0; i < nums.length; i++)
{
nums[i] = sc.nextLong();
if (nums[i] < 0)
{
//If the provided number is a negative number, flip to positive
//(assuming test cases assume we check the absolute value)
nums[i] = nums[i] * - 1;
}
}
sc.close();
// nums = bubbleSort(nums);
if (nums[0] == 0 && nums[1] == 0 && nums[2] == 0)
{
System.out.println("0");
}
else if (nums[0] == 0 || nums[1] == 0 || nums[2] == 0)
{
System.out.println("NA");
}
else
{
long lcm = getLowestCommonMultiple(nums[2], getLowestCommonMultiple(nums[1], nums[0]));
System.out.println(lcm);
}
}
public static long getLowestCommonMultiple(long a, long b)
{
long gcd = getGreatestCommonDivisor(a, b);
return (a*b) / gcd;
}
public static long getGreatestCommonDivisor(long a, long b)
{
if (b == 0) return a;
else return (getGreatestCommonDivisor(b, a%b));
}
//region bubble sort function (may not be needed for algorithm)
// public static long[] bubbleSort(long[] arr)
// {
// int leng = arr.length;
// for (int i = 0; i < leng -1; i++)
// {
// boolean swapped = false;
// for (int j = 0; j < leng - i - 1; j++)
// {
// if (arr[j] > arr[j+1])
// {
// long temp = arr[j+1];
// arr[j+1] = arr[j];
// arr[j] = temp;
// swapped = true;
// }
// }
// if (!swapped)
// {
// break;
// }
// }
// return arr;
// }
//endregion;
}