From 60e0dfe0b4fad0e39055a36be502f23412ebc821 Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 07:38:03 +0800 Subject: [PATCH 1/6] add type hints to math/sieve --- maths/sieve_of_eratosthenes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index faf6fc0f9a98..d1e312577dbf 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -13,9 +13,9 @@ import math +from typing import List - -def sieve(n): +def sieve(n: int) -> List[int]: """ Returns a list with all prime numbers up to n. From 70d6f141d6ca82169f90e9729136d595d66e621d Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 07:55:25 +0800 Subject: [PATCH 2/6] add doctest --- maths/sieve_of_eratosthenes.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index d1e312577dbf..a5706faa6226 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -15,6 +15,7 @@ import math from typing import List + def sieve(n: int) -> List[int]: """ Returns a list with all prime numbers up to n. @@ -58,4 +59,7 @@ def sieve(n: int) -> List[int]: if __name__ == "__main__": + import doctest + + doctest.testmod() print(sieve(int(input("Enter n: ").strip()))) From 3522b05d4e4b3fe239a6344c3d652dddd9a9227d Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 08:08:07 +0800 Subject: [PATCH 3/6] math/sieve: remove manual doctest --- maths/sieve_of_eratosthenes.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index a5706faa6226..e4f39b8ea3ba 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -59,7 +59,4 @@ def sieve(n: int) -> List[int]: if __name__ == "__main__": - import doctest - - doctest.testmod() print(sieve(int(input("Enter n: ").strip()))) From 1f376e865afad8cf6912f41fc744d618428db96e Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 14:26:52 +0800 Subject: [PATCH 4/6] add check for negative --- maths/sieve_of_eratosthenes.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index e4f39b8ea3ba..2b70b898bde6 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -34,6 +34,9 @@ def sieve(n: int) -> List[int]: [] """ + if n <= 0: + raise ValueError("Please enter positive integers.") + l = [True] * (n + 1) # noqa: E741 prime = [] start = 2 From 9dacd495424639047265bb5709f3e57f546d83aa Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 23 Nov 2020 10:58:39 +0530 Subject: [PATCH 5/6] Update maths/sieve_of_eratosthenes.py --- maths/sieve_of_eratosthenes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index 2b70b898bde6..9ae9553c9a51 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -35,7 +35,8 @@ def sieve(n: int) -> List[int]: """ if n <= 0: - raise ValueError("Please enter positive integers.") + raise ValueError(f"{n}: Invalid input, please enter a positive integer.") + l = [True] * (n + 1) # noqa: E741 prime = [] From 75801d8df941927b2f21ef66423776c661992ba5 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 23 Nov 2020 11:03:39 +0530 Subject: [PATCH 6/6] Update sieve_of_eratosthenes.py --- maths/sieve_of_eratosthenes.py | 39 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/maths/sieve_of_eratosthenes.py b/maths/sieve_of_eratosthenes.py index 9ae9553c9a51..47a086546900 100644 --- a/maths/sieve_of_eratosthenes.py +++ b/maths/sieve_of_eratosthenes.py @@ -8,7 +8,7 @@ Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes doctest provider: Bruno Simas Hadlich (https://github.com/brunohadlich) -Also thanks Dmitry (https://github.com/LizardWizzard) for finding the problem +Also thanks to Dmitry (https://github.com/LizardWizzard) for finding the problem """ @@ -16,51 +16,50 @@ from typing import List -def sieve(n: int) -> List[int]: +def prime_sieve(num: int) -> List[int]: """ Returns a list with all prime numbers up to n. - >>> sieve(50) + >>> prime_sieve(50) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] - >>> sieve(25) + >>> prime_sieve(25) [2, 3, 5, 7, 11, 13, 17, 19, 23] - >>> sieve(10) + >>> prime_sieve(10) [2, 3, 5, 7] - >>> sieve(9) + >>> prime_sieve(9) [2, 3, 5, 7] - >>> sieve(2) + >>> prime_sieve(2) [2] - >>> sieve(1) + >>> prime_sieve(1) [] """ - if n <= 0: - raise ValueError(f"{n}: Invalid input, please enter a positive integer.") + if num <= 0: + raise ValueError(f"{num}: Invalid input, please enter a positive integer.") - - l = [True] * (n + 1) # noqa: E741 + sieve = [True] * (num + 1) prime = [] start = 2 - end = int(math.sqrt(n)) + end = int(math.sqrt(num)) while start <= end: # If start is a prime - if l[start] is True: + if sieve[start] is True: prime.append(start) # Set multiples of start be False - for i in range(start * start, n + 1, start): - if l[i] is True: - l[i] = False + for i in range(start * start, num + 1, start): + if sieve[i] is True: + sieve[i] = False start += 1 - for j in range(end + 1, n + 1): - if l[j] is True: + for j in range(end + 1, num + 1): + if sieve[j] is True: prime.append(j) return prime if __name__ == "__main__": - print(sieve(int(input("Enter n: ").strip()))) + print(prime_sieve(int(input("Enter a positive integer: ").strip())))