Python: Greatest Common Divisor (GCD)

preview_player
Показать описание
The greatest common divisor (GCD) is the largest integer that evenly divides two numbers. (GCD is also called the Greatest Common Factor.) For example, the GCD of 21 and 14 is 7. This video shows you a beautiful recursive definition of GCD and how to write it in Python.
Рекомендации по теме
Комментарии
Автор

As a curiosity you can also write
def GCD(a, b):
return a if b==0 else GCD(b, a%b)

if anyone is obsessed with shortcode :))

kacpermokrzycki
Автор

i don't understand how can a%b and b%a can be the same thing

mrgfdgfd
Автор

we do this in school and we haven't taken the recursive functions yet, is there an easier way to write a function like this

martinesse
Автор

Do you type in a percentage sign to represent the mod?

twright
Автор

Am i a crappy student if i can't figure this out alone... :(

CFGPGFF
Автор

It's better to use ternar operator:
def gcd(a, b):
return gcd(b % a, a) if a and b else max(a, b)

spirridd
Автор

lol i did it toatly diffrently yours will be faster though they boath work

siddietrich
visit shbcf.ru