Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_prime solution #66

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions solutions/is_prime.py
Saeed-Emad marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module for determining whether a number is prime.

Module contents:
- is_prime: checks if a given number is prime.

Created on Jan 06, 2025
@author: Ibrahim Elmisbah
"""


def is_prime(n: int) -> bool:
"""Determine if a number is prime.

Parameters:
n: int, the number to check if prime

Returns:
bool: True if the number is prime, False otherwise

Raises:
AssertionError: if the argument is not an integer

>>> is_prime(23)
True
>>> is_prime(10)
False
>>> is_prime(17)
True
>>> is_prime(1)
False
>>> is_prime(-5)
False
"""
# Ensure the input is an integer
assert isinstance(n, int), "input must be an integer"

# If n is less than or equal to 1, return False (not prime)
if n <= 1:
return False

# Check divisibility from 2 to sqrt(n)
for i in range(2, int(n**0.5) + 1):
# If n is divisible by any number between 2 and sqrt(n), it's not prime
if n % i == 0:
return False

# If no divisors were found, n is prime
return True
99 changes: 99 additions & 0 deletions solutions/tests/test_is_prime.py
Saeed-Emad marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Unit tests for the is_prime function.

This module contains unit tests for the is_prime function.
"""

import unittest
from ..is_prime import is_prime


class TestIsPrime(unittest.TestCase):
"""
Test cases for the is_prime function.

This class contains tests to verify the correctness of the is_prime function,
including boundary cases, defensive assertions, and expected behavior for
prime and non-prime numbers.
"""

def test_negative_number(self):
"""
Test that negative numbers are not considered prime.
"""
actual = is_prime(-5)
expected = False
self.assertEqual(actual, expected)

def test_zero(self):
"""
Test that zero is not considered prime.
"""
actual = is_prime(0)
expected = False
self.assertEqual(actual, expected)

def test_one(self):
"""
Test that one is not considered prime.
"""
actual = is_prime(1)
expected = False
self.assertEqual(actual, expected)

def test_small_prime(self):
"""
Test that a small prime number returns True.
"""
actual = is_prime(7)
expected = True
self.assertEqual(actual, expected)

def test_small_non_prime(self):
"""
Test that a small non-prime number returns False.
"""
actual = is_prime(4)
expected = False
self.assertEqual(actual, expected)

def test_large_prime(self):
"""
Test that a large prime number returns True.
"""
actual = is_prime(7919)
expected = True
self.assertEqual(actual, expected)

def test_large_non_prime(self):
"""
Test that a large non-prime number returns False.
"""
actual = is_prime(8000)
expected = False
self.assertEqual(actual, expected)

def test_defensive_assertion_non_integer(self):
"""
Test that a non-integer input raises an AssertionError.
"""
with self.assertRaises(AssertionError):
is_prime("not a number")

def test_boundary_case_two(self):
"""
Test that two is correctly identified as prime.
"""
actual = is_prime(2)
expected = True
self.assertEqual(actual, expected)

def test_boundary_case_three(self):
"""
Test that three is correctly identified as prime.
"""
actual = is_prime(3)
expected = True
self.assertEqual(actual, expected)
Loading