The way you calc the GCD is complicated and a waste of resources.
Collecting all divisors of a number takes a lot of time.
Bug: You forgot that n is divisor of itself !
void Divisor(int n,vector<int>vec)
{
for(int i=1; i<n>+1; i++)>
{
if(n%i==0)
{
vec.push_back(i); }
}
}</n></int>
Greatest common divisor - Wikipedia, the free encyclopedia[
^]
[UpDate]
The problem with your program is that the bigger the numbers, longer it takes to get the answer. It is O(n).
The right way to do it is by taking advantage of the mathematical relation between the 2 numbers and the GCD.
The 2 numbers are multiples of the GCD, it imply that the difference between the 2 numbers is also a multiple of the GCD.
This means that GCD(A,B) <=> GCD(B, A%B)
This
int GCD(int A, int B)
{
int C;
C= A % B;
if (C == 0)
return B;
return GCD(B, C);
}
is a recursive version.