Below method is more memory efficient because it does not create additional stack frames for recursive functions calls (as done in the above method). >>> gcd_by_iterative(24, 40) 8 >>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40) True >>> gcd_by_iterative(
(x: int, y: int)
| 37 | |
| 38 | |
| 39 | def gcd_by_iterative(x: int, y: int) -> int: |
| 40 | """ |
| 41 | Below method is more memory efficient because it does not create additional |
| 42 | stack frames for recursive functions calls (as done in the above method). |
| 43 | >>> gcd_by_iterative(24, 40) |
| 44 | 8 |
| 45 | >>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40) |
| 46 | True |
| 47 | >>> gcd_by_iterative(-3, -9) |
| 48 | 3 |
| 49 | >>> gcd_by_iterative(3, -9) |
| 50 | 3 |
| 51 | >>> gcd_by_iterative(1, -800) |
| 52 | 1 |
| 53 | >>> gcd_by_iterative(11, 37) |
| 54 | 1 |
| 55 | >>> gcd_by_iterative(0, 0) |
| 56 | 0 |
| 57 | """ |
| 58 | while y: # --> when y=0 then loop will terminate and return x as final GCD. |
| 59 | x, y = y, x % y |
| 60 | return abs(x) |
| 61 | |
| 62 | |
| 63 | def main(): |
no outgoing calls
no test coverage detected