From 46432d477719c9890164fa00d0af703ed563ea5d Mon Sep 17 00:00:00 2001 From: Mehal Srivastava Date: Fri, 17 Oct 2025 09:52:06 +0530 Subject: [PATCH 1/2] test --- maths/sieve_of_sundaram.py | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 maths/sieve_of_sundaram.py diff --git a/maths/sieve_of_sundaram.py b/maths/sieve_of_sundaram.py new file mode 100644 index 000000000000..c42e1198a952 --- /dev/null +++ b/maths/sieve_of_sundaram.py @@ -0,0 +1,89 @@ +""" +Sieve of Sundaram Algorithm + +The Sieve of Sundaram is an algorithm for finding prime numbers up to a specified integer. +It was discovered by Indian mathematician S. P. Sundaram in 1934. + +Wikipedia: https://en.wikipedia.org/wiki/Sieve_of_Sundaram +""" + +from typing import List + + +def sieve_of_sundaram(limit: int) -> List[int]: + """ + Generate all prime numbers up to the given limit using Sieve of Sundaram. + + The algorithm works by creating a list of integers and marking composite numbers, + then extracting the remaining unmarked numbers which represent primes. + + Args: + limit: Upper bound (exclusive) for finding primes + + Returns: + List of all prime numbers less than the limit + + Raises: + ValueError: If limit is negative + + Examples: + >>> sieve_of_sundaram(30) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] + >>> sieve_of_sundaram(10) + [2, 3, 5, 7] + >>> sieve_of_sundaram(2) + [] + >>> sieve_of_sundaram(4) + [2, 3] + >>> sieve_of_sundaram(1) + [] + >>> sieve_of_sundaram(0) + [] + >>> sieve_of_sundaram(-5) + Traceback (most recent call last): + ... + ValueError: limit must be non-negative + """ + if limit < 0: + raise ValueError("limit must be non-negative") + + if limit <= 2: + return [] + + # Calculate the range for Sundaram sieve + # We need to find primes up to 'limit', so we work with (limit-1)//2 + n = (limit - 1) // 2 + + # Create a boolean array and initialize all entries as not marked (False) + # marked[i] represents whether (2*i + 1) is composite + marked = [False] * (n + 1) + + # Mark numbers using Sundaram's formula: i + j + 2*i*j + # where i <= j and i + j + 2*i*j <= n + for i in range(1, n + 1): + j = i + while i + j + 2 * i * j <= n: + marked[i + j + 2 * i * j] = True + j += 1 + + # Collect unmarked numbers and transform them to get primes + primes = [2] # 2 is the only even prime + + for i in range(1, n + 1): + if not marked[i]: + primes.append(2 * i + 1) + + return primes + + +if __name__ == "__main__": + print("Sieve of Sundaram Demo") + print("-" * 20) + + test_limits = [10, 30, 50, 100] + + for limit in test_limits: + primes = sieve_of_sundaram(limit) + print(f"Primes up to {limit}: {primes}") + print(f"Count: {len(primes)}") + print() From ff0b3a46e0f890156526d242d9374ff82afbf6a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 17 Oct 2025 04:25:39 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/sieve_of_sundaram.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/maths/sieve_of_sundaram.py b/maths/sieve_of_sundaram.py index c42e1198a952..c099bd60aa0f 100644 --- a/maths/sieve_of_sundaram.py +++ b/maths/sieve_of_sundaram.py @@ -13,19 +13,19 @@ def sieve_of_sundaram(limit: int) -> List[int]: """ Generate all prime numbers up to the given limit using Sieve of Sundaram. - + The algorithm works by creating a list of integers and marking composite numbers, then extracting the remaining unmarked numbers which represent primes. - + Args: limit: Upper bound (exclusive) for finding primes - + Returns: List of all prime numbers less than the limit - + Raises: ValueError: If limit is negative - + Examples: >>> sieve_of_sundaram(30) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] @@ -46,18 +46,18 @@ def sieve_of_sundaram(limit: int) -> List[int]: """ if limit < 0: raise ValueError("limit must be non-negative") - + if limit <= 2: return [] - + # Calculate the range for Sundaram sieve # We need to find primes up to 'limit', so we work with (limit-1)//2 n = (limit - 1) // 2 - + # Create a boolean array and initialize all entries as not marked (False) # marked[i] represents whether (2*i + 1) is composite marked = [False] * (n + 1) - + # Mark numbers using Sundaram's formula: i + j + 2*i*j # where i <= j and i + j + 2*i*j <= n for i in range(1, n + 1): @@ -65,23 +65,23 @@ def sieve_of_sundaram(limit: int) -> List[int]: while i + j + 2 * i * j <= n: marked[i + j + 2 * i * j] = True j += 1 - + # Collect unmarked numbers and transform them to get primes primes = [2] # 2 is the only even prime - + for i in range(1, n + 1): if not marked[i]: primes.append(2 * i + 1) - + return primes if __name__ == "__main__": print("Sieve of Sundaram Demo") print("-" * 20) - + test_limits = [10, 30, 50, 100] - + for limit in test_limits: primes = sieve_of_sundaram(limit) print(f"Primes up to {limit}: {primes}")