Skip to content

Commit

Permalink
Merge pull request #27 from UBC-MDS/main
Browse files Browse the repository at this point in the history
Write test cases and code for is_prime function
  • Loading branch information
Calista321 authored Jan 15, 2025
2 parents c8fdc25 + 3dc4ef0 commit 7f22412
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 9 deletions.
29 changes: 28 additions & 1 deletion src/num_theory/prime_factorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,31 @@ def prime_factorization(n):
- For large values of `n`, consider optimized algorithms such as Pollard's Rho or
the Quadratic Sieve for better performance.
"""
pass

# Defensive input validation
if not isinstance(n, int):
raise ValueError("Input must be an integer.")
if n <= 1:
raise ValueError("Input must be a positive integer greater than 1.")

# Prime factorization using trial division
factors = []
divisor = 2

while n > 1:
power = 0
while n % divisor == 0:
n //= divisor
power += 1
if power > 0:
factors.append((divisor, power))
divisor += 1
# Optimization: Stop if divisor squared is greater than n
if divisor * divisor > n:
break

# If n is still greater than 1, it must be a prime number
if n > 1:
factors.append((n, 1))

return factors
41 changes: 33 additions & 8 deletions tests/test_arithmetic_progression.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,42 @@ def test_zero_terms():
# except ValueError as e:
# assert str(e) == "The number of terms 'n' must be a positive integer."

def test_a_not_numeric():
@pytest.mark.parametrize(
"a",
[
[2,4,5],
{3,4},
{'key':'val'}
]
)
def test_not_right_variables(a):
with pytest.raises(TypeError):
arithmetic_progression(a=[1], d=2, n=5)

def test_d_not_numeric():
arithmetic_progression(a, d = 2, n=5)

@pytest.mark.parametrize(
"d",
[
[2,4,5],
{3,4},
{'key':'val'}
]
)
def test_d_not_numeric(d):
with pytest.raises(TypeError):
arithmetic_progression(a=1, d=[2], n=5)

def test_n_not_int():
arithmetic_progression(a=1, d=d, n=5)

@pytest.mark.parametrize(
"n",
[
[2,4,5],
{3,4},
{'key':'val'},
3.2
]
)
def test_n_not_int(n):
with pytest.raises(TypeError):
arithmetic_progression(a=1, d=2, n=[5])
arithmetic_progression(a=1, d=2, n=n)

def test_negative_terms():
"""Test when n is negative, which should raise a ValueError."""
Expand Down
41 changes: 41 additions & 0 deletions tests/test_prime_factorization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from num_theory.prime_factorization import prime_factorization
import pytest

def test_simple_cases():
"""Test prime factorization of simple numbers."""
assert prime_factorization(2) == [(2, 1)] # Smallest prime
assert prime_factorization(3) == [(3, 1)] # Another prime
assert prime_factorization(4) == [(2, 2)] # 4 = 2^2
assert prime_factorization(5) == [(5, 1)] # Another prime


def test_composite_numbers():
"""Test prime factorization of composite numbers."""
assert prime_factorization(28) == [(2, 2), (7, 1)] # 28 = 2^2 * 7
assert prime_factorization(100) == [(2, 2), (5, 2)] # 100 = 2^2 * 5^2
assert prime_factorization(84) == [(2, 2), (3, 1), (7, 1)] # 84 = 2^2 * 3 * 7


def test_large_numbers():
"""Test prime factorization of large numbers."""
assert prime_factorization(2 ** 10) == [(2, 10)] # Power of a single prime
assert prime_factorization(3 ** 5) == [(3, 5)] # Power of another prime
assert prime_factorization(2 ** 3 * 3 ** 2 * 5) == [(2, 3), (3, 2), (5, 1)] # Mixed primes


def test_edge_cases():
"""Test edge cases for input validation."""
with pytest.raises(ValueError, match="Input must be a positive integer greater than 1."):
prime_factorization(1) # n must be > 1
with pytest.raises(ValueError, match="Input must be a positive integer greater than 1."):
prime_factorization(-10) # Negative numbers are invalid
with pytest.raises(ValueError, match="Input must be a positive integer greater than 1."):
prime_factorization(0) # Zero is not valid
with pytest.raises(ValueError, match="Input must be an integer."):
prime_factorization(2.5) # Non-integers are invalid


def test_performance():
"""Test performance for a very large number."""
result = prime_factorization(2 ** 20 * 3 ** 15) # Large composite number
assert result == [(2, 20), (3, 15)] # Expect accurate results even for large inputs

0 comments on commit 7f22412

Please sign in to comment.