summaryrefslogtreecommitdiff
path: root/AlgoDesignAndTechniqueEdxJava/sources
diff options
context:
space:
mode:
authorHaidong Ji2018-08-19 15:28:15 -0500
committerHaidong Ji2018-08-19 15:28:15 -0500
commitb3a6f3bc47098c81bef2c7d16690a5801149dbd8 (patch)
tree6646dbeefa43ec4f8fcc05c47985c1496a8aea76 /AlgoDesignAndTechniqueEdxJava/sources
parente56fcf9023ba43919cae20393a2b85d89206271c (diff)
Least Common Multiple done!
Learned two interesting things: 1. Use GCD to get LCM https://www.idomaths.com/hcflcm.php#formula 2. To make a literal of numbers long, add L at the end of the number!
Diffstat (limited to 'AlgoDesignAndTechniqueEdxJava/sources')
-rw-r--r--AlgoDesignAndTechniqueEdxJava/sources/GcdLcm.java (renamed from AlgoDesignAndTechniqueEdxJava/sources/GCD.java)9
1 files changed, 7 insertions, 2 deletions
diff --git a/AlgoDesignAndTechniqueEdxJava/sources/GCD.java b/AlgoDesignAndTechniqueEdxJava/sources/GcdLcm.java
index 5e29e4f..efef982 100644
--- a/AlgoDesignAndTechniqueEdxJava/sources/GCD.java
+++ b/AlgoDesignAndTechniqueEdxJava/sources/GcdLcm.java
@@ -1,6 +1,6 @@
import java.util.Scanner;
-public class GCD {
+public class GcdLcm {
public static int getGCD(int a, int b) {
if (b == 0) {
@@ -19,6 +19,11 @@ public class GCD {
int a = in.nextInt();
int b = in.nextInt();
- System.out.println(getGCD(a, b));
+ System.out.println(getLCM(a, b));
+ }
+
+ public static long getLCM(int a, int b) {
+ // https://www.idomaths.com/hcflcm.php#formula
+ return (long) a * (long) b / (long) getGCD(a, b);
}
}