From 310efd163049daa820e8c0979dda879fc2c756cb Mon Sep 17 00:00:00 2001 From: Sai Sravya Thumati <64857617+Sai-Sravya-Thumati@users.noreply.github.com> Date: Mon, 6 Oct 2025 11:37:47 +0530 Subject: [PATCH 1/3] Making it more readable by adding necessary details! --- maths/basic_maths.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 833f31c18b9e..3d6bb7962048 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -4,6 +4,11 @@ def prime_factors(n: int) -> list: + """ + Uses a standard method of dividing by 2, then odd divisors up to sqrt(n). + Time Complexity: O(sqrt(n)) + Space Complexity: O(log n) on average + """ """Find Prime Factors. >>> prime_factors(100) [2, 2, 5, 5] @@ -120,3 +125,4 @@ def euler_phi(n: int) -> int: import doctest doctest.testmod() + From 2edc2c6ffd78e75228e1daf10c422b4963bdc03f Mon Sep 17 00:00:00 2001 From: Sai Sravya Thumati <64857617+Sai-Sravya-Thumati@users.noreply.github.com> Date: Mon, 6 Oct 2025 11:41:02 +0530 Subject: [PATCH 2/3] Adding new algorithms in here which are very necessary to know! --- maths/basic_maths.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 3d6bb7962048..83a826974321 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -2,6 +2,42 @@ import math +def gcd(a: int, b: int) -> int: + """Calculate the Greatest Common Divisor (GCD) using Euclid's Algorithm. + >>> gcd(54, 24) + 6 + >>> gcd(10, 0) + 10 + >>> gcd(0, 10) + 10 + >>> gcd(0, 0) + Traceback (most recent call last): + ... + ValueError: At least one number must be non-zero + >>> gcd(-54, 24) + 6 + """ + if a == 0 and b == 0: + raise ValueError("At least one number must be non-zero") + a, b = abs(a), abs(b) + while b: + a, b = b, a % b + return a + + +def lcm(a: int, b: int) -> int: + """Calculate the Least Common Multiple (LCM) using GCD. + >>> lcm(12, 15) + 60 + >>> lcm(0, 10) + 0 + >>> lcm(-12, 15) + 60 + """ + if a == 0 or b == 0: + return 0 + return abs(a * b) // gcd(a, b) + def prime_factors(n: int) -> list: """ @@ -126,3 +162,4 @@ def euler_phi(n: int) -> int: doctest.testmod() + From 327f69648d499cdefe3914ffd6ba6cd25e60afa2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 06:12:11 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/basic_maths.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 83a826974321..e00efc734e91 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -2,6 +2,7 @@ import math + def gcd(a: int, b: int) -> int: """Calculate the Greatest Common Divisor (GCD) using Euclid's Algorithm. >>> gcd(54, 24) @@ -161,5 +162,3 @@ def euler_phi(n: int) -> int: import doctest doctest.testmod() - -