Calculate Greatest Common Divisor (GCD). >>> greatest_common_divisor(24, 40) 8 >>> greatest_common_divisor(1, 1) 1 >>> greatest_common_divisor(1, 800) 1 >>> greatest_common_divisor(11, 37) 1 >>> greatest_common_divisor(3, 5) 1 >>> greatest_common_divi
(a: int, b: int)
| 8 | |
| 9 | |
| 10 | def greatest_common_divisor(a: int, b: int) -> int: |
| 11 | """ |
| 12 | Calculate Greatest Common Divisor (GCD). |
| 13 | >>> greatest_common_divisor(24, 40) |
| 14 | 8 |
| 15 | >>> greatest_common_divisor(1, 1) |
| 16 | 1 |
| 17 | >>> greatest_common_divisor(1, 800) |
| 18 | 1 |
| 19 | >>> greatest_common_divisor(11, 37) |
| 20 | 1 |
| 21 | >>> greatest_common_divisor(3, 5) |
| 22 | 1 |
| 23 | >>> greatest_common_divisor(16, 4) |
| 24 | 4 |
| 25 | >>> greatest_common_divisor(-3, 9) |
| 26 | 3 |
| 27 | >>> greatest_common_divisor(9, -3) |
| 28 | 3 |
| 29 | >>> greatest_common_divisor(3, -9) |
| 30 | 3 |
| 31 | >>> greatest_common_divisor(-3, -9) |
| 32 | 3 |
| 33 | >>> greatest_common_divisor(0, 0) |
| 34 | 0 |
| 35 | """ |
| 36 | return abs(b) if a == 0 else greatest_common_divisor(b % a, a) |
| 37 | |
| 38 | |
| 39 | def gcd_by_iterative(x: int, y: int) -> int: |
no outgoing calls
no test coverage detected