MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / gcd_by_iterative

Function gcd_by_iterative

maths/greatest_common_divisor.py:39–60  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

37
38
39def 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
63def main():

Callers 6

simplify_fractionFunction · 0.90
find_mod_inverseFunction · 0.90
check_keysFunction · 0.90
get_random_keyFunction · 0.90
generate_keyFunction · 0.90
mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected