From c1c2b3f38df1a89e667595c9053243effec4e231 Mon Sep 17 00:00:00 2001 From: agarwalpranay02 Date: Wed, 29 Oct 2025 01:00:09 +0530 Subject: [PATCH 1/5] Create factorial_iterative.py --- maths/factorial_iterative.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 maths/factorial_iterative.py diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py new file mode 100644 index 000000000000..429dce979a74 --- /dev/null +++ b/maths/factorial_iterative.py @@ -0,0 +1,16 @@ +def factorial_iterative(n: int) -> int: + """ + Return the factorial of n using an iterative approach. + Raises ValueError for negative inputs. + """ + if n < 0: + raise ValueError("Input must be a non-negative integer") + result = 1 + for i in range(1, n + 1): + result *= i + return result + + +if __name__ == "__main__": + # simple demonstration + print(factorial_iterative(5)) # expected 120 From b4fb04a10b285ec245d803fd00e3d5a2262c5a54 Mon Sep 17 00:00:00 2001 From: agarwalpranay02 Date: Wed, 29 Oct 2025 01:24:52 +0530 Subject: [PATCH 2/5] Update factorial_iterative.py Address review: add doctests and descriptive parameter name --- maths/factorial_iterative.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index 429dce979a74..deffc1d8988e 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial_iterative.py @@ -1,12 +1,19 @@ -def factorial_iterative(n: int) -> int: +def factorial_iterative(number: int) -> int: """ - Return the factorial of n using an iterative approach. - Raises ValueError for negative inputs. + Return the factorial of a non-negative integer using an iterative method. + + >>> factorial_iterative(5) + 120 + >>> factorial_iterative(0) + 1 + >>> factorial_iterative(1) + 1 """ - if n < 0: + if number < 0: raise ValueError("Input must be a non-negative integer") + result = 1 - for i in range(1, n + 1): + for i in range(2, number + 1): result *= i return result @@ -14,3 +21,4 @@ def factorial_iterative(n: int) -> int: if __name__ == "__main__": # simple demonstration print(factorial_iterative(5)) # expected 120 + From da16917b497485aab53aa2c1c514efeb19378bc6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Oct 2025 19:55:11 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/factorial_iterative.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index deffc1d8988e..752f355b8821 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial_iterative.py @@ -21,4 +21,3 @@ def factorial_iterative(number: int) -> int: if __name__ == "__main__": # simple demonstration print(factorial_iterative(5)) # expected 120 - From cfe0d16e2d4fd301ec90f25a728af37777d1713e Mon Sep 17 00:00:00 2001 From: agarwalpranay02 Date: Wed, 29 Oct 2025 01:30:40 +0530 Subject: [PATCH 4/5] Create gcd_iterative.py Add iterative GCD implementation --- maths/gcd_iterative.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 maths/gcd_iterative.py diff --git a/maths/gcd_iterative.py b/maths/gcd_iterative.py new file mode 100644 index 000000000000..bc998dad2a43 --- /dev/null +++ b/maths/gcd_iterative.py @@ -0,0 +1,13 @@ +def gcd_iterative(a: int, b: int) -> int: + """ + Compute the greatest common divisor (GCD) of two numbers iteratively. + + Examples: + >>> gcd_iterative(48, 18) + 6 + >>> gcd_iterative(7, 5) + 1 + """ + while b != 0: + a, b = b, a % b + return a From 56a97a99fb745caaa6891e9cdd3f7f56accc5e82 Mon Sep 17 00:00:00 2001 From: agarwalpranay02 Date: Wed, 29 Oct 2025 01:33:59 +0530 Subject: [PATCH 5/5] Update gcd_iterative.py Fix: replace single letter parameters with descriptive names --- maths/gcd_iterative.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/gcd_iterative.py b/maths/gcd_iterative.py index bc998dad2a43..3d3cd7d4ab8b 100644 --- a/maths/gcd_iterative.py +++ b/maths/gcd_iterative.py @@ -1,4 +1,4 @@ -def gcd_iterative(a: int, b: int) -> int: +def gcd_iterative(first_number: int, second_number: int) -> int: """ Compute the greatest common divisor (GCD) of two numbers iteratively. @@ -8,6 +8,6 @@ def gcd_iterative(a: int, b: int) -> int: >>> gcd_iterative(7, 5) 1 """ - while b != 0: - a, b = b, a % b - return a + while second_number != 0: + first_number, second_number = second_number, first_number % second_number + return first_number