refactor: LeastCommonMultiple (#5435)

* refactor: LeastCommonMultiple

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko
2024-08-30 08:43:45 +02:00
committed by GitHub
parent 9515d96ab6
commit d189c3a719
2 changed files with 19 additions and 39 deletions

View File

@ -1,41 +1,27 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
import java.util.Scanner;
/** /**
* Is a common mathematics concept to find the smallest value number * Is a common mathematics concept to find the smallest value number
* that can be divide using either number without having the remainder. * that can be divide using either number without having the remainder.
* https://maticschool.blogspot.com/2013/11/find-least-common-multiple-lcm.html * https://maticschool.blogspot.com/2013/11/find-least-common-multiple-lcm.html
* @author LauKinHoong * @author LauKinHoong
*/ */
public final class LeastCommonMultiple { public final class LeastCommonMultiple {
private LeastCommonMultiple() { private LeastCommonMultiple() {
} }
/** /**
* Driver Code * Finds the least common multiple of two numbers.
*/ *
public static void main(String[] args) { * @param num1 The first number.
Scanner input = new Scanner(System.in); * @param num2 The second number.
System.out.println("Please enter first number >> "); * @return The least common multiple of num1 and num2.
int num1 = input.nextInt();
System.out.println("Please enter second number >> ");
int num2 = input.nextInt();
System.out.println("The least common multiple of two numbers is >> " + lcm(num1, num2));
input.close();
}
/*
* get least common multiple from two number
*/ */
public static int lcm(int num1, int num2) { public static int lcm(int num1, int num2) {
int high; int high;
int num3; int num3;
int cmv = 0; int cmv = 0;
/*
* value selection for the numerator
*/
if (num1 > num2) { if (num1 > num2) {
high = num1; high = num1;
num3 = num1; num3 = num1;

View File

@ -1,27 +1,21 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
import org.junit.jupiter.api.Assertions; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class LeastCommonMultipleTest { import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/* class LeastCommonMultipleTest {
* Test for first number greater than second number
*/ @ParameterizedTest
@Test @MethodSource("provideTestCases")
public void testForFirst() { void testLcm(int num1, int num2, int expected) {
int result = LeastCommonMultiple.lcm(6, 8); assertEquals(expected, LeastCommonMultiple.lcm(num1, num2));
int expected = 24;
Assertions.assertEquals(result, expected);
} }
/* private static Stream<Arguments> provideTestCases() {
* Test for second number greater than first number return Stream.of(Arguments.of(12, 18, 36), Arguments.of(5, 10, 10), Arguments.of(7, 3, 21), Arguments.of(21, 6, 42), Arguments.of(1, 1, 1), Arguments.of(8, 12, 24), Arguments.of(14, 35, 70), Arguments.of(15, 25, 75), Arguments.of(100, 25, 100), Arguments.of(0, 10, 0));
*/
@Test
public void testForSecond() {
int result = LeastCommonMultiple.lcm(8, 6);
int expected = 24;
Assertions.assertEquals(result, expected);
} }
} }