Introducing …. the Greedy Calvinosaurus Dinosaur
In this week's post, learn about Greatest Common Divisor.
Last week, we introduced several questions based on calculating the Greatest Common Divisor of several integers and polynomials. Using the Euclidean Algorithm is a standard approach to helping us to quickly calculate these values.
Judging from the comments in the solution discussions, here are some questions for you to ponder:
1) If , can ?
2) What is the value of ?
3) Why does the Euclidean algorithm work? Specifically, why does for any integers ?
For those who want a coding challenge:
Write code to calculate the GCD of 2 numbers, which ends in finitely many steps. You are not allowed to use conditions like “If n > 0, continue” or equivalent hacks.
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
I generally programme in Pascal. Apart from the setting-up exercises (to ensure that a and b are positive and ordered), the function calls itself recursively as many times as the Euclidean algorithm is used in the calculation of the gcd.
Log in to reply
As I said, you are not allowed to use "if n >0" conditions (similarly for "while n>0"). You have hidden it within calling of the gcd function.
The main aspect of this problem is to figure out the number of steps which the Euclidean algorithm has to take.
Log in to reply
So you want to know about the time complexity of the Euclidean algorithm. Try some consecutive Fibonacci numbers.
Log in to reply
Log in to reply
a≥b≥1, then the number of applications of the Division Algorithm needed to calculate the HCF of a and b is no greater than 5log10b. Thus:
IfHello Calvin! Will you post the answers to the 3 questions in the future? That would greatly help.
A few questions, how do you define gcd of negative numbers? Can you please give a few hints for the second question? In the first question, can n be negative?
Thanks!
I hope these are right:
1) Yes, if n<−10
2) 7
Log in to reply
Yup.
@Calvin What do you mean by " finite many steps"? Do you mean to write a code in the least amount of lines of code?
Log in to reply
It shouldn't have a phrase equivalent to "If n,m>0, continue".
It can have a phrase "For i = 1 to max(n,m)", though of course this is very inefficient.
Log in to reply
Is this one legit?
Log in to reply
Log in to reply
0 as one of the values - it would try to divide by 0.
Your code would crash if you enteredIf you fix this, the code is about optimal for speed, but it breaks the rules about no loops, like my first version did.
max(a,b), which is extremely tedious. What is the complexity of your statement? How many steps would it take for you to find the gcd two 109 numbers?
This has to check every number from 1 toLog in to reply
109 numbers'? Thanks for replying.
The complexity is O(n) I guess, because it would take about min(a, b) steps in the worst case to find the GCD. Right? How can I find the complexity of the one down below? I think it's O(log(n)) but I am not at all sure and have no idea how to find it. What does it mean to 'find the gcd twoLog in to reply
109."
I'm guessing he means "find the GCD of two numbers each aboutAlright. I'm working on a code right now...during class.
So in Python, we can't use a while loop?
Log in to reply
I'm not sure if this is allowed but it works.
astr = input("Type first number") bstr = input("Type second number") a = int(astr) b = int(bstr) while a < b or a > b: if a > b: a = a-b else: b = b-a if a == b: astr1 = str(a) print(astr1)
1) If n<10, then gcd(n,m) can be greater than 10. This is because if n can be negative too. Here's an example gcd(−20,40)=20.
2) gcd(−14,−21)=7
3) I'll post it later