Skip to content

Commit ef5810d

Browse files
author
alxkm
committed
refactor: EulersFunction
1 parent 101cb95 commit ef5810d

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

src/main/java/com/thealgorithms/others/EulersFunction.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package com.thealgorithms.others;
22

33
/**
4-
* @brief utility class for <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>
4+
* Utility class for computing
5+
* <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>.
56
*/
67
public final class EulersFunction {
78
private EulersFunction() {
89
}
910

11+
/**
12+
* Validates that the input is a positive integer.
13+
*
14+
* @param n the input number to validate
15+
* @throws IllegalArgumentException if {@code n} is non-positive
16+
*/
1017
private static void checkInput(int n) {
1118
if (n <= 0) {
1219
throw new IllegalArgumentException("n must be positive.");
1320
}
1421
}
1522

1623
/**
17-
* @brief computes the value of Euler's totient function for given input
18-
* @details has time complexity of O(sqrt(n))
19-
* @param n the input
20-
* @exception IllegalArgumentException n is non-positive
21-
* @return the value of Euler's totient function for the input
24+
* Computes the value of Euler's totient function for a given input.
25+
* This function has a time complexity of O(sqrt(n)).
26+
*
27+
* @param n the input number
28+
* @return the value of Euler's totient function for the given input
29+
* @throws IllegalArgumentException if {@code n} is non-positive
2230
*/
2331
public static int getEuler(int n) {
2432
checkInput(n);

src/test/java/com/thealgorithms/others/EulersFunctionTest.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,32 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6-
import java.util.HashMap;
7-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
import java.util.stream.Stream;
811

912
class EulersFunctionTest {
10-
@Test
11-
public void testGetEuler() {
12-
HashMap<Integer, Integer> testCases = new HashMap<>();
13-
testCases.put(1, 1);
14-
testCases.put(2, 1);
15-
testCases.put(3, 2);
16-
testCases.put(4, 2);
17-
testCases.put(5, 4);
18-
testCases.put(6, 2);
19-
testCases.put(10, 4);
20-
testCases.put(21, 12);
21-
testCases.put(69, 44);
22-
testCases.put(47, 46);
23-
testCases.put(46, 22);
24-
testCases.put(55, 40);
25-
testCases.put(34, 16);
26-
testCases.put(20, 8);
27-
testCases.put(20, 8);
28-
testCases.put(1024, 512);
29-
30-
for (final var tc : testCases.entrySet()) {
31-
assertEquals(tc.getValue(), EulersFunction.getEuler(tc.getKey()));
32-
}
13+
14+
@ParameterizedTest
15+
@MethodSource("provideNumbersForGetEuler")
16+
void testGetEuler(int input, int expected) {
17+
assertEquals(expected, EulersFunction.getEuler(input));
18+
}
19+
20+
@ParameterizedTest
21+
@MethodSource("provideInvalidNumbersForGetEuler")
22+
void testGetEulerThrowsExceptionForNonPositiveInput(int input) {
23+
assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(input));
3324
}
3425

35-
@Test
36-
public void testGetEulerThrowsExceptionForNonPositiveInput() {
37-
assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(0));
26+
private static Stream<Arguments> provideNumbersForGetEuler() {
27+
return Stream.of(Arguments.of(1, 1), Arguments.of(2, 1), Arguments.of(3, 2), Arguments.of(4, 2), Arguments.of(5, 4), Arguments.of(6, 2), Arguments.of(10, 4), Arguments.of(21, 12), Arguments.of(69, 44), Arguments.of(47, 46), Arguments.of(46, 22), Arguments.of(55, 40), Arguments.of(34, 16), Arguments.of(20, 8), Arguments.of(1024, 512));
28+
}
29+
30+
private static Stream<Arguments> provideInvalidNumbersForGetEuler() {
31+
return Stream.of(Arguments.of(0), Arguments.of(-1), Arguments.of(-10));
3832
}
3933
}
34+

0 commit comments

Comments
 (0)