Skip to content

Add comprehensive doctests for maths/greatest_common_divisor.py #12829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 6, 2025
commit af1c0ff39d7357f16dd9c0309973d9d9391990ed
48 changes: 24 additions & 24 deletions maths/greatest_common_divisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
def greatest_common_divisor(a: int, b: int) -> int:
"""
Calculate Greatest Common Divisor (GCD) using Euclidean algorithm recursively.

The GCD of two integers is the largest positive integer that divides both numbers.

Args:
a: First integer
b: Second integer

Returns:
The greatest common divisor of a and b

Examples:
Basic cases:
>>> greatest_common_divisor(24, 40)
Expand All @@ -28,7 +28,7 @@ def greatest_common_divisor(a: int, b: int) -> int:
25
>>> greatest_common_divisor(17, 19)
1

Edge cases with small numbers:
>>> greatest_common_divisor(1, 1)
1
Expand All @@ -40,15 +40,15 @@ def greatest_common_divisor(a: int, b: int) -> int:
1
>>> greatest_common_divisor(16, 4)
4

Cases with zero:
>>> greatest_common_divisor(0, 5)
5
>>> greatest_common_divisor(5, 0)
5
>>> greatest_common_divisor(0, 0)
0

Negative numbers:
>>> greatest_common_divisor(-3, 9)
3
Expand All @@ -62,19 +62,19 @@ def greatest_common_divisor(a: int, b: int) -> int:
8
>>> greatest_common_divisor(-48, 18)
6

Large numbers:
>>> greatest_common_divisor(1071, 462)
21
>>> greatest_common_divisor(12345, 54321)
3

Same numbers:
>>> greatest_common_divisor(42, 42)
42
>>> greatest_common_divisor(-15, -15)
15

One divides the other:
>>> greatest_common_divisor(15, 45)
15
Expand All @@ -87,17 +87,17 @@ def greatest_common_divisor(a: int, b: int) -> int:
def gcd_by_iterative(x: int, y: int) -> int:
"""
Calculate Greatest Common Divisor (GCD) using iterative Euclidean algorithm.

This method is more memory efficient because it does not create additional
stack frames for recursive function calls.

Args:
x: First integer
y: Second integer

Returns:
The greatest common divisor of x and y

Examples:
Basic cases:
>>> gcd_by_iterative(24, 40)
Expand All @@ -108,15 +108,15 @@ def gcd_by_iterative(x: int, y: int) -> int:
25
>>> gcd_by_iterative(17, 19)
1

Verify equivalence with recursive version:
>>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40)
True
>>> greatest_common_divisor(48, 18) == gcd_by_iterative(48, 18)
True
>>> greatest_common_divisor(100, 25) == gcd_by_iterative(100, 25)
True

Edge cases with small numbers:
>>> gcd_by_iterative(1, 1)
1
Expand All @@ -126,15 +126,15 @@ def gcd_by_iterative(x: int, y: int) -> int:
1
>>> gcd_by_iterative(16, 4)
4

Cases with zero:
>>> gcd_by_iterative(0, 5)
5
>>> gcd_by_iterative(5, 0)
5
>>> gcd_by_iterative(0, 0)
0

Negative numbers:
>>> gcd_by_iterative(-3, -9)
3
Expand All @@ -148,19 +148,19 @@ def gcd_by_iterative(x: int, y: int) -> int:
8
>>> gcd_by_iterative(-48, 18)
6

Large numbers:
>>> gcd_by_iterative(1071, 462)
21
>>> gcd_by_iterative(12345, 54321)
3

Same numbers:
>>> gcd_by_iterative(42, 42)
42
>>> gcd_by_iterative(-15, -15)
15

One divides the other:
>>> gcd_by_iterative(15, 45)
15
Expand All @@ -175,10 +175,10 @@ def gcd_by_iterative(x: int, y: int) -> int:
def main():
"""
Call Greatest Common Divisor function with user input.

Prompts user for two integers separated by comma and calculates their GCD
using both recursive and iterative methods.

Examples:
This function handles user input, so direct doctests aren't practical.
However, it should handle valid input like "24,40" and invalid input gracefully.
Expand All @@ -197,4 +197,4 @@ def main():


if __name__ == "__main__":
main()
main()
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy