mobius function Algorithm

The Mobius function algorithm is a number-theoretical function used in various mathematical applications, particularly in the field of multiplicative number theory. It is denoted as μ(n) and defined as follows: μ(n) is equal to 1 if n is a square-free positive integer with an even number of prime factors, -1 if it is a square-free positive integer with an odd number of prime factors, and 0 if it has a squared prime factor. In simpler terms, the Mobius function helps determine the "structure" of a number in terms of its prime factors and whether it has any repeated prime factors. The algorithm for computing the Mobius function typically involves factoring the input number and then analyzing its prime factors. One common approach to calculate μ(n) is to use the sieve of Eratosthenes, a well-known algorithm for finding prime numbers up to a given limit. By sieving out the prime factors of the input number, one can easily determine if it is square-free and count the number of distinct prime factors. Once this information is obtained, the Mobius function's value can be determined according to the aforementioned rules. The Mobius function algorithm has various applications in number theory, such as understanding the distribution of prime numbers, solving Diophantine equations, and studying arithmetic functions' behavior.
"""
References: https://en.wikipedia.org/wiki/M%C3%B6bius_function
References: wikipedia:square free number
python/black : True
flake8 : True
"""

from maths.prime_factors import prime_factors
from maths.is_square_free import is_square_free


def mobius(n: int) -> int:
    """
    Mobius function
    >>> mobius(24)
    0
    >>> mobius(-1)
    1
    >>> mobius('asd')
    Traceback (most recent call last):
        ...
    TypeError: '<=' not supported between instances of 'int' and 'str'
    >>> mobius(10**400)
    0
    >>> mobius(10**-400)
    1
    >>> mobius(-1424)
    1
    >>> mobius([1, '2', 2.0])
    Traceback (most recent call last):
        ...
    TypeError: '<=' not supported between instances of 'int' and 'list'
    """
    factors = prime_factors(n)
    if is_square_free(factors):
        return -1 if len(factors) % 2 else 1
    return 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()

LANGUAGE:

DARK MODE: