|
| 1 | +""" |
| 2 | +Sieve of Sundaram Algorithm |
| 3 | +
|
| 4 | +The Sieve of Sundaram is an algorithm for finding prime numbers up to a specified integer. |
| 5 | +It was discovered by Indian mathematician S. P. Sundaram in 1934. |
| 6 | +
|
| 7 | +Wikipedia: https://en.wikipedia.org/wiki/Sieve_of_Sundaram |
| 8 | +""" |
| 9 | + |
| 10 | +from typing import List |
| 11 | + |
| 12 | + |
| 13 | +def sieve_of_sundaram(limit: int) -> List[int]: |
| 14 | + """ |
| 15 | + Generate all prime numbers up to the given limit using Sieve of Sundaram. |
| 16 | + |
| 17 | + The algorithm works by creating a list of integers and marking composite numbers, |
| 18 | + then extracting the remaining unmarked numbers which represent primes. |
| 19 | + |
| 20 | + Args: |
| 21 | + limit: Upper bound (exclusive) for finding primes |
| 22 | + |
| 23 | + Returns: |
| 24 | + List of all prime numbers less than the limit |
| 25 | + |
| 26 | + Raises: |
| 27 | + ValueError: If limit is negative |
| 28 | + |
| 29 | + Examples: |
| 30 | + >>> sieve_of_sundaram(30) |
| 31 | + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] |
| 32 | + >>> sieve_of_sundaram(10) |
| 33 | + [2, 3, 5, 7] |
| 34 | + >>> sieve_of_sundaram(2) |
| 35 | + [] |
| 36 | + >>> sieve_of_sundaram(4) |
| 37 | + [2, 3] |
| 38 | + >>> sieve_of_sundaram(1) |
| 39 | + [] |
| 40 | + >>> sieve_of_sundaram(0) |
| 41 | + [] |
| 42 | + >>> sieve_of_sundaram(-5) |
| 43 | + Traceback (most recent call last): |
| 44 | + ... |
| 45 | + ValueError: limit must be non-negative |
| 46 | + """ |
| 47 | + if limit < 0: |
| 48 | + raise ValueError("limit must be non-negative") |
| 49 | + |
| 50 | + if limit <= 2: |
| 51 | + return [] |
| 52 | + |
| 53 | + # Calculate the range for Sundaram sieve |
| 54 | + # We need to find primes up to 'limit', so we work with (limit-1)//2 |
| 55 | + n = (limit - 1) // 2 |
| 56 | + |
| 57 | + # Create a boolean array and initialize all entries as not marked (False) |
| 58 | + # marked[i] represents whether (2*i + 1) is composite |
| 59 | + marked = [False] * (n + 1) |
| 60 | + |
| 61 | + # Mark numbers using Sundaram's formula: i + j + 2*i*j |
| 62 | + # where i <= j and i + j + 2*i*j <= n |
| 63 | + for i in range(1, n + 1): |
| 64 | + j = i |
| 65 | + while i + j + 2 * i * j <= n: |
| 66 | + marked[i + j + 2 * i * j] = True |
| 67 | + j += 1 |
| 68 | + |
| 69 | + # Collect unmarked numbers and transform them to get primes |
| 70 | + primes = [2] # 2 is the only even prime |
| 71 | + |
| 72 | + for i in range(1, n + 1): |
| 73 | + if not marked[i]: |
| 74 | + primes.append(2 * i + 1) |
| 75 | + |
| 76 | + return primes |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + print("Sieve of Sundaram Demo") |
| 81 | + print("-" * 20) |
| 82 | + |
| 83 | + test_limits = [10, 30, 50, 100] |
| 84 | + |
| 85 | + for limit in test_limits: |
| 86 | + primes = sieve_of_sundaram(limit) |
| 87 | + print(f"Primes up to {limit}: {primes}") |
| 88 | + print(f"Count: {len(primes)}") |
| 89 | + print() |
0 commit comments