- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathliouville_lambda.py
46 lines (39 loc) · 1.34 KB
/
liouville_lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
== Liouville Lambda Function ==
The Liouville Lambda function, denoted by λ(n)
and λ(n) is 1 if n is the product of an even number of prime numbers,
and -1 if it is the product of an odd number of primes.
https://en.wikipedia.org/wiki/Liouville_function
"""
# Author : Akshay Dubey (https://github.com/itsAkshayDubey)
frommaths.prime_factorsimportprime_factors
defliouville_lambda(number: int) ->int:
"""
This functions takes an integer number as input.
returns 1 if n has even number of prime factors and -1 otherwise.
>>> liouville_lambda(10)
1
>>> liouville_lambda(11)
-1
>>> liouville_lambda(0)
Traceback (most recent call last):
...
ValueError: Input must be a positive integer
>>> liouville_lambda(-1)
Traceback (most recent call last):
...
ValueError: Input must be a positive integer
>>> liouville_lambda(11.0)
Traceback (most recent call last):
...
TypeError: Input value of [number=11.0] must be an integer
"""
ifnotisinstance(number, int):
msg=f"Input value of [number={number}] must be an integer"
raiseTypeError(msg)
ifnumber<1:
raiseValueError("Input must be a positive integer")
return-1iflen(prime_factors(number)) %2else1
if__name__=="__main__":
importdoctest
doctest.testmod()