Skip to content

Commit af1c0ff

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent d16263e commit af1c0ff

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

maths/greatest_common_divisor.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
def greatest_common_divisor(a: int, b: int) -> int:
99
"""
1010
Calculate Greatest Common Divisor (GCD) using Euclidean algorithm recursively.
11-
11+
1212
The GCD of two integers is the largest positive integer that divides both numbers.
13-
13+
1414
Args:
1515
a: First integer
1616
b: Second integer
17-
17+
1818
Returns:
1919
The greatest common divisor of a and b
20-
20+
2121
Examples:
2222
Basic cases:
2323
>>> greatest_common_divisor(24, 40)
@@ -28,7 +28,7 @@ def greatest_common_divisor(a: int, b: int) -> int:
2828
25
2929
>>> greatest_common_divisor(17, 19)
3030
1
31-
31+
3232
Edge cases with small numbers:
3333
>>> greatest_common_divisor(1, 1)
3434
1
@@ -40,15 +40,15 @@ def greatest_common_divisor(a: int, b: int) -> int:
4040
1
4141
>>> greatest_common_divisor(16, 4)
4242
4
43-
43+
4444
Cases with zero:
4545
>>> greatest_common_divisor(0, 5)
4646
5
4747
>>> greatest_common_divisor(5, 0)
4848
5
4949
>>> greatest_common_divisor(0, 0)
5050
0
51-
51+
5252
Negative numbers:
5353
>>> greatest_common_divisor(-3, 9)
5454
3
@@ -62,19 +62,19 @@ def greatest_common_divisor(a: int, b: int) -> int:
6262
8
6363
>>> greatest_common_divisor(-48, 18)
6464
6
65-
65+
6666
Large numbers:
6767
>>> greatest_common_divisor(1071, 462)
6868
21
6969
>>> greatest_common_divisor(12345, 54321)
7070
3
71-
71+
7272
Same numbers:
7373
>>> greatest_common_divisor(42, 42)
7474
42
7575
>>> greatest_common_divisor(-15, -15)
7676
15
77-
77+
7878
One divides the other:
7979
>>> greatest_common_divisor(15, 45)
8080
15
@@ -87,17 +87,17 @@ def greatest_common_divisor(a: int, b: int) -> int:
8787
def gcd_by_iterative(x: int, y: int) -> int:
8888
"""
8989
Calculate Greatest Common Divisor (GCD) using iterative Euclidean algorithm.
90-
90+
9191
This method is more memory efficient because it does not create additional
9292
stack frames for recursive function calls.
93-
93+
9494
Args:
9595
x: First integer
9696
y: Second integer
97-
97+
9898
Returns:
9999
The greatest common divisor of x and y
100-
100+
101101
Examples:
102102
Basic cases:
103103
>>> gcd_by_iterative(24, 40)
@@ -108,15 +108,15 @@ def gcd_by_iterative(x: int, y: int) -> int:
108108
25
109109
>>> gcd_by_iterative(17, 19)
110110
1
111-
111+
112112
Verify equivalence with recursive version:
113113
>>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40)
114114
True
115115
>>> greatest_common_divisor(48, 18) == gcd_by_iterative(48, 18)
116116
True
117117
>>> greatest_common_divisor(100, 25) == gcd_by_iterative(100, 25)
118118
True
119-
119+
120120
Edge cases with small numbers:
121121
>>> gcd_by_iterative(1, 1)
122122
1
@@ -126,15 +126,15 @@ def gcd_by_iterative(x: int, y: int) -> int:
126126
1
127127
>>> gcd_by_iterative(16, 4)
128128
4
129-
129+
130130
Cases with zero:
131131
>>> gcd_by_iterative(0, 5)
132132
5
133133
>>> gcd_by_iterative(5, 0)
134134
5
135135
>>> gcd_by_iterative(0, 0)
136136
0
137-
137+
138138
Negative numbers:
139139
>>> gcd_by_iterative(-3, -9)
140140
3
@@ -148,19 +148,19 @@ def gcd_by_iterative(x: int, y: int) -> int:
148148
8
149149
>>> gcd_by_iterative(-48, 18)
150150
6
151-
151+
152152
Large numbers:
153153
>>> gcd_by_iterative(1071, 462)
154154
21
155155
>>> gcd_by_iterative(12345, 54321)
156156
3
157-
157+
158158
Same numbers:
159159
>>> gcd_by_iterative(42, 42)
160160
42
161161
>>> gcd_by_iterative(-15, -15)
162162
15
163-
163+
164164
One divides the other:
165165
>>> gcd_by_iterative(15, 45)
166166
15
@@ -175,10 +175,10 @@ def gcd_by_iterative(x: int, y: int) -> int:
175175
def main():
176176
"""
177177
Call Greatest Common Divisor function with user input.
178-
178+
179179
Prompts user for two integers separated by comma and calculates their GCD
180180
using both recursive and iterative methods.
181-
181+
182182
Examples:
183183
This function handles user input, so direct doctests aren't practical.
184184
However, it should handle valid input like "24,40" and invalid input gracefully.
@@ -197,4 +197,4 @@ def main():
197197

198198

199199
if __name__ == "__main__":
200-
main()
200+
main()

0 commit comments

Comments
 (0)