Files
JavaScript/Maths/LiouvilleFunction.js
2022-09-15 12:20:58 +05:30

26 lines
980 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
* Liouville Function: https://en.wikipedia.org/wiki/Liouville_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 positive integer with an even number of prime factors.
* λ(n) = 1 if n positive integer with an odd number of prime factors.
*/
/**
* @function liouvilleFunction
* @description -> This method returns λ(n) of given number n
* returns 1 when number has even number of prime factors
* returns -1 when number has odd number of prime factors
* @param {Integer} number
* @returns {Integer} 1|-1
*/
import { PrimeFactors } from './PrimeFactors.js'
export const liouvilleFunction = (number) => {
if (number <= 0) {
throw new Error('Number must be greater than zero.')
}
return PrimeFactors(number).length % 2 === 0 ? 1 : -1
}