From 0653011002fa8ada4fb3e5d482788aee9c9693fe Mon Sep 17 00:00:00 2001 From: Mishu03 Date: Tue, 14 Oct 2025 20:52:13 +0530 Subject: [PATCH 1/2] Added three randomized algorithms: Reservoir sampling, Random quicksort, Miller-Rabin primality Test --- .../miller_rabin_primality test.py | 51 +++++++++++++++++++ randomized_algorithms/randomized_quicksort.py | 30 +++++++++++ randomized_algorithms/reservoir_sampling.py | 34 +++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 randomized_algorithms/miller_rabin_primality test.py create mode 100644 randomized_algorithms/randomized_quicksort.py create mode 100644 randomized_algorithms/reservoir_sampling.py diff --git a/randomized_algorithms/miller_rabin_primality test.py b/randomized_algorithms/miller_rabin_primality test.py new file mode 100644 index 000000000000..d5d25a7c18ab --- /dev/null +++ b/randomized_algorithms/miller_rabin_primality test.py @@ -0,0 +1,51 @@ +import random + +def miller_rabin_primality_test(n: int, k: int = 5) -> bool: + """ + Probabilistic primality test using Miller-Rabin algorithm. + + Args: + n (int): Number to test for primality. + k (int): Number of iterations for accuracy. + + Returns: + bool: True if n is probably prime, False if composite. + + Examples: + >>> miller_rabin_primality_test(2) + True + >>> miller_rabin_primality_test(15) + False + >>> miller_rabin_primality_test(17) + True + """ + if n <= 1: + return False + if n <= 3: + return True + if n % 2 == 0: + return False + + # Write n-1 as 2^r * d + d = n - 1 + r = 0 + while d % 2 == 0: + d //= 2 + r += 1 + + for _ in range(k): + a = random.randint(2, n - 2) + x = pow(a, d, n) + if x == 1 or x == n - 1: + continue + for _ in range(r - 1): + x = pow(x, 2, n) + if x == n - 1: + break + else: + return False + return True + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file diff --git a/randomized_algorithms/randomized_quicksort.py b/randomized_algorithms/randomized_quicksort.py new file mode 100644 index 000000000000..92d39ae87a53 --- /dev/null +++ b/randomized_algorithms/randomized_quicksort.py @@ -0,0 +1,30 @@ +import random +from typing import List + +def randomized_quicksort(arr: List[int]) -> List[int]: + """ + Sorts a list of integers using randomized QuickSort algorithm. + + Args: + arr (List[int]): List of integers to sort. + + Returns: + List[int]: Sorted list of integers. + + Examples: + >>> randomized_quicksort([3, 6, 1, 8, 4]) + [1, 3, 4, 6, 8] + >>> randomized_quicksort([]) + [] + """ + if len(arr) <= 1: + return arr + pivot = random.choice(arr) + less = [x for x in arr if x < pivot] + equal = [x for x in arr if x == pivot] + greater = [x for x in arr if x > pivot] + return randomized_quicksort(less) + equal + randomized_quicksort(greater) + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file diff --git a/randomized_algorithms/reservoir_sampling.py b/randomized_algorithms/reservoir_sampling.py new file mode 100644 index 000000000000..b17f369502b4 --- /dev/null +++ b/randomized_algorithms/reservoir_sampling.py @@ -0,0 +1,34 @@ +import random +from typing import List, Iterator, TypeVar + +T = TypeVar("T") + +def reservoir_sampling(stream: Iterator[T], k: int) -> List[T]: + """ + Randomly select k items from a stream of unknown length using reservoir sampling. + + Args: + stream (Iterator[T]): Input data stream. + k (int): Number of items to sample. + + Returns: + List[T]: List of k randomly sampled items. + + Examples: + >>> stream = iter(range(1, 11)) + >>> len(reservoir_sampling(stream, 5)) + 5 + """ + reservoir: List[T] = [] + for i, item in enumerate(stream): + if i < k: + reservoir.append(item) + else: + j = random.randint(0, i) + if j < k: + reservoir[j] = item + return reservoir + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From ea09d1f5e0ceba22762517e68eb3a08d32fe019e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Oct 2025 15:26:09 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- randomized_algorithms/miller_rabin_primality test.py | 5 ++++- randomized_algorithms/randomized_quicksort.py | 5 ++++- randomized_algorithms/reservoir_sampling.py | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/randomized_algorithms/miller_rabin_primality test.py b/randomized_algorithms/miller_rabin_primality test.py index d5d25a7c18ab..bf87eb0469e2 100644 --- a/randomized_algorithms/miller_rabin_primality test.py +++ b/randomized_algorithms/miller_rabin_primality test.py @@ -1,5 +1,6 @@ import random + def miller_rabin_primality_test(n: int, k: int = 5) -> bool: """ Probabilistic primality test using Miller-Rabin algorithm. @@ -46,6 +47,8 @@ def miller_rabin_primality_test(n: int, k: int = 5) -> bool: return False return True + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() diff --git a/randomized_algorithms/randomized_quicksort.py b/randomized_algorithms/randomized_quicksort.py index 92d39ae87a53..1001de3eeb87 100644 --- a/randomized_algorithms/randomized_quicksort.py +++ b/randomized_algorithms/randomized_quicksort.py @@ -1,6 +1,7 @@ import random from typing import List + def randomized_quicksort(arr: List[int]) -> List[int]: """ Sorts a list of integers using randomized QuickSort algorithm. @@ -25,6 +26,8 @@ def randomized_quicksort(arr: List[int]) -> List[int]: greater = [x for x in arr if x > pivot] return randomized_quicksort(less) + equal + randomized_quicksort(greater) + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() diff --git a/randomized_algorithms/reservoir_sampling.py b/randomized_algorithms/reservoir_sampling.py index b17f369502b4..485578f6d7c1 100644 --- a/randomized_algorithms/reservoir_sampling.py +++ b/randomized_algorithms/reservoir_sampling.py @@ -3,6 +3,7 @@ T = TypeVar("T") + def reservoir_sampling(stream: Iterator[T], k: int) -> List[T]: """ Randomly select k items from a stream of unknown length using reservoir sampling. @@ -29,6 +30,8 @@ def reservoir_sampling(stream: Iterator[T], k: int) -> List[T]: reservoir[j] = item return reservoir + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod()