-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added Mobius Function #1058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added Mobius Function #1058
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
55226d9
Add files via upload
QuantumNovice 2f069f5
Update mobius_function.py
QuantumNovice 7df62b7
Add files via upload
QuantumNovice b79bddb
Add files via upload
QuantumNovice 6393f22
Add files via upload
QuantumNovice e00ff91
Add files via upload
QuantumNovice b577a52
Add files via upload
QuantumNovice aba05df
Add files via upload
QuantumNovice e18d9b2
Update mobius_function.py
QuantumNovice cbb584b
Delete mobius_function.py
QuantumNovice 77b29ce
Add files via upload
QuantumNovice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,39 @@ | ||
""" | ||
References: wikipedia:square free number | ||
python/black : True | ||
flake8 : True | ||
""" | ||
from typing import List | ||
|
||
|
||
def is_square_free(factors: List[int]) -> bool: | ||
""" | ||
# doctest: +NORMALIZE_WHITESPACE | ||
This functions takes a list of prime factors as input. | ||
returns True if the factors are square free. | ||
>>> is_square_free([1, 1, 2, 3, 4]) | ||
False | ||
|
||
These are wrong but should return some value | ||
it simply checks for repition in the numbers. | ||
>>> is_square_free([1, 3, 4, 'sd', 0.0]) | ||
True | ||
|
||
>>> is_square_free([1, 0.5, 2, 0.0]) | ||
True | ||
>>> is_square_free([1, 2, 2, 5]) | ||
False | ||
>>> is_square_free('asd') | ||
True | ||
>>> is_square_free(24) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: 'int' object is not iterable | ||
""" | ||
return len(set(factors)) == len(factors) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
This file contains hidden or 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,43 @@ | ||
""" | ||
Refrences: 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.