DEV Community

slackman
slackman

Posted on

Python math.gcd

(10,2) => 2
(3, 5) => 1

def gcd(a,b):
    if a < b:
        a,b = b,a
    while b:
        a,b = b,a%b
    return a

import math

assert gcd(13, 2436) == 1
assert gcd(10, 2) == 2
assert math.gcd(13, 2436) == 1
assert math.gcd(10, 2) == 2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)