Skip to content

tests: add tests for EulersFunction #4216

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
Jun 19, 2023
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
30 changes: 17 additions & 13 deletions src/main/java/com/thealgorithms/others/EulersFunction.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package com.thealgorithms.others;

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

// This method returns us number of x that (x < n) and gcd(x, n) == 1 in O(sqrt(n)) time
// complexity;
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
*/
public static int getEuler(int n) {
checkInput(n);
int result = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
Expand All @@ -26,10 +36,4 @@ public static int getEuler(int n) {
}
return result;
}

public static void main(String[] args) {
for (int i = 1; i < 100; i++) {
System.out.println(getEuler(i));
}
}
}
39 changes: 39 additions & 0 deletions src/test/java/com/thealgorithms/others/EulersFunctionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.thealgorithms.others;

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;

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()));
}
}

@Test
public void testGetEulerThrowsExceptionForNonPositiveInput() {
assertThrows(IllegalArgumentException.class, () -> EulersFunction.getEuler(0));
}
}