Skip to content

Commit 0653011

Browse files
committed
Added three randomized algorithms: Reservoir sampling, Random quicksort, Miller-Rabin primality Test
1 parent 709c18e commit 0653011

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import random
2+
3+
def miller_rabin_primality_test(n: int, k: int = 5) -> bool:
4+
"""
5+
Probabilistic primality test using Miller-Rabin algorithm.
6+
7+
Args:
8+
n (int): Number to test for primality.
9+
k (int): Number of iterations for accuracy.
10+
11+
Returns:
12+
bool: True if n is probably prime, False if composite.
13+
14+
Examples:
15+
>>> miller_rabin_primality_test(2)
16+
True
17+
>>> miller_rabin_primality_test(15)
18+
False
19+
>>> miller_rabin_primality_test(17)
20+
True
21+
"""
22+
if n <= 1:
23+
return False
24+
if n <= 3:
25+
return True
26+
if n % 2 == 0:
27+
return False
28+
29+
# Write n-1 as 2^r * d
30+
d = n - 1
31+
r = 0
32+
while d % 2 == 0:
33+
d //= 2
34+
r += 1
35+
36+
for _ in range(k):
37+
a = random.randint(2, n - 2)
38+
x = pow(a, d, n)
39+
if x == 1 or x == n - 1:
40+
continue
41+
for _ in range(r - 1):
42+
x = pow(x, 2, n)
43+
if x == n - 1:
44+
break
45+
else:
46+
return False
47+
return True
48+
49+
if __name__ == "__main__":
50+
import doctest
51+
doctest.testmod()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import random
2+
from typing import List
3+
4+
def randomized_quicksort(arr: List[int]) -> List[int]:
5+
"""
6+
Sorts a list of integers using randomized QuickSort algorithm.
7+
8+
Args:
9+
arr (List[int]): List of integers to sort.
10+
11+
Returns:
12+
List[int]: Sorted list of integers.
13+
14+
Examples:
15+
>>> randomized_quicksort([3, 6, 1, 8, 4])
16+
[1, 3, 4, 6, 8]
17+
>>> randomized_quicksort([])
18+
[]
19+
"""
20+
if len(arr) <= 1:
21+
return arr
22+
pivot = random.choice(arr)
23+
less = [x for x in arr if x < pivot]
24+
equal = [x for x in arr if x == pivot]
25+
greater = [x for x in arr if x > pivot]
26+
return randomized_quicksort(less) + equal + randomized_quicksort(greater)
27+
28+
if __name__ == "__main__":
29+
import doctest
30+
doctest.testmod()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import random
2+
from typing import List, Iterator, TypeVar
3+
4+
T = TypeVar("T")
5+
6+
def reservoir_sampling(stream: Iterator[T], k: int) -> List[T]:
7+
"""
8+
Randomly select k items from a stream of unknown length using reservoir sampling.
9+
10+
Args:
11+
stream (Iterator[T]): Input data stream.
12+
k (int): Number of items to sample.
13+
14+
Returns:
15+
List[T]: List of k randomly sampled items.
16+
17+
Examples:
18+
>>> stream = iter(range(1, 11))
19+
>>> len(reservoir_sampling(stream, 5))
20+
5
21+
"""
22+
reservoir: List[T] = []
23+
for i, item in enumerate(stream):
24+
if i < k:
25+
reservoir.append(item)
26+
else:
27+
j = random.randint(0, i)
28+
if j < k:
29+
reservoir[j] = item
30+
return reservoir
31+
32+
if __name__ == "__main__":
33+
import doctest
34+
doctest.testmod()

0 commit comments

Comments
 (0)