Skip to content

Commit 69d0070

Browse files
Add mobius function (TheAlgorithms#3241)
1 parent 6cfb628 commit 69d0070

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.maths;
2+
3+
/*
4+
* Java program for mobius 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, 0, 1} depending on the factorization of n into prime factors:
7+
* μ(n) = +1 if n is a square-free positive integer with an even number of prime factors.
8+
* μ(n) = −1 if n is a square-free positive integer with an odd number of prime factors.
9+
* μ(n) = 0 if n has a squared prime factor.
10+
* Wikipedia: https://en.wikipedia.org/wiki/M%C3%B6bius_function
11+
*
12+
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
13+
*
14+
* */
15+
public class MobiusFunction {
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 is less than or equals 1
22+
* or number has even number of prime factors
23+
* 0 when number has repeated prime factor
24+
* -1 when number has odd number of prime factors
25+
*/
26+
static int mobius(int number) {
27+
28+
if(number <= 0) {
29+
//throw exception when number is less than or is zero
30+
throw new IllegalArgumentException("Number must be greater than zero.");
31+
}
32+
33+
if(number == 1) {
34+
//return 1 if number passed is less or is 1
35+
return 1;
36+
}
37+
38+
int primeFactorCount=0;
39+
40+
for(int i = 1; i <= number; i++) {
41+
//find prime factors of number
42+
if(number % i == 0 && PrimeCheck.isPrime(i)) {
43+
//check if number is divisible by square of prime factor
44+
if(number % (i*i) == 0) {
45+
//if number is divisible by square of prime factor
46+
return 0;
47+
}
48+
/*increment primeFactorCount by 1
49+
if number is not divisible by square of found prime factor*/
50+
primeFactorCount++;
51+
}
52+
}
53+
54+
return (primeFactorCount % 2 == 0) ? 1 : -1;
55+
}
56+
57+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 MobiusFunctionTest {
8+
9+
@Test
10+
void testMobiusForZero() {
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+
MobiusFunction.mobius(number);
18+
});
19+
String actualMessage = exception.getMessage();
20+
21+
//then
22+
assertEquals(expectedMessage, actualMessage);
23+
}
24+
25+
@Test
26+
void testMobiusForNegativeNumber() {
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+
MobiusFunction.mobius(number);
34+
});
35+
String actualMessage = exception.getMessage();
36+
37+
//then
38+
assertEquals(expectedMessage, actualMessage);
39+
}
40+
41+
@Test
42+
void testMobiusFunction(){
43+
int[] expectedResultArray = {
44+
1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0, 1, 1, -1, 0,
45+
0, 1, 0, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, 1, 1, 0, -1, -1, -1, 0, 0, 1, -1, 0, 0,
46+
0, 1, 0, -1, 0, 1, 0, 1, 1, -1, 0, -1, 1, 0, 0, 1, -1, -1, 0, 1, -1, -1, 0, -1, 1,
47+
0, 0, 1, -1, -1, 0, 0, 1, -1, 0, 1, 1, 1, 0, -1, 0, 1, 0, 1, 1, 1, 0, -1, 0, 0, 0
48+
};
49+
50+
for(int i = 1; i <= 100; i++) {
51+
52+
//given
53+
int expectedValue = expectedResultArray[i-1];
54+
55+
//when
56+
int actualValue = MobiusFunction.mobius(i);
57+
58+
//then
59+
assertEquals(expectedValue,actualValue);
60+
}
61+
}
62+
63+
}

0 commit comments

Comments
 (0)