diff --git a/primality/primality.py b/primality/primality.py index a738922..95a936f 100644 --- a/primality/primality.py +++ b/primality/primality.py @@ -210,3 +210,64 @@ def rand_prime(m: int, n: int, strategy: RandomStrategy = RandomStrategy.RANDOM_ return secrets.SystemRandom().choice(found_primes) else: raise ValueError("Couldn't find the selected strategy") + +def next_prime_after_operation(n : int, m : int, operation : str): + """ + Arguments: + {n} integer -- The Starting index of Range + {m} integer -- The Ending index of Range + {string} string -- The operation to be performed + + Returns: + integer -- The next prime number after the sum of the operation + """ + if not isinstance(n, int): + raise TypeError("next_prime_after_sum() expect parameter n to be int. Given: " + + str(type(n)) + '.') + if not isinstance(m, int): + raise TypeError("next_prime_after_sum() expect parameter m to be int. Given: " + + str(type(m)) + '.') + if not isinstance(operation, str): + raise TypeError("next_prime_after_sum() expect parameter string to be string. Given: " + + str(type(operation)) + '.') + if operation == "+": + return next_prime(n + m) + elif operation == "-": + return next_prime(n - m) + elif operation == "*": + return next_prime(n * m) + elif operation == "/": + return next_prime(int(n / m)) + else: + raise ValueError("Couldn't find the selected operation") + + +def prev_prime_before_operation(n : int, m : int, operation : str): + """ + Arguments: + {n} integer -- The Starting index of Range + {m} integer -- The Ending index of Range + {string} string -- The operation to be performed + + Returns: + integer -- The previous prime number before the operation + """ + if not isinstance(n, int): + raise TypeError("prev_prime_before_op() expect parameter n to be int. Given: " + + str(type(n)) + '.') + if not isinstance(m, int): + raise TypeError("prev_prime_before_op() expect parameter m to be int. Given: " + + str(type(m)) + '.') + if not isinstance(operation, str): + raise TypeError("prev_prime_before_op() expect parameter string to be string. Given: " + + str(type(operation)) + '.') + if operation == "+": + return prev_prime(n + m) + elif operation == "-": + return prev_prime(n - m) + elif operation == "*": + return prev_prime(n * m) + elif operation == "/": + return prev_prime(int(n / m)) + else: + raise ValueError("Couldn't find the selected operation") \ No newline at end of file diff --git a/tests/test_primes.py b/tests/test_primes.py index 200e5ad..a07ecce 100644 --- a/tests/test_primes.py +++ b/tests/test_primes.py @@ -65,6 +65,36 @@ def test_rand_primes_random_lib(self): self.assertGreater(random_prime, lower_bound) self.assertLess(random_prime, upper_bound) + def test_next_prime_after_operation_sum(self): + next_prime_after_sum = primality.next_prime_after_operation(10, 20, '+') + self.assertEqual(next_prime_after_sum, 31) + def test_next_prime_after_operation_subtraction(self): + next_prime_after_subtraction = primality.next_prime_after_operation(20, 10, '-') + self.assertEqual(next_prime_after_subtraction, 11) + + def test_next_prime_after_operation_multiplication(self): + next_prime_after_multiplication = primality.next_prime_after_operation(10, 10, '*') + self.assertEqual(next_prime_after_multiplication, 101) + + def test_next_prime_after_operation_division(self): + next_prime_after_division = primality.next_prime_after_operation(100, 10, '/') + self.assertEqual(next_prime_after_division, 11) + + def test_prev_prime_before_operation_sum(self): + prev_prime_before_sum = primality.prev_prime_before_operation(10, 20, '+') + self.assertEqual(prev_prime_before_sum, 29) + + def test_prev_prime_before_operation_subtraction(self): + prev_prime_before_subtraction = primality.prev_prime_before_operation(20, 10, '-') + self.assertEqual(prev_prime_before_subtraction, 7) + + def test_prev_prime_before_operation_multiplication(self): + prev_prime_before_multiplication = primality.prev_prime_before_operation(10, 10, '*') + self.assertEqual(prev_prime_before_multiplication, 97) + + def test_prev_prime_before_operation_division(self): + prev_prime_before_division = primality.prev_prime_before_operation(100, 10, '/') + self.assertEqual(prev_prime_before_division, 7) if __name__ == '__main__': unittest.main()