Skip to content

refactor: cleanup EulersFunction #5388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/main/java/com/thealgorithms/others/EulersFunction.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package com.thealgorithms.others;

/**
* @brief utility class for <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>
* Utility class for computing
* <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>.
*/
public final class EulersFunction {
private EulersFunction() {
}

/**
* Validates that the input is a positive integer.
*
* @param n the input number to validate
* @throws IllegalArgumentException if {@code n} is non-positive
*/
private static void checkInput(int n) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive.");
}
}

/**
* @brief computes the value of Euler's totient function for given input
* @details has time complexity of O(sqrt(n))
* @param n the input
* @exception IllegalArgumentException n is non-positive
* @return the value of Euler's totient function for the input
* Computes the value of Euler's totient function for a given input.
* This function has a time complexity of O(sqrt(n)).
*
* @param n the input number
* @return the value of Euler's totient function for the given input
* @throws IllegalArgumentException if {@code n} is non-positive
*/
public static int getEuler(int n) {
checkInput(n);
Expand Down
48 changes: 21 additions & 27 deletions src/test/java/com/thealgorithms/others/EulersFunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,31 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.HashMap;
import org.junit.jupiter.api.Test;
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 EulersFunctionTest {
@Test
public void testGetEuler() {
HashMap<Integer, Integer> testCases = new HashMap<>();
testCases.put(1, 1);
testCases.put(2, 1);
testCases.put(3, 2);
testCases.put(4, 2);
testCases.put(5, 4);
testCases.put(6, 2);
testCases.put(10, 4);
testCases.put(21, 12);
testCases.put(69, 44);
testCases.put(47, 46);
testCases.put(46, 22);
testCases.put(55, 40);
testCases.put(34, 16);
testCases.put(20, 8);
testCases.put(20, 8);
testCases.put(1024, 512);

for (final var tc : testCases.entrySet()) {
assertEquals(tc.getValue(), EulersFunction.getEuler(tc.getKey()));
}
@ParameterizedTest
@MethodSource("provideNumbersForGetEuler")
void testGetEuler(int input, int expected) {
assertEquals(expected, EulersFunction.getEuler(input));
}

@Test
public void testGetEulerThrowsExceptionForNonPositiveInput() {
assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(0));
@ParameterizedTest
@MethodSource("provideInvalidNumbersForGetEuler")
void testGetEulerThrowsExceptionForNonPositiveInput(int input) {
assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(input));
}

private static Stream<Arguments> provideNumbersForGetEuler() {
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));
}

private static Stream<Arguments> provideInvalidNumbersForGetEuler() {
return Stream.of(Arguments.of(0), Arguments.of(-1), Arguments.of(-10));
}
}