Repository: TheAlgorithms/Java
Pull request #3284 (2022-09-23 06:02:20)

algorithm: Liouville lambda function implementation
### **Describe your change:**

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->
Issue #3265 

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java b/src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java
new file mode 100644
index 00000000..3d49cec0
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java
@@ -0,0 +1,34 @@
+package com.thealgorithms.maths;
+
+/*
+ * Java program for liouville lambda function
+ * For any positive integer n, define λ(n) as the sum of the primitive nth roots of unity. 
+ * It has values in {−1, 1} depending on the factorization of n into prime factors:
+ *   λ(n) = +1 if n is a positive integer with an even number of prime factors.
+ *   λ(n) = −1 if n is a positive integer with an odd number of prime factors.
+ * Wikipedia: https://en.wikipedia.org/wiki/Liouville_function
+ * 
+ * Author: Akshay Dubey (https://github.com/itsAkshayDubey)
+ * 
+ * */
+
+public class LiouvilleLambdaFunction {
+
+	/**
+	 * This method returns λ(n) of given number n
+	 *
+	 * @param number Integer value which λ(n) is to be calculated 
+	 * @return  1 when number has even number of prime factors
+	 *         -1 when number has odd number of prime factors
+	 * @throws IllegalArgumentException when number is negative
+	 */
+	static int liouvilleLambda(int number) {
+		if(number <= 0) {
+			//throw exception when number is less than or is zero
+			throw new IllegalArgumentException("Number must be greater than zero.");
+		}
+
+		//return 1 if size of prime factor list is even, -1 otherwise
+		return PrimeFactorization.pfactors(number).size() % 2 == 0 ? 1 : -1;
+	}
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/LiouvilleLambdaFunctionTest.java b/src/test/java/com/thealgorithms/maths/LiouvilleLambdaFunctionTest.java
new file mode 100644
index 00000000..0959b616
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/LiouvilleLambdaFunctionTest.java
@@ -0,0 +1,66 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+class LiouvilleLambdaFunctionTest {
+
+	@Test
+	void testLiouvilleLambdaMustThrowExceptionIfNumberIsZero() {
+		//given
+		int number = 0;
+		String expectedMessage = "Number must be greater than zero.";
+
+		//when
+		Exception exception = assertThrows(IllegalArgumentException.class, () -> {
+			LiouvilleLambdaFunction.liouvilleLambda(number);
+		});
+		String actualMessage = exception.getMessage();
+
+		//then
+		assertEquals(expectedMessage, actualMessage);
+	}
+
+	@Test
+	void testLiouvilleLambdaMustThrowExceptionIfNumberIsNegative() {
+		//given
+		int number = -1;
+		String expectedMessage = "Number must be greater than zero.";
+
+		//when
+		Exception exception = assertThrows(IllegalArgumentException.class, () -> {
+			LiouvilleLambdaFunction.liouvilleLambda(number);
+		});
+		String actualMessage = exception.getMessage();
+
+		//then
+		assertEquals(expectedMessage, actualMessage);
+	}
+
+	@Test
+	void testLiouvilleLambdaMustReturnNegativeOne() {
+		//given
+		int number = 11;
+		int expectedOutput = -1;
+
+		//when
+		int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
+
+		//then
+		assertEquals(expectedOutput, actualOutput);
+	}
+
+	@Test
+	void testLiouvilleLambdaMustReturnPositiveOne() {
+		//given
+		int number = 10;
+		int expectedOutput = 1;
+
+		//when
+		int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
+
+		//then
+		assertEquals(expectedOutput, actualOutput);
+	}
+}