forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0bc166
commit 490757e
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
def is_square_free(factors): | ||
''' | ||
This functions takes a list of prime factors as input. | ||
returns True if the factors are square free. | ||
''' | ||
for i in factors: | ||
if factors.count(i) > 1: | ||
return False | ||
return True | ||
|
||
|
||
def prime_factors(n): | ||
''' | ||
Returns prime factors of n as a list. | ||
''' | ||
i = 2 | ||
factors = [] | ||
while i * i <= n: | ||
if n % i: | ||
i += 1 | ||
else: | ||
n //= i | ||
factors.append(i) | ||
if n > 1: | ||
factors.append(n) | ||
return factors | ||
|
||
def mobius_function(n): | ||
''' | ||
Defines Mobius function | ||
''' | ||
factors = prime_factors(n) | ||
if is_square_free(factors): | ||
if len(factors)%2 == 0: | ||
return 1 | ||
elif len(factors)%2 != 0: | ||
return -1 | ||
else: | ||
return 0 |