Skip to content

Commit ea8e046

Browse files
Add Liouville lambda function (TheAlgorithms#3284)
1 parent 2fbb1d6 commit ea8e046

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.maths;
2+
3+
/*
4+
* Java program for liouville lambda function
5+
* For any positive integer n, define λ(n) as the sum of the primitive nth roots of unity.
6+
* It has values in {−1, 1} depending on the factorization of n into prime factors:
7+
* λ(n) = +1 if n is a positive integer with an even number of prime factors.
8+
* λ(n) = −1 if n is a positive integer with an odd number of prime factors.
9+
* Wikipedia: https://en.wikipedia.org/wiki/Liouville_function
10+
*
11+
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
12+
*
13+
* */
14+
15+
public class LiouvilleLambdaFunction {
16+
17+
/**
18+
* This method returns λ(n) of given number n
19+
*
20+
* @param number Integer value which λ(n) is to be calculated
21+
* @return 1 when number has even number of prime factors
22+
* -1 when number has odd number of prime factors
23+
* @throws IllegalArgumentException when number is negative
24+
*/
25+
static int liouvilleLambda(int number) {
26+
if(number <= 0) {
27+
//throw exception when number is less than or is zero
28+
throw new IllegalArgumentException("Number must be greater than zero.");
29+
}
30+
31+
//return 1 if size of prime factor list is even, -1 otherwise
32+
return PrimeFactorization.pfactors(number).size() % 2 == 0 ? 1 : -1;
33+
}
34+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class LiouvilleLambdaFunctionTest {
8+
9+
@Test
10+
void testLiouvilleLambdaMustThrowExceptionIfNumberIsZero() {
11+
//given
12+
int number = 0;
13+
String expectedMessage = "Number must be greater than zero.";
14+
15+
//when
16+
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
17+
LiouvilleLambdaFunction.liouvilleLambda(number);
18+
});
19+
String actualMessage = exception.getMessage();
20+
21+
//then
22+
assertEquals(expectedMessage, actualMessage);
23+
}
24+
25+
@Test
26+
void testLiouvilleLambdaMustThrowExceptionIfNumberIsNegative() {
27+
//given
28+
int number = -1;
29+
String expectedMessage = "Number must be greater than zero.";
30+
31+
//when
32+
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
33+
LiouvilleLambdaFunction.liouvilleLambda(number);
34+
});
35+
String actualMessage = exception.getMessage();
36+
37+
//then
38+
assertEquals(expectedMessage, actualMessage);
39+
}
40+
41+
@Test
42+
void testLiouvilleLambdaMustReturnNegativeOne() {
43+
//given
44+
int number = 11;
45+
int expectedOutput = -1;
46+
47+
//when
48+
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
49+
50+
//then
51+
assertEquals(expectedOutput, actualOutput);
52+
}
53+
54+
@Test
55+
void testLiouvilleLambdaMustReturnPositiveOne() {
56+
//given
57+
int number = 10;
58+
int expectedOutput = 1;
59+
60+
//when
61+
int actualOutput = LiouvilleLambdaFunction.liouvilleLambda(number);
62+
63+
//then
64+
assertEquals(expectedOutput, actualOutput);
65+
}
66+
}

0 commit comments

Comments
 (0)