Skip to content
Open
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
89 changes: 88 additions & 1 deletion Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,91 @@ def exponent(number1, number2):
ans = 1
for _ in range(0,number2):
ans *= number1
return ans
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}")
Binary file added __pycache__/Functions.cpython-312.pyc
Binary file not shown.
18 changes: 17 additions & 1 deletion calculator_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pytest
import pytest # pyright: ignore[reportMissingImports]
import Functions

def test_add():
Expand All @@ -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)