Skip to content

Commit

Permalink
Merge pull request #37 from MIT-Emerging-Talent/check-if-coprime
Browse files Browse the repository at this point in the history
Check if coprime
  • Loading branch information
Melat-arch authored Jan 12, 2025
2 parents 98b3e8d + c626017 commit 4f7d020
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
53 changes: 53 additions & 0 deletions solutions/check_coprime.py
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions solutions/tests/test_check_coprime.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 4f7d020

Please sign in to comment.