From 55226d91b2bb1c2ee17dd857b0bb6c7d785ebad5 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sat, 20 Jul 2019 19:47:14 +0500 Subject: [PATCH 01/11] Add files via upload --- maths/is_square_free.py | 42 ++++++++++++++++++++++++++++++++++++++++ maths/mobius_function.py | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 maths/is_square_free.py create mode 100644 maths/mobius_function.py diff --git a/maths/is_square_free.py b/maths/is_square_free.py new file mode 100644 index 000000000000..4dbf1b235689 --- /dev/null +++ b/maths/is_square_free.py @@ -0,0 +1,42 @@ +''' +References: wikipedia:square free number +''' +from typing import List + +def is_square_free(factors:List[int]) -> bool: + ''' + 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 numb + >>> 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 + ''' + i: int + if len(set(factors)) != len(factors): + return False + return True + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/mobius_function.py b/maths/mobius_function.py new file mode 100644 index 000000000000..2217b9b05f25 --- /dev/null +++ b/maths/mobius_function.py @@ -0,0 +1,42 @@ +''' +Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function +''' + +from prime_factors import prime_factors +from 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): + if len(factors)%2 == 0: + return 1 + elif len(factors)%2 != 0: + return -1 + else: + return 0 + +if __name__ == '__main__': + import doctest + doctest.testmod() From 2f069f540b6b2ed916c8462134a3881ef85ee7fe Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sat, 20 Jul 2019 19:56:59 +0500 Subject: [PATCH 02/11] Update mobius_function.py --- maths/mobius_function.py | 118 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/maths/mobius_function.py b/maths/mobius_function.py index 2217b9b05f25..7c9345f2e6aa 100644 --- a/maths/mobius_function.py +++ b/maths/mobius_function.py @@ -2,7 +2,123 @@ Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function ''' -from prime_factors import prime_factors +from prime_factors import prime_factors''' +Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function +References: wikipedia:square free number +''' + +from typing import List + + + +def prime_factors(n: int) -> List[int]: + """ + Returns prime factors of n as a list. + + >>> prime_factors(0) + [] + >>> prime_factors(100) + [2, 2, 5, 5] + >>> prime_factors(2560) + [2, 2, 2, 2, 2, 2, 2, 2, 2, 5] + >>> prime_factors(10**-2) + [] + >>> prime_factors(0.02) + [] + >>> x = prime_factors(10**241) # doctest: +NORMALIZE_WHITESPACE + >>> x == [2]*241 + [5]*241 + True + >>> prime_factors(10**-354) + [] + >>> prime_factors('hello') + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and 'str' + >>> prime_factors([1,2,'hello']) + Traceback (most recent call last): + ... + TypeError: '<=' not supported between instances of 'int' and '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 is_square_free(factors:List[int]) -> bool: + ''' + 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 numb + >>> 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 + ''' + i: int + if len(set(factors)) != len(factors): + return False + return True + +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): + if len(factors)%2 == 0: + return 1 + elif len(factors)%2 != 0: + return -1 + else: + return 0 + +if __name__ == '__main__': + import doctest + doctest.testmod() + from is_square_free import is_square_free From 7df62b75c1e2b74682e965175a831b5a53223a49 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sat, 20 Jul 2019 20:06:01 +0500 Subject: [PATCH 03/11] Add files via upload --- maths/mobius_function.py | 42 ---------------------------------------- 1 file changed, 42 deletions(-) diff --git a/maths/mobius_function.py b/maths/mobius_function.py index 7c9345f2e6aa..3e98af97d678 100644 --- a/maths/mobius_function.py +++ b/maths/mobius_function.py @@ -1,9 +1,5 @@ ''' Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function -''' - -from prime_factors import prime_factors''' -Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function References: wikipedia:square free number ''' @@ -84,44 +80,6 @@ def is_square_free(factors:List[int]) -> bool: return False return True -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): - if len(factors)%2 == 0: - return 1 - elif len(factors)%2 != 0: - return -1 - else: - return 0 - -if __name__ == '__main__': - import doctest - doctest.testmod() - -from is_square_free import is_square_free - - def mobius(n:int) -> int: ''' Mobius function From b79bddb2335cf208ef8e02893331f699256e6731 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sat, 20 Jul 2019 20:11:14 +0500 Subject: [PATCH 04/11] Add files via upload --- maths/mobius_function.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/mobius_function.py b/maths/mobius_function.py index 3e98af97d678..065189528b73 100644 --- a/maths/mobius_function.py +++ b/maths/mobius_function.py @@ -108,8 +108,7 @@ def mobius(n:int) -> int: return 1 elif len(factors)%2 != 0: return -1 - else: - return 0 + return 0 if __name__ == '__main__': import doctest From 6393f220f3fbb3a82b888eee9b5f5970158fbd35 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sat, 20 Jul 2019 21:24:45 +0500 Subject: [PATCH 05/11] Add files via upload --- maths/is_square_free.py | 35 +++++++++++++++----------------- maths/mobius_function.py | 43 ++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/maths/is_square_free.py b/maths/is_square_free.py index 4dbf1b235689..acc13fa5f833 100644 --- a/maths/is_square_free.py +++ b/maths/is_square_free.py @@ -1,39 +1,36 @@ -''' +""" References: wikipedia:square free number -''' +python/black : True +flake8 : True +""" from typing import List -def is_square_free(factors:List[int]) -> bool: - ''' + +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]) + >>> is_square_free([1, 1, 2, 3, 4]) False - + These are wrong but should return some value - it simply checks for repition in the numb - >>> is_square_free([1,3,4,'sd', 0.0]) + 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]) + >>> is_square_free([1, 0.5, 2, 0.0]) True - - >>> is_square_free([1,2, 2, 5]) + >>> 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 - ''' - i: int - if len(set(factors)) != len(factors): - return False - return True + """ + return len(set(factors)) == len(factors) if __name__ == "__main__": diff --git a/maths/mobius_function.py b/maths/mobius_function.py index 065189528b73..ecda70078b94 100644 --- a/maths/mobius_function.py +++ b/maths/mobius_function.py @@ -1,12 +1,13 @@ -''' +""" Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function References: wikipedia:square free number -''' +python/black : True +flake8 : True +""" from typing import List - def prime_factors(n: int) -> List[int]: """ Returns prime factors of n as a list. @@ -30,7 +31,7 @@ def prime_factors(n: int) -> List[int]: Traceback (most recent call last): ... TypeError: '<=' not supported between instances of 'int' and 'str' - >>> prime_factors([1,2,'hello']) + >>> prime_factors([1, 2, 'hello']) Traceback (most recent call last): ... TypeError: '<=' not supported between instances of 'int' and 'list' @@ -48,23 +49,24 @@ def prime_factors(n: int) -> List[int]: factors.append(n) return factors -def is_square_free(factors:List[int]) -> bool: - ''' + +def is_square_free(factors: List[int]) -> bool: + """ 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]) + >>> is_square_free([1, 1, 2, 3, 4]) False These are wrong but should return some value it simply checks for repition in the numb - >>> is_square_free([1,3,4,'sd', 0.0]) + >>> is_square_free([1, 3, 4, 'sd', 0.0]) True - >>> is_square_free([1,0.5, 2, 0.0]) + >>> is_square_free([1, 0.5, 2, 0.0]) True - >>> is_square_free([1,2, 2, 5]) + >>> is_square_free([1, 2, 2, 5]) False >>> is_square_free('asd') @@ -74,14 +76,14 @@ def is_square_free(factors:List[int]) -> bool: Traceback (most recent call last): ... TypeError: 'int' object is not iterable - ''' - i: int + """ if len(set(factors)) != len(factors): - return False + return False return True -def mobius(n:int) -> int: - ''' + +def mobius(n: int) -> int: + """ Mobius function >>> mobius(24) 0 @@ -101,15 +103,14 @@ def mobius(n:int) -> int: Traceback (most recent call last): ... TypeError: '<=' not supported between instances of 'int' and 'list' - ''' + """ factors = prime_factors(n) if is_square_free(factors): - if len(factors)%2 == 0: - return 1 - elif len(factors)%2 != 0: - return -1 + return -1 if len(factors) % 2 else 1 return 0 -if __name__ == '__main__': + +if __name__ == "__main__": import doctest + doctest.testmod() From e00ff91099ec3140da9f35861375b7fc5fca5c7a Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sat, 20 Jul 2019 21:48:38 +0500 Subject: [PATCH 06/11] Add files via upload --- mobius_function.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 mobius_function.py diff --git a/mobius_function.py b/mobius_function.py new file mode 100644 index 000000000000..3e49ae62de95 --- /dev/null +++ b/mobius_function.py @@ -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 typing import List +from .prime_factors import prime_factors +from .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() From b577a5209bfb9430c2bf81ddd1430a9554bc72c5 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sat, 20 Jul 2019 21:57:04 +0500 Subject: [PATCH 07/11] Add files via upload --- maths/mobius_function.py | 77 ++-------------------------------------- 1 file changed, 2 insertions(+), 75 deletions(-) diff --git a/maths/mobius_function.py b/maths/mobius_function.py index ecda70078b94..f8987e9075ed 100644 --- a/maths/mobius_function.py +++ b/maths/mobius_function.py @@ -6,81 +6,8 @@ """ from typing import List - - -def prime_factors(n: int) -> List[int]: - """ - Returns prime factors of n as a list. - - >>> prime_factors(0) - [] - >>> prime_factors(100) - [2, 2, 5, 5] - >>> prime_factors(2560) - [2, 2, 2, 2, 2, 2, 2, 2, 2, 5] - >>> prime_factors(10**-2) - [] - >>> prime_factors(0.02) - [] - >>> x = prime_factors(10**241) # doctest: +NORMALIZE_WHITESPACE - >>> x == [2]*241 + [5]*241 - True - >>> prime_factors(10**-354) - [] - >>> prime_factors('hello') - Traceback (most recent call last): - ... - TypeError: '<=' not supported between instances of 'int' and 'str' - >>> prime_factors([1, 2, 'hello']) - Traceback (most recent call last): - ... - TypeError: '<=' not supported between instances of 'int' and '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 is_square_free(factors: List[int]) -> bool: - """ - 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 numb - >>> 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 - """ - if len(set(factors)) != len(factors): - return False - return True - +from math.prime_factors import prime_factors +from math.is_square_free import is_square_free def mobius(n: int) -> int: """ From aba05dfe7972731cd1768a3d05b8a0fff22cd211 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sat, 20 Jul 2019 22:01:07 +0500 Subject: [PATCH 08/11] Add files via upload --- maths/mobius_function.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/mobius_function.py b/maths/mobius_function.py index f8987e9075ed..79ed432ab5da 100644 --- a/maths/mobius_function.py +++ b/maths/mobius_function.py @@ -6,8 +6,8 @@ """ from typing import List -from math.prime_factors import prime_factors -from math.is_square_free import is_square_free +from maths.prime_factors import prime_factors +from maths.is_square_free import is_square_free def mobius(n: int) -> int: """ From e18d9b2dd3773e0055ef8234da5ef267d61c19ec Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sat, 20 Jul 2019 22:08:02 +0500 Subject: [PATCH 09/11] Update mobius_function.py --- maths/mobius_function.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/mobius_function.py b/maths/mobius_function.py index 79ed432ab5da..fb7a7b60fe7f 100644 --- a/maths/mobius_function.py +++ b/maths/mobius_function.py @@ -36,7 +36,6 @@ def mobius(n: int) -> int: return -1 if len(factors) % 2 else 1 return 0 - if __name__ == "__main__": import doctest From cbb584b210940294266d54971c8133e4c262182a Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sun, 21 Jul 2019 13:25:53 +0500 Subject: [PATCH 10/11] Delete mobius_function.py --- mobius_function.py | 43 ------------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 mobius_function.py diff --git a/mobius_function.py b/mobius_function.py deleted file mode 100644 index 3e49ae62de95..000000000000 --- a/mobius_function.py +++ /dev/null @@ -1,43 +0,0 @@ -""" -Refrences: https://en.wikipedia.org/wiki/M%C3%B6bius_function -References: wikipedia:square free number -python/black : True -flake8 : True -""" - -from typing import List -from .prime_factors import prime_factors -from .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() From 77b29ceaf0e7665769847fa2f7267e40730027b6 Mon Sep 17 00:00:00 2001 From: QuantumNovice <43876848+QuantumNovice@users.noreply.github.com> Date: Sun, 21 Jul 2019 13:31:16 +0500 Subject: [PATCH 11/11] Add files via upload --- maths/mobius_function.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/mobius_function.py b/maths/mobius_function.py index fb7a7b60fe7f..15fb3d4380f4 100644 --- a/maths/mobius_function.py +++ b/maths/mobius_function.py @@ -5,10 +5,10 @@ flake8 : True """ -from typing import List from maths.prime_factors import prime_factors from maths.is_square_free import is_square_free + def mobius(n: int) -> int: """ Mobius function @@ -36,6 +36,7 @@ def mobius(n: int) -> int: return -1 if len(factors) % 2 else 1 return 0 + if __name__ == "__main__": import doctest