diff --git a/Functions.py b/Functions.py index 5fd95e8..0eba98a 100644 --- a/Functions.py +++ b/Functions.py @@ -23,4 +23,91 @@ def exponent(number1, number2): ans = 1 for _ in range(0,number2): ans *= number1 - return ans \ No newline at end of file + return ans + +#Function to perform factorial +def factorial(number): + ans = 1 + if number < 0: + ans = number + print("Please enter a non-negative number") + return + elif number == 0: + for i in range(1, number + 1): + ans = ans * i + print(f"The factorial of 0 is {ans}") + else: + for i in range(1, number + 1): + ans = ans * i + print(f"The factorial of {number} is {ans}") + return ans + +#Function to perform square root +def square_root(number): + try: + x = float(number) + except ValueError: + print("That isn’t a valid number.") + raise SystemExit + + if x < 0: + print("Cannot take the square root of a negative number (real domain).") + raise SystemExit + + if x == 0 or x == 1: + print(f"√{x} = {float(x)}") + raise SystemExit + + # Newton‑Raphson iteration. + # guess starts at x/2 (a decent generic starting point) + # stop when the change between successive guesses is < tolerance + # max_iter protects us from an infinite loop in pathological cases + tolerance = 1e-12 + max_iter = 100 + + guess = x / 2.0 + iteration = 0 + + while iteration < max_iter: + next_guess = (guess + x / guess) / 2.0 # g_{n+1} = (g_n + x/g_n)/2 + if abs(next_guess - guess) < tolerance: + guess = next_guess + break + guess = next_guess + iteration += 1 + + if iteration == max_iter: + # We didn’t reach the tolerance, but we still have a usable approximation. + print(f"Reached max iterations ({max_iter}). Approximation may be rough.") + print(f"√{x} ≈ {guess}") + +#Function to calculate greatest common denominator +def GCD(number1, number2): + try: + a = int(number1) + b = int(number2) + except ValueError: + print("Both inputs must be valid integers.") + raise SystemExit + + if a < 0 or b < 0: + print("GCD is defined for non‑negative integers only.") + raise SystemExit + + # Edge cases – if either number is zero, the GCD is the other number. + if a == 0: + print(f"GCD({a}, {b}) = {b}") + raise SystemExit + if b == 0: + print(f"GCD({a}, {b}) = {a}") + raise SystemExit + + # Euclidean algorithm (iterative version). + # While b is not zero, replace (a, b) with (b, a % b). + # When the loop finishes, a holds the GCD. + while b != 0: + remainder = a % b + a = b + b = remainder + + print(f"Greatest common denominator = {a}") \ No newline at end of file diff --git a/__pycache__/Functions.cpython-312.pyc b/__pycache__/Functions.cpython-312.pyc new file mode 100644 index 0000000..c9b0784 Binary files /dev/null and b/__pycache__/Functions.cpython-312.pyc differ diff --git a/calculator_test.py b/calculator_test.py index 63a80a6..d652401 100644 --- a/calculator_test.py +++ b/calculator_test.py @@ -1,4 +1,4 @@ -import pytest +import pytest # pyright: ignore[reportMissingImports] import Functions def test_add(): @@ -24,3 +24,19 @@ def test_mod(): def test_exponent(): assert Functions.exponent(2,8) == 256 assert Functions.exponent(100,3) == 1000000 + +def test_factorial(): + try: + assert Functions.factorial(-2) < 0, "Should return a negative value message\n" + except AssertionError as err: + print(err) + + try: + assert Functions.factorial(0) == 1, "Result should equal 1\n" + except AssertionError as err: + print(err) + + try: + assert Functions.factorial(5) == 120, "Result should equal 120" + except AssertionError as err: + print(err) \ No newline at end of file