Euclid's Lemma : d divides a and b, if and only if d divides a-b and b Euclid's Algorithm >>> greatest_common_divisor(7,5) 1 Note : In number theory, two integers a and b are said to be relatively prime, mutually prime, or co-prime if the only positive integer (factor
(a: int, b: int)
| 127 | |
| 128 | |
| 129 | def greatest_common_divisor(a: int, b: int) -> int: |
| 130 | """ |
| 131 | Euclid's Lemma : d divides a and b, if and only if d divides a-b and b |
| 132 | Euclid's Algorithm |
| 133 | |
| 134 | >>> greatest_common_divisor(7,5) |
| 135 | 1 |
| 136 | |
| 137 | Note : In number theory, two integers a and b are said to be relatively prime, |
| 138 | mutually prime, or co-prime if the only positive integer (factor) that divides |
| 139 | both of them is 1 i.e., gcd(a,b) = 1. |
| 140 | |
| 141 | >>> greatest_common_divisor(121, 11) |
| 142 | 11 |
| 143 | |
| 144 | """ |
| 145 | if a < b: |
| 146 | a, b = b, a |
| 147 | |
| 148 | while a % b != 0: |
| 149 | a, b = b, a % b |
| 150 | |
| 151 | return b |
| 152 | |
| 153 | |
| 154 | if __name__ == "__main__": |