Here is a function (in Python) which (hopefully) returns the gcd of two positive integers a and b:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Assuming and are distinct positive integers, on each iteration of the loop starting on line 5 gets smaller.
The same goes when and are of opposite parity.
Suppose and are distinct positive integers not both even. If is a common divisor of and , then must also be odd.
If both and are odd, then is even. Let be the largest power of 2 dividing . Since and are relative prime, then is a divisor of . Therefore is also a divisor of and of .
By a similar argument, one can see that is a common divisor of and (resp. and ) in case of and having opposite parity.
So if is a common divisor of and , then also is a divisor of the value returned by the bgcd function.
Lemma: Let and (n) be non-zero integers such that and . Then or (m=-n).
Letting m=\textrm{gcd(a,b), and the value returned by the bgcd function, we need to show . From the definition of the greatest common divisor it follows that if n \mid a) and \(n \mid b), then \(n \mid m. Clearly this is the case when . So we are left with the cases , . Suppose . Then .
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
Please fix the LaTeX and your general formatting. Very hard to read when you display a lot of things that need not be displayed.