0

GCD time complexity

Solved: True Help me how to calculate time complexity of GCD function: Int GCD(int a, int b) { While (b != 0) { int tmp = a % b; a = b; b = tmp; } return a; }

16th Oct 2024, 10:31 PM
AraL
AraL - avatar
12 Respostas
+ 1
Bob_Li thanks, that was useful and I understood the answer:)
24th Oct 2024, 6:15 PM
AraL
AraL - avatar
0
AraL what are you trying to do? Can you provide brief information?
17th Oct 2024, 7:12 AM
Alhaaz
Alhaaz - avatar
0
Alhaaz this loop is not infinite, look at while condition, after some iterations, finally b variable will become 0 and then while will be end....
17th Oct 2024, 8:21 AM
AraL
AraL - avatar
0
Alhaaz give some number to variables and trace it
17th Oct 2024, 8:21 AM
AraL
AraL - avatar
0
AraL you want to find GCD(Greatest Common Division) right? Here is the correct syntax: - int GCD(){ while(a != b) { if(a> b) a-=b; else b-= a; } return a; }
17th Oct 2024, 9:26 AM
Alhaaz
Alhaaz - avatar
0
Alhaaz my question is finding time complexity of this code, I didn't mean I want a code to find GCD
17th Oct 2024, 10:38 AM
AraL
AraL - avatar
0
Time complexity of this code is O(log(min(a,b)) but I don't know the way to find this time complexity
17th Oct 2024, 10:39 AM
AraL
AraL - avatar
0
a >= b and a reduces with iteration, so it's log(min(a, b))
17th Oct 2024, 3:01 PM
Sharpneli
Sharpneli - avatar
0
Sharpneli I can't understand, need more explanation
17th Oct 2024, 3:09 PM
AraL
AraL - avatar
0
Bob_Li thanks, but it didn't help because finding the time complexity of this algorithm is almost different with algorithm that just use (+, -, *, ÷ ...)
19th Oct 2024, 4:41 PM
AraL
AraL - avatar