diff --git a/solutions/check_coprime.py b/solutions/check_coprime.py new file mode 100644 index 000000000..30e8614ae --- /dev/null +++ b/solutions/check_coprime.py @@ -0,0 +1,53 @@ +"""Module to check if two numbers are relatively prime. + +This module defines a function to determine whether two numbers (Integer) are relatively +prime, meaning they have no common factors than 1. + +@author: Vahab +@Created: 11/01/2025 +""" + + +def check_coprime(first_integer: int, second_integer: int) -> bool: + """Check if two numbers are relatively prime. + + Two numbers are relatively prime when there + are no common factors other than 1 + + Parameters: + first_integer (int): The first number (must be a positive integer). + second_integer (int): The second number (must be a positive integer). + + + Returns: + bool: True if the numbers are relatively prime, False otherwise. + + Raises: + ValueError: If either of the inputs is not a positive integer. + TypeError: If either of the inputs is not an integer. + + >>> check_coprime(15, 28) + True + >>> check_coprime(8, 12) + False + >>> check_coprime(7, 9) + True + """ + + # Check if inputs are integers + if not isinstance(first_integer, int) or not isinstance(second_integer, int): + raise TypeError("Both inputs must be integers.") + + # Check if inputs are positive integers + if first_integer <= 0 or second_integer <= 0: + raise ValueError("Both numbers must be positive integers.") + + # Find the smaller of the two numbers + smaller = min(first_integer, second_integer) + + # Check for any common factors greater than 1 + for i in range(2, smaller + 1): + if first_integer % i == 0 and second_integer % i == 0: + return False + + return True diff --git a/solutions/tests/test_check_coprime.py b/solutions/tests/test_check_coprime.py new file mode 100644 index 000000000..0cd2afef5 --- /dev/null +++ b/solutions/tests/test_check_coprime.py @@ -0,0 +1,53 @@ +"""Tests for check_coprime function. + + +This module contains unit tests to ensure the check_coprime function works as expected, +including handling edge cases and defensive assertions. + +@author: Vahab +@created: 11/01/2025 +""" + +import unittest + + +from solutions.check_coprime import check_coprime + + +class TestCoprimeFunction(unittest.TestCase): + """Test cases for the check_coprime function.""" + + def test_coprime_true(self): + """Test case where the numbers are relatively prime.""" + self.assertTrue(check_coprime(15, 28)) + + def test_coprime_false(self): + """Test case where the numbers are not relatively prime.""" + self.assertFalse(check_coprime(8, 12)) + + def test_coprime_same_value(self): + """Test case where both numbers are the same and greater than 1.""" + self.assertFalse(check_coprime(12, 12)) + + def test_coprime_negative_input(self): + """Test case where negative numbers are provided.""" + with self.assertRaises(ValueError): + check_coprime(-5, 28) + + def test_coprime_non_integer_input(self): + """Test case where non-integer inputs are provided.""" + with self.assertRaises(TypeError): + check_coprime("15", 28) + + def test_coprime_large_numbers(self): + """Test case with large numbers to check performance.""" + self.assertTrue(check_coprime(1, 100)) + + def test_coprime_edge_case(self): + """Test case where one input is 0.""" + with self.assertRaises(ValueError): + check_coprime(0, 10) + + +if __name__ == "__main__": + unittest.main()