From be0af2a542abf2ac8ee8b4e163efd24ad827db16 Mon Sep 17 00:00:00 2001 From: alvarezba Date: Wed, 1 Oct 2025 16:25:37 -0700 Subject: [PATCH 1/4] Added a function for calculating factorials --- Functions.py | 14 +++++++++++++- __pycache__/Functions.cpython-312.pyc | Bin 0 -> 1429 bytes 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 __pycache__/Functions.cpython-312.pyc diff --git a/Functions.py b/Functions.py index 5fd95e8..9689d67 100644 --- a/Functions.py +++ b/Functions.py @@ -23,4 +23,16 @@ 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: + print("Please enter a non-negative number") + elif number == 0: + print("The factorial of 0 is 1") + else: + for i in range(1, number + 1): + ans = ans * i + print("The factorial of ", number, " is", ans) \ 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 0000000000000000000000000000000000000000..caee1d6216d3788b44e28689c51785bc1a7bb196 GIT binary patch literal 1429 zcma)6&1(}u6rb6TWYc_&I`Ls@w9b?j_M`U^bPUdRrfvpoerRIt%NzGUpH?NzH zow`?&=ZKXhYTi!WGMw9PI+a#!BQu^+a~U_MIu=RYbj^%oSf)KuDAjpY(?|$tDRnan zu!L%nzH0VMWOz$B-GmS5$Xi3T<`JQ}5GWTSB0&4DL;|}m*>2h)YQ`ZnLV)g}6((VT zBiVuN2==_2bBsc+M0$ab5g(r&u5dVeKr@Plrjs~u$LOVx&kczl&duj7jr0L&;{U|7 zqD&^2!2rSGGUs$eh&0Z}n{83epeI3u%Dm6{$OFPMw-OSN&&?x+pWUNJ_Fy6f*F~b5 zSzQqb>jWG^paavcXl?+7|#oB0b@y>D_3enyfzDjt(~v z76&W1CWR|6mSfBOPaf9a!m*{htrj2L7Y@^KET0cxuzau&c|3zGNTe$>ezScFqY^w0 z*fI9U4xxL8Lpj;dC?9)DU6naW>)~Uc^FU5z+!aTJ-2sQ@&BkWi!f0hkfXGU4mh^*% z(Codlb2(kLbs0*c6Iqo_%N#fLtm+s=U2Z*BZ}9%2E-!%CEn=uS*;Up3XVXFlboO)Y}ho(PIt&KEErJ3ehkoVOf49#;#c0T6y1tV z?nswvq26lp%@A9U?nq-l;se#^YvrS+UY(G=czH|*_?&kFCL Date: Wed, 1 Oct 2025 22:45:09 -0700 Subject: [PATCH 2/4] Added a test for factorial function --- Functions.py | 9 +++++++-- calculator_test.py | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Functions.py b/Functions.py index 9689d67..0bdc3b3 100644 --- a/Functions.py +++ b/Functions.py @@ -29,10 +29,15 @@ def exponent(number1, number2): def factorial(number): ans = 1 if number < 0: + ans = number print("Please enter a non-negative number") + return elif number == 0: - print("The factorial of 0 is 1") + 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("The factorial of ", number, " is", ans) \ No newline at end of file + print(f"The factorial of {number} is {ans}") + return ans \ No newline at end of file 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 From 4bd683e712aa353c56e5a665e2667c6df06a52d3 Mon Sep 17 00:00:00 2001 From: alvarezba Date: Wed, 1 Oct 2025 23:42:43 -0700 Subject: [PATCH 3/4] Added a function to calculate square root --- Functions.py | 41 +++++++++++++++++++++++++- __pycache__/Functions.cpython-312.pyc | Bin 1429 -> 1571 bytes 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Functions.py b/Functions.py index 0bdc3b3..c75c367 100644 --- a/Functions.py +++ b/Functions.py @@ -40,4 +40,43 @@ def factorial(number): for i in range(1, number + 1): ans = ans * i print(f"The factorial of {number} is {ans}") - return ans \ No newline at end of file + 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}") \ No newline at end of file diff --git a/__pycache__/Functions.cpython-312.pyc b/__pycache__/Functions.cpython-312.pyc index caee1d6216d3788b44e28689c51785bc1a7bb196..c9b07848b5d38cf7667c7784171814f8d1c1112e 100644 GIT binary patch delta 452 zcmbQry_kphG%qg~0}x0H-pg1yk#`DX)Wk)$8_!K(Qe*>)G6V7F2S8#vLkeRpV+}(J zQ#KO=LlIvMLoMUvYGy@7&BTtXvBszm}qI#$E2WBAu2~d}S+JfQ*sTcTku5jpn;9}t6Z}9rU&LAW{!HO8oiVM^h wNURW9k-Q@G0-x~}4wD<)d<|ZcMOoDuPx# delta 411 zcmZ3?GnJe7G%qg~0}#CIyPIJ?k#`EC=fp*}8$Z-D$+81QnSuE8A|NrHA%(G)v4$aq zDVvFbp@?Vl1ZKs0o)Si&6d0titcJ1~87dh{K>8r!AU%v#K=x{&J`iwWh?QhusAZ~Q ztYNBR1+o~COkoA8W-DQVs08YPvKSe9q*;LGYO?zki2@~y#DGNA>VmQrrCTzu@;H2C1<4n21I_o-WG~_b f(nY)s43oFAiZRMgKEtZUs5$vFs|F`0BUBXt25McG From d77baee916417645c23fecd6f2a9987894de6c99 Mon Sep 17 00:00:00 2001 From: alvarezba Date: Wed, 1 Oct 2025 23:56:53 -0700 Subject: [PATCH 4/4] Added greatest common denominator --- Functions.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Functions.py b/Functions.py index c75c367..0eba98a 100644 --- a/Functions.py +++ b/Functions.py @@ -79,4 +79,35 @@ def square_root(number): 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}") \ No newline at end of file + 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